Skip to content

Instantly share code, notes, and snippets.

@yanzm
Created January 26, 2017 03:03
Show Gist options
  • Save yanzm/ff0feb74e1e796db56115aa0356151ca to your computer and use it in GitHub Desktop.
Save yanzm/ff0feb74e1e796db56115aa0356151ca to your computer and use it in GitHub Desktop.
Customize GridLayoutManager sample
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final RecyclerView recyclerView = new RecyclerView(this);
setContentView(recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new CustomGridLayoutManager(this, 6));
recyclerView.setAdapter(new MyAdapter());
}
public static class ViewHolder extends RecyclerView.ViewHolder {
@NonNull
static ViewHolder create(ViewGroup parent) {
final int height = (int) (100 * parent.getResources().getDisplayMetrics().density);
final TextView textView = new TextView(parent.getContext());
textView.setGravity(Gravity.CENTER);
textView.setBackgroundResource(R.drawable.border);
textView.setLayoutParams(new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, height));
return new ViewHolder(textView);
}
final TextView textView;
private ViewHolder(TextView itemView) {
super(itemView);
textView = itemView;
}
}
public static class MyAdapter extends RecyclerView.Adapter<ViewHolder> {
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return ViewHolder.create(parent);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.textView.setText(String.valueOf(position));
}
@Override
public int getItemCount() {
return 50;
}
}
public static class CustomGridLayoutManager extends GridLayoutManager {
private OrientationHelper orientationHelperForHorizontal;
private int offset;
public CustomGridLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
public CustomGridLayoutManager(Context context, int spanCount) {
super(context, spanCount);
}
public CustomGridLayoutManager(Context context, int spanCount, int orientation, boolean reverseLayout) {
super(context, spanCount, orientation, reverseLayout);
}
private int getExtra() {
return super.getWidth();
}
@Override
public int getWidth() {
// 魔改造 その1
return super.getWidth() + getExtra();
}
@Override
public boolean canScrollHorizontally() {
return true;
}
@Override
public int scrollHorizontallyBy(int dx, RecyclerView.Recycler recycler, RecyclerView.State state) {
if (getChildCount() == 0 || dx == 0) {
return 0;
}
ensureLayoutState();
final int layoutDirection = dx > 0 ? 1 : -1;
final int absDy = Math.abs(dx);
final int consumed = calculateHorizontalOffsetForScroll(layoutDirection);
if (consumed < 0) {
return 0;
}
final int scrolled = absDy > consumed ? layoutDirection * consumed : dx;
orientationHelperForHorizontal.offsetChildren(-scrolled);
return scrolled;
}
private int calculateHorizontalOffsetForScroll(int layoutDirection) {
int scrollingOffset;
if (layoutDirection == 1) {
scrollingOffset = getExtra() + offset;
} else {
scrollingOffset = -offset;
}
return scrollingOffset;
}
void ensureLayoutState() {
if (orientationHelperForHorizontal == null) {
orientationHelperForHorizontal = OrientationHelper.createOrientationHelper(this, OrientationHelper.HORIZONTAL);
}
}
@Override
public void layoutDecoratedWithMargins(View child, int left, int top, int right, int bottom) {
// 魔改造 その2
super.layoutDecoratedWithMargins(child, left + offset, top, right + offset, bottom);
}
@Override
public void offsetChildrenHorizontal(int dx) {
super.offsetChildrenHorizontal(dx);
this.offset += dx;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment