我有一个水平滚动的回收者视图。 recyclerview中的每个项目都有edittext。当我点击edittext时,recyclerview滚动到结束。我怎么能禁止滚动? 谢谢!
答案 0 :(得分:0)
您可以在EditText上设置焦点上的事件侦听器,以禁用焦点上的滚动,并在焦点失焦时再次启用它。
类似的东西:
var formData = new FormData($('#form-id'));
params = $('#form-id').serializeArray();
$.each(params, function(i, val) {
formData.append(val.name, val.value);
});
其中disabler是此类的一个实例(来自this answer):
edit_Text.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
recyclerview.addOnItemTouchListener(disabler);
}else {
recyclerview.removeOnItemTouchListener(disabler);
}
}
});
答案 1 :(得分:0)
这可以通过使用两个侦听器来解决。第一个用户在触摸输入字段时禁用滚动:
private View.OnFocusChangeListener onFocusChangeListener = new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus){
manager.setScrollEnabled(false);
}
}
};
将onFocusChangeListener设置为onBindViewHolder()方法中RecyclerView的每个项目的每个输入字段。第二个侦听器可以在用户触摸时滚动:
private RecyclerView.OnItemTouchListener onItemTouchListener = new RecyclerView.OnItemTouchListener() {
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
RecyclerView.LayoutManager manager = rv.getLayoutManager();
if (manager instanceof CustomLinearLayoutManager){
((CustomLinearLayoutManager)manager).setScrollEnabled(true);
}
return false;
}
@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
};
此答案中描述了允许您启用或禁用滚动的自定义LinearLayoutManager的代码:How to disable RecyclerView scrolling?
诀窍是覆盖一种方法:
@Override
public boolean canScrollHorizontally() {
return isScrollEnabled && super.canScrollHorizontally();
}
如果要启用/禁用滚动行为,请设置变量isScrollEnabled:
public void setScrollEnabled(boolean flag) {
this.isScrollEnabled = flag;
}