欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android若干条并排RecyclerView滑动实时联动

程序员文章站 2022-05-16 16:16:41
android若干条并排recyclerview滑动实时联动 以水平方向并排排列的两条recyclerview为例,实现一个简单的功能:这两个recyclerview要实时联合滚动,即其中任意一个r...

android若干条并排recyclerview滑动实时联动 以水平方向并排排列的两条recyclerview为例,实现一个简单的功能:这两个recyclerview要实时联合滚动,即其中任意一个recyclerview,将触发其余所有recyclerview同时滚动相同。
写布局:




    

    

    

代码:
package zhangphil.recyclerview;

import android.graphics.color;
import android.os.bundle;
import android.support.annotation.nonnull;
import android.support.annotation.nullable;
import android.support.v7.app.appcompatactivity;
import android.support.v7.widget.linearlayoutmanager;
import android.support.v7.widget.recyclerview;
import android.view.layoutinflater;
import android.view.view;
import android.view.viewgroup;
import android.widget.textview;

import java.util.arraylist;

public class recyclerviewactivity extends appcompatactivity {
    private recyclerview mrecyclerview1;
    private recyclerview mrecyclerview2;

    private arraylist mitems;

    @override
    protected void oncreate(@nullable bundle savedinstancestate) {
        super.oncreate(savedinstancestate);
        setcontentview(r.layout.recycler_view_activity);

        mitems = new arraylist<>();
        for (int i = 0; i < 100; i++) {
            mitems.add(i);
        }

        initrecyclerview1();
        initrecyclerview2();

        mrecyclerview1.addonscrolllistener(new recyclerview.onscrolllistener() {
            @override
            public void onscrolled(recyclerview recyclerview, int dx, int dy) {
                if (recyclerview.getscrollstate() != recyclerview.scroll_state_idle) {
                    mrecyclerview2.scrollby(dx, dy);
                }
            }
        });

        mrecyclerview2.addonscrolllistener(new recyclerview.onscrolllistener() {
            @override
            public void onscrolled(recyclerview recyclerview, int dx, int dy) {
                if (recyclerview.getscrollstate() != recyclerview.scroll_state_idle) {
                    mrecyclerview1.scrollby(dx, dy);
                }
            }
        });
    }

    private void initrecyclerview1() {
        mrecyclerview1 = findviewbyid(r.id.recycler_view_1);
        linearlayoutmanager layoutmanager = new linearlayoutmanager(this);
        layoutmanager.setorientation(linearlayoutmanager.vertical);
        mrecyclerview1.setlayoutmanager(layoutmanager);

        recyclerviewadapter madapter = new recyclerviewadapter(1);
        mrecyclerview1.setadapter(madapter);
    }

    private void initrecyclerview2() {
        mrecyclerview2 = findviewbyid(r.id.recycler_view_2);
        linearlayoutmanager layoutmanager = new linearlayoutmanager(this);
        layoutmanager.setorientation(linearlayoutmanager.vertical);
        mrecyclerview2.setlayoutmanager(layoutmanager);

        recyclerviewadapter madapter = new recyclerviewadapter(2);
        mrecyclerview2.setadapter(madapter);
    }

    private class recyclerviewadapter extends recyclerview.adapter {
        private int id;

        public recyclerviewadapter(int id) {
            this.id = id;
        }

        @nonnull
        @override
        public myvh oncreateviewholder(@nonnull viewgroup parent, int viewtype) {
            view view = layoutinflater.from(getapplicationcontext()).inflate(android.r.layout.simple_list_item_2, parent, false);
            return new myvh(view);
        }

        @override
        public void onbindviewholder(@nonnull myvh holder, int position) {
            holder.text1.settext("recyclerview:" + id);
            holder.text2.settext(mitems.get(position) + "");

            switch (id) {
                case 1:
                    holder.text1.setbackgroundcolor(color.red);
                    break;

                case 2:
                    holder.text1.setbackgroundcolor(color.blue);
                    break;
            }
        }

        @override
        public int getitemcount() {
            return mitems.size();
        }
    }

    private class myvh extends recyclerview.viewholder {
        public textview text1;
        public textview text2;

        public myvh(view itemview) {
            super(itemview);
            text1 = itemview.findviewbyid(android.r.id.text1);
            text1.settextcolor(color.white);
            text2 = itemview.findviewbyid(android.r.id.text2);
            text2.settextcolor(color.dkgray);
        }
    }
}

同时滑动其中一条,然后第二条也实时联合滚动

Android若干条并排RecyclerView滑动实时联动