Skip to content

Instantly share code, notes, and snippets.

@proevan
Last active March 21, 2016 10:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save proevan/c702b3c43b6affde63af to your computer and use it in GitHub Desktop.
Save proevan/c702b3c43b6affde63af to your computer and use it in GitHub Desktop.
Synchronized Scrolling RecyclerViewPager
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import com.lsjwzh.widget.recyclerviewpager.RecyclerViewPager;
public class SingleFlingPagerActivity extends Activity {
private static final int LIST_ITEM_WIDTH = 210;
private static final int LIST_2_ITEM_WIDTH = 70;
private static final int LIST_ITEM_PADDING = 10;
private static final float LIST_ITEM_TOTAL_WIDTH_RATIO =
(float) (LIST_2_ITEM_WIDTH + 2 * LIST_ITEM_PADDING) / (float) (LIST_ITEM_WIDTH + 2 * LIST_ITEM_PADDING);
protected RecyclerViewPager mRecyclerView;
protected RecyclerViewPager mRecyclerView2;
private boolean mIsListLeadScrool = true;
private boolean mIsList2LeadScrool = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.demo_single_fling_pager);
initViewPager();
initViewPager2();
}
protected void initViewPager() {
mRecyclerView = (RecyclerViewPager)findViewById(R.id.viewpager);
LinearLayoutManager layout = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL,
false);
mRecyclerView.setLayoutManager(layout);
mRecyclerView.setAdapter(new LayoutAdapter(this, mRecyclerView));
mRecyclerView.setHasFixedSize(true);
Display display = getWindowManager().getDefaultDisplay();
int screenWidth = display.getWidth();
int listItemWidthInDp = LIST_ITEM_WIDTH;
int listItemPaddingInDp = LIST_ITEM_PADDING;
int padding = Math.round((screenWidth - convertDpToPixel(listItemWidthInDp + 2 * listItemPaddingInDp, this)) / 2);
mRecyclerView.setPadding(padding, 0, padding, 0);
mRecyclerView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
mIsListLeadScrool = true;
mIsList2LeadScrool = false;
mRecyclerView2.stopScroll();
return false;
}
});
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int scrollState) {
}
@Override
public void onScrolled(RecyclerView recyclerView, int i, int i2) {
if (mIsListLeadScrool) {
mRecyclerView2.scrollBy(Math.round(LIST_ITEM_TOTAL_WIDTH_RATIO * i), Math.round(LIST_ITEM_TOTAL_WIDTH_RATIO * i2));
}
}
});
mRecyclerView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
}
});
}
protected void initViewPager2() {
mRecyclerView2 = (RecyclerViewPager)findViewById(R.id.viewpager2);
LinearLayoutManager layout = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL,
false);
mRecyclerView2.setLayoutManager(layout);
mRecyclerView2.setAdapter(new LayoutAdapter2(this, mRecyclerView2));
mRecyclerView2.setHasFixedSize(true);
Display display = getWindowManager().getDefaultDisplay();
int screenWidth = display.getWidth();
int listItemWidthInDp = LIST_2_ITEM_WIDTH;
int listItemPaddingInDp = LIST_ITEM_PADDING;
int padding = Math.round((screenWidth - convertDpToPixel(listItemWidthInDp + 2 * listItemPaddingInDp, this)) / 2);
mRecyclerView2.setPadding(padding, 0, padding, 0);
mRecyclerView2.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
mIsList2LeadScrool = true;
mIsListLeadScrool = false;
mRecyclerView.stopScroll();
return false;
}
});
mRecyclerView2.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int scrollState) {
}
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
@Override
public void onScrolled(RecyclerView recyclerView, int i, int i2) {
if (mIsList2LeadScrool) {
mRecyclerView.scrollBy(Math.round(i / LIST_ITEM_TOTAL_WIDTH_RATIO), Math.round(i2 / LIST_ITEM_TOTAL_WIDTH_RATIO));
}
}
});
mRecyclerView2.addOnPageChangedListener(new RecyclerViewPager.OnPageChangedListener() {
@Override
public void OnPageChanged(int oldPosition, int newPosition) {
}
});
mRecyclerView2.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
}
});
}
public static float convertDpToPixel(float dp, Context context){
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
float px = dp * (metrics.densityDpi / 160f);
return px;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment