SwipeDismissListViewTouchListener in ListView and ViewPager

637 views Asked by At

I have a ViewPager that contains a ListFragment with a TouchListener (SwipeDismissListViewTouchListener). I modified the ViewPager like this:

public class CustomViewPager extends ViewPager {

public CustomViewPager(Context context, AttributeSet attrs) {
    super(context, attrs);
}

private boolean pagingEnabled = true;

public void setPagingEnabled(boolean enabled) {
    pagingEnabled = enabled;
}

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
    if (!pagingEnabled) {
        return false; // do not intercept
    }
    return super.onInterceptTouchEvent(event);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (!pagingEnabled) {
        return false; // do not consume
    }
    return super.onTouchEvent(event);
   }
  }

And I disable the swipe of the ViewPager from the onOptionsItemSelected in the ListFragment method.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_edit) {
        CustomViewPager vp = (CustomViewPager) getActivity().findViewById(R.id.pager);
        vp.setPagingEnabled(false);
        getListView().requestDisallowInterceptTouchEvent(true);

    }
    return super.onOptionsItemSelected(item);
}


@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    SwipeDismissListViewTouchListener touchListener =
            new SwipeDismissListViewTouchListener(
                    getListView(),
                    new SwipeDismissListViewTouchListener.DismissCallbacks() {

                        @Override
                        public boolean canDismiss(int position) {
                            return true;
                        }

                        @Override
                        public void onDismiss(ListView listView, int[] reverseSortedPositions) {
                            for (int position : reverseSortedPositions) {
                                removeItem(position);
                            }

                        }
                    }
            );

    getListView().setOnTouchListener(touchListener);
    // Setting this scroll listener is required to ensure that during ListView scrolling,
    // we don't look for swipes.
    getListView().setOnScrollListener(touchListener.makeScrollListener());


}

The swipe of the ViewPager is disabled but the problem that I can not running the SwipeDismissListViewTouchListener. I have tested these answers but do not work me

Nesting Android ViewPager, Swiping ListItems inside a ListView horizontally

ViewPager intercepts all x-axis onTouch events. How to disable?

Capture swipe to dismiss listview gestures in ViewPager

Any solution? Thanks

2

There are 2 answers

0
pablogupi On BEST ANSWER

I found solution for my error. In this question I answer how disable onClick method in Fabric library. Can I set custom onClick on my timeline using fabric sdk?

The project is same the project is the same and this related error. In this method disable views,clickable and long clickable:

//helper method to disable subviews
    private void disableViewAndSubViews(ViewGroup layout) { 
        layout.setEnabled(false); 
        for (int i = 0; i < layout.getChildCount(); i++) { 
            View child = layout.getChildAt(i); 
            if (child instanceof ViewGroup) { 
                disableViewAndSubViews((ViewGroup) child); 
            } else { 
                child.setEnabled(false); 
                child.setClickable(false); 
                child.setLongClickable(false); 
            } 
        } 
    } 

and disable method to swipe2dismiss

The solution is add on list adapter this lines:

 @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final View view = super.getView(position, convertView, parent);

        //disable subviews to avoid links are clickable
        ViewTweetUtils.disableViewAndSubViews((ViewGroup) view);

        //enable root view and attach custom listener
        view.setEnabled(true);
        view.setClickable(false);

        return view;
    } 

view.setEnabled only not works. You must add view.setClickable(false). Why? I don't know but it works.

Thanks for the help

1
EE66 On

Your issue should be solved in a few simple actions.

  • Remove the onInterceptTouch() and onTouch() at the ViewPager, its not needed.
  • If im not mistaken u r missing a call to notifyDatasetChange() after the for loop in the onDismiss().
  • Go into the SwipeTouchListener code and create a clause for direction so it will be enable for a ceratain direction (think about Google now that enable swipe only for one direction). Im guessing that u r enabling the swipe only for the first and last pages, if not then think about it becuase enabling swipe2dismiss for a middle page is a bad ux descion.