Two RecyclerViews desynchronizes during programm scrolling

时间:2018-02-15 12:31:20

标签: android scroll android-recyclerview synchronization programmatically

I have two RecyclerViews on my screen. And I need to scroll both at the same time on the same distance programmatically. But if I do it fast - the RecyclerViews desynchronizes.

In this source code you should fast click on button in the right top and will see the result.

enter image description here

This is Activity, where button listener for scrolling lists created:

public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView1;
private RecyclerView recyclerView2;
private Adapter adapter1;
private Adapter adapter2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    recyclerView1 = findViewById(R.id.recycler_view1);
    recyclerView2 = findViewById(R.id.recycler_view2);

    adapter1 = new Adapter();
    recyclerView1.setLayoutManager(new LinearLayoutManager(this));
    recyclerView1.setAdapter(adapter1);

    adapter2 = new Adapter();
    recyclerView2.setLayoutManager(new LinearLayoutManager(this));
    recyclerView2.setAdapter(adapter2);

    findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            recyclerView1.smoothScrollBy(0, 400);
            recyclerView2.smoothScrollBy(0, 400);
        }
    });
}
}

Adapter for lists:

public class Adapter extends RecyclerView.Adapter<Adapter.Holder>{

private boolean changeHeight = false;

@Override
public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
    return new Holder(view);
}

@Override
public void onBindViewHolder(Holder holder, int position) {
    holder.setContent(String.valueOf(position), position % 2 == 0 ? R.color.colorPrimary : R.color.colorAccent);
    if (changeHeight && position == 3) holder.changeHeight();
}

@Override
public int getItemCount() {
    return 100;
}

class Holder extends RecyclerView.ViewHolder {

    private View view;
    private TextView textView;

    public Holder(View itemView) {
        super(itemView);
        view = itemView;
        textView = itemView.findViewById(R.id.text_view);
    }

    public void setContent(String text, int colorRes) {
        textView.setText(text);
        textView.setBackgroundResource(colorRes);
    }

    public void changeHeight() {
        ViewGroup.LayoutParams params = view.getLayoutParams();
        params.height = view.getHeight() * 2;
        view.setLayoutParams(params);
    }
}
}

This is activity_main.xml layout for Activity

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_view1"
    android:layout_width="0dp"
    android:layout_weight="0.5"
    android:layout_height="match_parent"/>

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_view2"
    android:layout_width="0dp"
    android:layout_weight="0.5"
    android:layout_height="match_parent"/>

<Button
    android:id="@+id/button"
    android:layout_width="100dp"
    android:layout_height="100dp"/>

</LinearLayout>

Item for RecyclerView item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="100dp">

<TextView
    android:id="@+id/text_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"/>

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

原油解决方案是将第一个回收商的卷轴复制到第二个回收商:

    recyclerView1.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            recyclerView2.scrollBy(dx, dy);
        }
    });

然后继续进行smoothScroll recyclerView1