我的RecyclerView里面有ViewPagers和按钮,所以我无法禁用滚动或禁用所有触摸。
我只禁用垂直滚动:
recyclerView.setLayoutManager(new LinearLayoutManager(this) {
@Override
public boolean canScrollVertically() {
return false;
}
});
问题是当我尝试使用代码滚动recyclerView时:
recyclerView.smoothScrollBy(0, 500);
android中的smoothScrollBy定义为:
public void smoothScrollBy(int dx, int dy, Interpolator interpolator) {
if (mLayout == null) {
Log.e(TAG, "Cannot smooth scroll without a LayoutManager set. "
+ "Call setLayoutManager with a non-null argument.");
return;
}
if (mLayoutFrozen) {
return;
}
if (!mLayout.canScrollHorizontally()) {
dx = 0;
}
if (!mLayout.canScrollVertically()) { // <--- problem
dy = 0;
}
if (dx != 0 || dy != 0) {
mViewFlinger.smoothScrollBy(dx, dy, interpolator);
}
}
当我调用scrollBy或smoothScrollBy时没有任何反应,因为canScrollVertically为false。
另请注意,如果我将其设置为true,则滚动,然后尝试false,scrollView抖动并且它不会完全滚动:
layoutManager.canScrollVertically = true;
recyclerView.smoothScrollBy(0, 500);
layoutManager.canScrollVertically = false; // -> when this line is set, the smoothScrolling stops immediately
我试图阻止用户垂直滚动,因为我试图处理代码中的垂直滚动。用户仍然可以与RecyclerView中的其他元素进行交互,并且能够在RecyclerView中水平滚动。
我可以尝试什么?
答案 0 :(得分:0)
要创建Horizontal RecyclerView,请使用以下代码:
LinearLayoutManager layoutManager = new
LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(layoutManager);
答案 1 :(得分:0)
您可以使用GestureDetector来实现此目的,请查看下面的代码。方法onScroll()
应该返回true,这是其中的重要部分。
public class MainActivity extends AppCompatActivity implements GestureDetector.OnGestureListener {
GestureDetector gestureDetector;
RecyclerView recyclerView;
Random random = (new Random());
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String s[] = {"One","One2","One3","One4","One5","One6","One7","One8","One9","One10","One11","One12","One13","One14","One","One2","One3","One4","One5","One6","One7","One8","One9","One10","One11","One12","One13","One14"};
recyclerView = (RecyclerView) findViewById(R.id.recycler);
recyclerView.setAdapter(new MyAdapter(s));
gestureDetector = new GestureDetector(this, this);
recyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
return gestureDetector.onTouchEvent(e);
// return false;
}
@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
});
}
public void scrollClicked(View v) {
recyclerView.smoothScrollBy(0, 100);
}
@Override
public boolean onDown(MotionEvent motionEvent) {
return false;
}
@Override
public void onShowPress(MotionEvent motionEvent) {
}
@Override
public boolean onSingleTapUp(MotionEvent motionEvent) {
return false;
}
@Override
public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
return true;
}
@Override
public void onLongPress(MotionEvent motionEvent) {
}
@Override
public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
return false;
}
public static class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private String[] mDataset;
// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public static class ViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView mTextView;
public ViewHolder(View v) {
super(v);
mTextView = (TextView) v;
}
}
// Provide a suitable constructor (depends on the kind of dataset)
public MyAdapter(String[] myDataset) {
mDataset = myDataset;
}
// Create new views (invoked by the layout manager)
@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.my_text_view, parent, false);
ViewHolder vh = new ViewHolder(v);
return vh;
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
holder.mTextView.setText(mDataset[position]);
}
// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return mDataset.length;
}
}
}