ViewPager窃取儿童碎片中的水平触摸事件

时间:2017-03-18 22:11:36

标签: java android android-fragments

所以我有ViewPagerViewPager包含两个标签,每个标签包含RecyclerView,可以在每个RecyclerView中添加和删除卡片。每个RecyclerView都包含在Fragment中。每个RecyclerView使用ItemTouchHelper来管理onSwiped()个事件。 RecyclerView内的卡片有几种行为:

  • 向右滑动可打开卡片的编辑器
  • 向左滑动会删除卡
  • 按住卡片并垂直向上或向下拖动重新定位卡片。

我的问题是ViewPager拦截了每个onSwiped()的{​​{1}}方法,但是,每个RecyclerView仍然可以正确接收垂直触摸事件。显然,这归结为触摸冲突问题。 RecyclerView想要更改标签的水平触摸事件,ViewPager想要水平触摸事件来刷卡。但是,RecyclerView正在窃取所有触摸事件。我已在ViewPager上禁用水平滑动(用户可以通过点击标签指示符来更改标签的唯一方式),但ViewPager仍然无法接收任何水平触摸事件。

此处RecyclerView已禁用滑动:

ViewPager

这是一个片段类:

public class NoSwipePager extends ViewPager {

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

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
    return false;
}
}

以下是管理@Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { view = inflater.inflate(R.layout.pit_tab, container, false); container.getParent().requestDisallowInterceptTouchEvent(true); RecyclerView rv = (RecyclerView) view.findViewById(R.id.movie_recycler_view); return view; }

的课程
RecyclerView

最后,这里应该调用的方法,但不是:

    public EditManager(RecyclerView rv, Context act, EditListener listener, ArrayList<Element> elements, boolean editing, boolean pit) {
    //rv = (RecyclerView) act.findViewById(R.id.movie_recycler_view);
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(act);
    linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    rv.setLayoutManager(linearLayoutManager);
    ((SimpleItemAnimator) rv.getItemAnimator()).setSupportsChangeAnimations(false);

    if(elements != null) elementsAdapter = new ElementsAdapter(listener, act, new LinkedList<>(elements), editing);
    else elementsAdapter = new ElementsAdapter(listener, act, null, editing);
    rv.setAdapter(elementsAdapter);

    ItemTouchHelper.Callback callback = new ElementTouchHelper(elementsAdapter);
    ItemTouchHelper helper = new ItemTouchHelper(callback);
    helper.attachToRecyclerView(rv);
}

1 个答案:

答案 0 :(得分:1)

尝试将var states = ['Alabama', 'Alaska']; var pop = [4863300, 741894]; var average = pop.reduce(function(sum, val){return sum + val;}) / pop.length; console.log('Average: ' + average); states.map(function(state, index) { // Convert 2 arrays to an array of objects representing state info return { name: state, population: pop[index] }; }).filter(function(stateInfo) { console.log(stateInfo); // Filter each item by returning true on items you want to include return stateInfo.population < average; }).forEach(function(stateInfo) { // Lastly, loop through your results console.log(stateInfo.name + ' has ' + stateInfo.population + ' people'); });替换为:

NoSwipePager

我已成功使用此方法为public class NoSwipePager extends ViewPager { public NoSwipePager(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected boolean canScroll(View v, boolean checkV, int dx, int x, int y) { if (v instanceof RecyclerView) { return(true); } return(super.canScroll(v, checkV, dx, x, y)); } } 页面中的地图(用ViewPager替换SurfaceView)。我的猜测是你需要用更复杂的东西替换RecyclerView测试(并根据你的应用自定义)。但我认为v instanceof RecyclerView更有可能为您提供所需内容而非阻止canScroll()