This is what I have done. I have displayed the hidden components on item of listview when you swipe the item of listview now I need to hide the components again when other item gets swiped or the listview is scrolled .
public class MainActivity extends Activity {
ListView lstView1;
ArrayList<HashMap<String, String>> MyArrList;
GestureDetector gestureDetector;
View.OnTouchListener gestureListener;
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 100;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyArrList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map;
/*** Rows 1 ***/
map = new HashMap<String, String>();
map.put("ImageID", "1");
map.put("ImageDesc", "Sea View1");
map.put("ImagePath", "pic_a");
map.put("status", "false");
MyArrList.add(map);
/*** Rows 2 ***/
map = new HashMap<String, String>();
map.put("ImageID", "2");
map.put("ImageDesc", "Sea View2");
map.put("ImagePath", "pic_b");
map.put("status", "false");
MyArrList.add(map);
/*** Rows 3 ***/
map = new HashMap<String, String>();
map.put("ImageID", "3");
map.put("ImageDesc", "Sea View 3");
map.put("ImagePath", "pic_c");
map.put("status", "false");
MyArrList.add(map);
/*** Rows 4 ***/
map = new HashMap<String, String>();
map.put("ImageID", "4");
map.put("ImageDesc", "Sea View 4");
map.put("ImagePath", "pic_d");
map.put("status", "false");
MyArrList.add(map);
// listView1
lstView1 = (ListView) findViewById(R.id.listView1);
lstView1.setAdapter(new ImageAdapter(this));
// lstView1.setEnabled(false);
gestureDetector = new GestureDetector(new MyGestureDetector());
gestureListener = new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) {
return true;
}
return false;
}
};
lstView1.setOnTouchListener(gestureListener);
//lstView1.setOnScrollListener(l)
}
class MyGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
try {
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
return false;
// right to left swipe
if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
int pos = YourSlideRightToLeft(lstView1.pointToPosition(
(int) e1.getX(), (int) e1.getY()));
MyArrList.get(pos).put("status", "true");
((BaseAdapter) lstView1.getAdapter())
.notifyDataSetChanged();
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
// left to right swipe
YourSlideLeftToRight(lstView1.pointToPosition(
(int) e1.getX(), (int) e1.getY()));
int pos = YourSlideRightToLeft(lstView1.pointToPosition(
(int) e1.getX(), (int) e1.getY()));
MyArrList.get(pos).put("status", "true");
((BaseAdapter) lstView1.getAdapter())
.notifyDataSetChanged();
}
} catch (Exception e) {
// nothing
return true;
}
return true;
}
}
// Command for Slide Right to Left (<-----)
public int YourSlideRightToLeft(int position) {
Toast.makeText(
MainActivity.this,
"<-- Your Slide Right to Left ImgID = "
+ MyArrList.get(position).get("ImageID"),
Toast.LENGTH_SHORT).show();
return position;
}
// Command for Slide Left to Right (<-----)
public int YourSlideLeftToRight(int position) {
Toast.makeText(
MainActivity.this,
"---> Your Slide Left to Right ImgID = "
+ MyArrList.get(position).get("ImageID"),
Toast.LENGTH_SHORT).show();
return position;
}
public class ImageAdapter extends BaseAdapter {
private Context context;
public ImageAdapter(Context c) {
// TODO Auto-generated method stub
context = c;
}
public int getCount() {
// TODO Auto-generated method stub
return MyArrList.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(final int position, View convertView,
ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.activity_column, null);
}
// ColImage
ImageView imageView = (ImageView) convertView
.findViewById(R.id.ColImgPath);
imageView.getLayoutParams().height = 80;
imageView.getLayoutParams().width = 80;
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(5, 5, 5, 5);
int ResID = context.getResources().getIdentifier(
MyArrList.get(position).get("ImagePath"), "drawable",
context.getPackageName());
imageView.setImageResource(ResID);
// ColImgID
TextView txtPosition = (TextView) convertView
.findViewById(R.id.ColImgID);
txtPosition.setPadding(10, 0, 0, 0);
txtPosition.setText(MyArrList.get(position).get("ImageID") + ".");
// ColPicname
TextView txtPicName = (TextView) convertView
.findViewById(R.id.ColImgDesc);
txtPicName.setPadding(5, 0, 0, 0);
txtPicName.setText(MyArrList.get(position).get("ImageDesc"));
String value = MyArrList.get(position).get("status");
if (value.contentEquals("true")) {
((TableRow) convertView.findViewById(R.id.tableRow4))
.setVisibility(View.VISIBLE);
((Button) convertView.findViewById(R.id.btn)).setTag(Integer
.valueOf(position));
((ToggleButton) convertView.findViewById(R.id.tg))
.setTag(Integer.valueOf(position));
} else {
((LinearLayout) convertView.findViewById(R.id.tableRow4))
.setVisibility(View.GONE);
}
return convertView;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
This 'SwipeToDismissUndoList' code might help you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With