Skip to content

Instantly share code, notes, and snippets.

@tank777
Created June 15, 2017 16:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tank777/739eafaf0708ffe1682f4409e1a0d866 to your computer and use it in GitHub Desktop.
Save tank777/739eafaf0708ffe1682f4409e1a0d866 to your computer and use it in GitHub Desktop.
stop recyclerview scroll
package com.bgt.stopscrollrecycleview;
import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.util.AttributeSet;
/**
* Created by Bhavesh on 15-06-2017.
*/
public class CustomLinearLayoutManager extends LinearLayoutManager {
private boolean isScrollEnabled = true;
public CustomLinearLayoutManager(Context context) {
super(context);
}
public CustomLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
super(context, orientation, reverseLayout);
}
public CustomLinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
public void setScrollEnabled(boolean flag) {
this.isScrollEnabled = flag;
}
@Override
public boolean canScrollHorizontally() {
//Similarly you can customize "canScrollHorizontally()" for managing horizontal scroll
return isScrollEnabled && super.canScrollHorizontally();
}
}
package com.bgt.stopscrollrecycleview;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
TestAdapter testAdapter;
CustomLinearLayoutManager lm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.rv_test);
lm = new CustomLinearLayoutManager(getApplicationContext(), LinearLayoutManager.HORIZONTAL,false);
lm.setScrollEnabled(false);
recyclerView.setLayoutManager(lm);
testAdapter = new TestAdapter();
recyclerView.setAdapter(testAdapter);
recyclerView.stopScroll();
}
public void mShowMore(View view) {
lm.setScrollEnabled(true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment