如何在android中同步滚动两个Recyclerviews?

时间:2016-08-18 08:31:52

标签: android android-recyclerview android-scroll epg

我正在尝试使用Recyclerviews在Android中创建一个EPG。它需要固定的顶行,水平滚动显示与时间对应的程序,固定最左侧的列,垂直滚动显示各种通道。

根据this SO回答,我附上了design

    <?xml version="1.0" encoding="utf-8"?>
    <!--Outer container layout-->
    <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context=".MainActivity">

    <!--To display Channels list-->
    <LinearLayout
        android:layout_width="150dp"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <!--Position (0,0)-->
        <TextView
            android:id="@+id/tv_change_date"
            android:layout_width="match_parent"
            android:layout_height="50dp" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/rcv_channel_name"
            android:scrollbars="none"
            android:layout_width="150dp"
            android:layout_height="match_parent"/>

        </LinearLayout>


    <!--To display Time and Programs list-->
    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <!--Time horizontal list-->
                <android.support.v7.widget.RecyclerView
                    android:id="@+id/rcv_vertical_header"
                    android:scrollbars="none"
                    android:layout_width="match_parent"
                    android:layout_height="50dp"/>

                <!--Vertical list whose each element is horizontal list to show programs-->
                <android.support.v7.widget.RecyclerView
                    android:id="@+id/rcv_vertical"
                    android:scrollbars="vertical"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"/>
            </LinearLayout>

    </HorizontalScrollView>
</LinearLayout>

现在,我需要同步rcv_vertical和rcv_channel_name的垂直滚动。我在此github project中实现了它。

public class SelfRemovingOnScrollListener extends RecyclerView.OnScrollListener {

    @Override
    public final void onScrollStateChanged(@NonNull final RecyclerView recyclerView, final int newState) {
        super.onScrollStateChanged(recyclerView, newState);
        if (newState == RecyclerView.SCROLL_STATE_IDLE) {
            recyclerView.removeOnScrollListener(this);
        }
    }
}

在MainActivity中

private final RecyclerView.OnScrollListener channelScrollListener = new SelfRemovingOnScrollListener() {
    @Override
    public void onScrolled(@NonNull final RecyclerView recyclerView, final int dx, final int dy) {
        super.onScrolled(recyclerView, dx, dy);
        programsRecyclerView.scrollBy(dx, dy);
    }
};
private final RecyclerView.OnScrollListener programScrollListener = new     SelfRemovingOnScrollListener() {

    @Override
    public void onScrolled(@NonNull final RecyclerView recyclerView, final int dx, final int dy) {
        super.onScrolled(recyclerView, dx, dy);
        channelsRecyclerView.scrollBy(dx, dy);
    }
};

@Override
protected void onStart() {
    super.onStart();
 //Sync channel name RCV and Programs RCV scrolling
    channelsRecyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
        private int mLastY;

        @Override
        public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
            Log.d("debug", "LEFT: onInterceptTouchEvent");

            final Boolean ret = rv.getScrollState() != RecyclerView.SCROLL_STATE_IDLE;
            if (!ret) {
                onTouchEvent(rv, e);
            }
            return Boolean.FALSE;
        }

        @Override
        public void onTouchEvent(RecyclerView rv, MotionEvent e) {
            Log.d("debug", "LEFT: onTouchEvent");


            final int action;
            if ((action = e.getAction()) == MotionEvent.ACTION_DOWN && programsRecyclerView
                    .getScrollState() == RecyclerView.SCROLL_STATE_IDLE) {
                mLastY = rv.getScrollY();
                Log.d("scroll","channelsRecyclerView Y: "+mLastY);
                rv.addOnScrollListener(channelScrollListener);
            }
            else {
                if (action == MotionEvent.ACTION_UP && rv.getScrollY() == mLastY) {
                    rv.removeOnScrollListener(channelScrollListener);
                }
            }
        }

        @Override
        public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
            Log.d("debug", "LEFT: onRequestDisallowInterceptTouchEvent");
        }
    });

    programsRecyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {

        private int mLastY;

        @Override
        public boolean onInterceptTouchEvent(@NonNull final RecyclerView rv, @NonNull final
        MotionEvent e) {
            Log.d("debug", "RIGHT: onInterceptTouchEvent");

            final Boolean ret = rv.getScrollState() != RecyclerView.SCROLL_STATE_IDLE;
            if (!ret) {
                onTouchEvent(rv, e);
            }
            return Boolean.FALSE;
        }

        @Override
        public void onTouchEvent(@NonNull final RecyclerView rv, @NonNull final MotionEvent e) {
            Log.d("debug", "RIGHT: onTouchEvent");

            final int action;
            if ((action = e.getAction()) == MotionEvent.ACTION_DOWN && channelsRecyclerView
                    .getScrollState
                            () == RecyclerView.SCROLL_STATE_IDLE) {
                mLastY = rv.getScrollY();
                rv.addOnScrollListener(programScrollListener);
                Log.d("scroll","programsRecyclerView Y: "+mLastY);
            }
            else {
                if (action == MotionEvent.ACTION_UP && rv.getScrollY() == mLastY) {
                    rv.removeOnScrollListener(programScrollListener);
                }
            }
        }

        @Override
        public void onRequestDisallowInterceptTouchEvent(final boolean disallowIntercept) {
            Log.d("debug", "RIGHT: onRequestDisallowInterceptTouchEvent");
        }
    });
[![}][3]][3]

在Horizo​​ntalScrollView中进行水平滚动之前,它工作正常。之后,左回收者视图&#34; rcv_channel_name&#34;滚动速度比右rcv_vertical快。

我们非常感谢您提供任何帮助或建议。

2 个答案:

答案 0 :(得分:2)

<强> EDITED! 我终于成功地完成了这项工作! 这是我的简短解决方案(另请参阅使用d-pad进行基于焦点的输入的旧答案):

首先,您需要3个回收者(在片段或活动中):

public RecyclerView vertical_recycler, current_focus_recycler, time_recycler;

然后添加

public int scroll_offset;

此变量将保持滚动偏移(以像素为单位)以供进一步使用。 接下来正确填充您的垂直RecyclerView,确保您的RecyclerView具有水平RecyclerViews作为子项。还要确保你的time_recycler比任何horizo​​ntal_recyclers都长(在其适配器的getItemCount中返回Integer.MAX_VALUE将确保你好......有点......):

@Override
    public int getItemCount() {
        return Integer.MAX_VALUE;
    }

接下来在Activity或Fragment中添加滚动侦听器

time_recycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                horizontal_scroll(dx);
                if (dx == 0) {
                    scroll_offset = 0;
                } else {
                    scroll_offset += dx;
                }
            }
        });

public void horizontal_scroll(int dx) {
        for (int i = 0, n = vertical_recycler.getChildCount(); i < n; ++i) {
            if (vertical_recycler.getChildAt(i).findViewById(R.id.horizontal_recycler) != current_focus_recycler) {
                vertical_recycler.getChildAt(i).findViewById(R.id.horizontal_recycler).scrollBy(dx, 0);
            }
        }
    }

在垂直RecyclerView适配器中添加所需的其他功能:

private AdapterSheduleHorizontal adapter_horizontal;

    @Override
    public void onViewAttachedToWindow(AdapterSheduleVertical.VerticalViewHolder holder){
        super.onViewAttachedToWindow(holder);

        ((LinearLayoutManager) holder.horizontal_recycler.getLayoutManager())
                .scrollToPositionWithOffset(0,
                        -fragmentShedule.scroll_offset);
        holder.horizontal_recycler.addOnScrollListener(onScrollListener);

    }

    @Override
    public void onViewDetachedFromWindow(VerticalViewHolder holder) {
        super.onViewDetachedFromWindow(holder);
        holder.horizontal_recycler.removeOnScrollListener(onScrollListener);
    }


    RecyclerView.OnScrollListener onScrollListener = new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            if(recyclerView == fragmentShedule.current_focus_recycler) {
                fragmentShedule.time_recycler.scrollBy(dx, 0);
            }

        }

        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int state) {
            super.onScrollStateChanged(recyclerView, state);
            switch (state) {
                case RecyclerView.SCROLL_STATE_IDLE:
                    fragmentShedule.current_focus_recycler = null;
                    break;
            }
        }


    };

@Override
    public void onBindViewHolder(VerticalViewHolder sheduleViewHolder, final int position) {

...
        LinearLayoutManager lm = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
        sheduleViewHolder.horizontal_recycler.setLayoutManager(lm);
        adapter_horizontal = new AdapterSheduleHorizontal(fragmentShedule, context, dayItemList);
        sheduleViewHolder.horizontal_recycler.setAdapter(adapter_horizontal);
...
    }

接下来在水平的RecyclerView适配器

...
    RecyclerView parent_recyclerview;
@Override
    public void onBindViewHolder(final HorizontalViewHolder sheduleViewHolder, final int i) {


        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT);

        lp.width = 500;//some width to 

        sheduleViewHolder.shedule_item.setLayoutParams(lp);

        sheduleViewHolder.itemView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                switch (event.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        fragmentShedule.current_focus_recycler = parent_recyclerview;
                        break;
                }

                return false;
            }
        });

        sheduleViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context, "click"+i, Toast.LENGTH_SHORT).show();
            }
        });


    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        parent_recyclerview = recyclerView;
    }

我相信这一切。请随意发表评论并告诉我,如果它不适合你,我会仔细检查我的代码(我已经测试了所有孩子的垂直和水平滚动)。

OLD ANSWER

好的,我将展示我的方法,该方法适用于我的EPG外观应用程序。

让我们假设您有使用RecyclerView子级的RecyclerView。父级纵向滚动,子级 - 水平滚动。 在这个布局的顶部,你有另一个带有时间线的RecyclerView(也可以水平滚动)。

您需要滚动时间轴以滚动所有回收者 - 孩子,除了那个,获得焦点/被触摸/正在滚动(如果您将不使用触摸屏功能,您只能使用焦点水平视图的孩子。)

有人说过,你需要注册当前正在滚动的子垃圾回收站。

public RecyclerView vertical_recycler, current_focus_recycler, time_recycler;

我也注册了一次滚动所有水平RecyclerViews的方法:

public void horizontal_scroll(int dx) {
        for (int i = 0, n = vertical_recycler.getChildCount(); i < n; ++i) {
            if (vertical_recycler.getChildAt(i).findViewById(R.id.horizontal_recycler) != current_focus_recycler) {
                vertical_recycler.getChildAt(i).findViewById(R.id.horizontal_recycler).scrollBy(dx, 0);
            }
        }
    }

为我的time_recycler设置适配器后,我添加了listener:

time_recycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                horizontal_scroll(dx);
            }
        });

到目前为止一切顺利。 比在我添加的水平RecyclerView-child对应的适配器中:

RecyclerView parent_recyclerview;

RecyclerView.OnScrollListener onScrollListener = new RecyclerView.OnScrollListener() {
    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {


        super.onScrolled(recyclerView, dx, dy);
            ((ActivityMain) context).time_recycler.scrollBy(dx, 0);
        }
    };

//...

@Override
    public void onBindViewHolder(final HorizontalViewHolder viewHolder, final int i) {

    viewHolder.shedule_item.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (v.hasFocus()) {
                ((ActivityMain) context).current_focus_recycler = parent_recyclerview;
                parent_recyclerview.addOnScrollListener(onScrollListener);
            } else {
                parent_recyclerview.removeOnScrollListener(onScrollListener);
            }
        }
    });

}

@Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        parent_recyclerview = recyclerView;
    }

//...

class HorizontalViewHolder extends RecyclerView.ViewHolder {
        private LinearLayout shedule_item;
    public HorizontalViewHolder(View view) {
        super(view);
        shedule_item = (LinearLayout) view.findViewById(R.id.shedule_item);
    }
}

水平RecyclerView项目的布局是:

<LinearLayout
    android:id="@+id/shedule_item" 
            .../>
    ...
</LinearLayout>

如果我使用键盘并使用箭头键浏览epg表,这适用于我的情况。通过一些调整(灰烬中的一些疼痛),您将能够拦截来自父级水平回收者的触摸事件给它的孩子。希望能帮到你。

答案 1 :(得分:0)

继续罗马T上面提供的答案,这修复了回收者视图不同步的行为(主要是在向相反方向飞行的情况下造成)

sheduleViewHolder.itemView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            switch (event.getAction()){
                case MotionEvent.ACTION_DOWN:
                    if (fragmentShedule.current_focus_recycler != null) {
                        //stop any scroll due to fling on recyclerview currently in focus
                        fragmentShedule.current_focus_recycler.stopScroll();
                    }
                    fragmentShedule.current_focus_recycler = parent_recyclerview;
                    break;
            }

            return false;
        }
    });

添加了一个stopScroll方法,用于取消由于最后一次触摸的回收站视图上的投掷而导致的正在进行的滚动 P.S。:因为声誉低而提交这个作为答案。