Introduction:
Here I am sharing a code to perform delete operation on listview by just swiping it to right or left.
- Keep in mind this thing will only work in 4.0 or above. First Download the Demo from here.
- After extracting the zip you will find three class file in src project.
1) Main Activity
- The Class which displays the view to user and also has listener implementation on button and listview.
2) SwipeDismissTouchListener
- This class is responsible to listen swipe over the button’s object.
dismissableButton.setOnTouchListener(new SwipeDismissTouchListener(
dismissableButton,
null,
new SwipeDismissTouchListener.OnDismissCallback() {
@Override
public void onDismiss(View view, Object token) {
dismissableContainer.removeView(dismissableButton);
}
}));
3) SwipeDismissListViewTouchListener
- This class is responsible to listen swipe over the listview’s item object. In below code snippet just check that we are passing listview object while constructing an object of this class.
SwipeDismissListViewTouchListener touchListener =
new SwipeDismissListViewTouchListener(
listView,
new SwipeDismissListViewTouchListener.OnDismissCallback() {
@Override
public void onDismiss(ListView listView, int[] reverseSortedPositions) {
for (int position : reverseSortedPositions) {
myAdapter.remove(myAdapter.getItem(position));
}
myAdapter.notifyDataSetChanged();
}
});
- Both SwipeDismissTouchListener and SwipeDismissListViewTouchListener implements OnTouchListener which listens every touch on particular views.
- You can also download source code from here.


0 comments :
Post a Comment