滚动多个水平RecyclerView在一起多个、水平、RecyclerView

2023-09-08 08:30:49 作者:優雅的轉身然後得瑟的走開

我创建一个EPG一样的显示效果,对此我有多个水平 RecyclerView S(如电视节目)一个LinearLayout中的内部封装。当我滚动RecyclerView之一,我想要的意见,其余将被滚动起来。

I'm creating an EPG like view for which I have multiple horizontal RecyclerViews (as tv programs) encapsulated inside a LinearLayout. When I scroll one of the RecyclerView, I want the rest of the views to be scrolled together.

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    layoutContent.setWeightSum(epg.getChannels().size());

    //prepare recycler views and add into layoutContent based on epg channels
    for(EPG.Channel ch : epg.getChannels()){
        AppLog.error(TAG, "Creating RecyclerView for: " + ch.getDisplayName());

        //create new recycler view
        final RecyclerView rv = new RecyclerView(layoutContent.getContext());
        lstRecyclerViews.add(rv);

        //set layout manager
        rv.setLayoutManager(new LinearLayoutManager(layoutContent.getContext(), LinearLayoutManager.HORIZONTAL, false));

        //create adapter
        rv.setAdapter(new MyAdapter(ch.getPrograms()));
        rv.setItemAnimator(new DefaultItemAnimator());

        //add into parent layout
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0);
        lp.weight = 1;
        layoutContent.addView(rv, lp);
    }
}

我尝试添加滚动监听我的意见,但我很困惑与 RecyclerView.OnScrollListener 的 onScrolled 方式,我可以'吨弄清楚如何滚动其他意见。

I've tried adding a scroll listener to my views but I'm confused with RecyclerView.OnScrollListener's onScrolled method as i can't figure out how to scroll other views.

任何帮助/建议,将是有益的。

Any help/suggestion would be helpful.

推荐答案

您需要随时重新计算水平回收的观点经常项目的(下一HRV)的位置 滚动在一个h.r.v.后基于少量的卷动的重新计算别人的立场发生在触及HRV

You need to always re-calculate position of current items in horizontal recycler views(next h.r.v.) After scrolling in one h.r.v. re-calculate positions of others based on small amount of scroll movement occured in touched h.r.v.

然后重写 onViewAttachedToWindow 适配器和使用方法 scrollToPositionWithOffset LinearLayoutManager 将其设置为正确的位置。

Then override onViewAttachedToWindow in adapter and use method scrollToPositionWithOffset from LinearLayoutManager of particularly h.r.v to set it to right position.

另外计算运动DX的时候,不要忘了关闭 onScrolled 方法的在手指,以避免重复处理同样的事件。

Also when calculating movement dx, don't forget to disable onScrolled method when finger is down to avoid multiple handling of the same event.

 
精彩推荐
图片推荐