[英]Disable scrolling in listview
我有一個列表視圖,根據一些邏輯,我想臨時禁用滾動。 view.setOnScrollListener(NULL); 沒有幫助我我想我需要寫一些代碼,有人可以給我一個文件或一些片段嗎?
謝謝
另一個沒有創建新自定義ListView的選項是將onTouchListener
附加到ListView,如果運動事件操作是ACTION_MOVE
,則在onTouch()
回調中返回true。
listView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
return (event.getAction() == MotionEvent.ACTION_MOVE);
}
});
在CustomListView中:
@Override
public boolean dispatchTouchEvent(MotionEvent ev){
if(ev.getAction()==MotionEvent.ACTION_MOVE)
return true;
return super.dispatchTouchEvent(ev);
}
然后ListView將對點擊作出反應,但不會改變滾動位置。
使用listview.setEnabled(false)禁用listview滾動
注意:這也會禁用行選擇
如果您有一個綁定到列表項的事件,則使用任何這些解決方案拖動列表仍將觸發該事件。 您希望使用以下方法來考慮用戶期望通過拖離所選項目來取消事件(改編自Pointer Null的答案):
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
final int actionMasked = ev.getActionMasked() & MotionEvent.ACTION_MASK;
if (actionMasked == MotionEvent.ACTION_DOWN) {
// Record the position the list the touch landed on
mPosition = pointToPosition((int) ev.getX(), (int) ev.getY());
return super.dispatchTouchEvent(ev);
}
if (actionMasked == MotionEvent.ACTION_MOVE) {
// Ignore move events
return true;
}
if (actionMasked == MotionEvent.ACTION_UP) {
// Check if we are still within the same view
if (pointToPosition((int) ev.getX(), (int) ev.getY()) == mPosition) {
super.dispatchTouchEvent(ev);
} else {
// Clear pressed state, cancel the action
setPressed(false);
invalidate();
return true;
}
}
return super.dispatchTouchEvent(ev);
}
提供完整的自定義視圖類: https : //gist.github.com/danosipov/6498490
制作您的CustomListView
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if(needToStop){
return false;}
return super.onInterceptTouchEvent(ev);
}
如果false
,孩子們將處理觸摸事件,請確保將if condition
為檢查是否需要滾動。
對我來說最好的答案是丹·奧西波夫的一個人。 我會像這樣改進它。 (觸發CANCEL動作事件而不是手動擦除按下狀態)。
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
final int actionMasked = ev.getActionMasked() & MotionEvent.ACTION_MASK;
if (actionMasked == MotionEvent.ACTION_DOWN) {
// Record the position the list the touch landed on
mPosition = pointToPosition((int) ev.getX(), (int) ev.getY());
return super.dispatchTouchEvent(ev);
}
if (actionMasked == MotionEvent.ACTION_MOVE) {
// Ignore move events
return true;
}
if (actionMasked == MotionEvent.ACTION_UP) {
// Check if we are still within the same view
if (pointToPosition((int) ev.getX(), (int) ev.getY()) != mPosition) {
// Clear pressed state, cancel the action
ev.setAction(MotionEvent.ACTION_CANCEL);
}
}
return super.dispatchTouchEvent(ev);
}
在列表視圖上編寫用於滑動刪除的代碼時,我想在檢測到滑動后阻止垂直滾動。 一旦滿足滑動刪除條件,我在ACTION_MOVE部分設置了一個布爾標志。 dispatchTouchEvent方法檢查該條件並阻止滾動工作。 在ACTION_UP中,我將標志設置為false以重新啟用滾動。
private float mY = Float.NaN;
private boolean mStopScroll = false;
@Override
public boolean onTouch(View view, MotionEvent event) {
if(!mStopScroll) {
mY = event.getY();
}
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE:
if(<condition that stops the vertical scroll>) {
mStopScroll = true;
}
break;
case MotionEvent.ACTION_UP:
mStopScroll = false;
// your code here
return true;
default:
}
return false;
}
@Override
public boolean dispatchTouchEvent(MotionEvent motionEvent) {
if(mStopScroll) {
motionEvent.setLocation(motionEvent.getX(), mY);
}
return super.dispatchTouchEvent(motionEvent);
}
我對Xamarin和MvvmCross用戶的回答很有意思。 主要概念與之前的帖子相同,因此主要步驟如下:
這里是helper類,允許在列表視圖中禁用滾動:
using System;
using Cirrious.MvvmCross.Binding.Droid.Views;
using Android.Content;
using Android.Util;
using Android.Views;
using Android.Database;
namespace MyProject.Android.Helpers
{
public class UnscrollableMvxListView
: MvxListView
{
private MyObserver _myObserver;
public UnscrollableMvxListView (Context context, IAttributeSet attrs, MvxAdapter adapter)
: base(context, attrs, adapter)
{
}
protected override void OnAttachedToWindow ()
{
base.OnAttachedToWindow ();
var dtso = new MyObserver(this);
_myObserver = dtso;
Adapter.RegisterDataSetObserver (dtso);
}
protected override void OnDetachedFromWindow ()
{
Log.Debug ("UnscrollableMvxListView", "OnDetachedFromWindow");
if (_myObserver != null) {
Adapter.UnregisterDataSetObserver (_myObserver);
_myObserver = null;
}
base.OnDetachedFromWindow ();
}
//Make List Unscrollable
private int _position;
public override bool DispatchTouchEvent (MotionEvent ev)
{
MotionEventActions actionMasked = ev.ActionMasked & MotionEventActions.Mask;
if (actionMasked == MotionEventActions.Down) {
// Record the position the list the touch landed on
_position = PointToPosition((int) ev.GetX (), (int) ev.GetY());
return base.DispatchTouchEvent(ev);
}
if (actionMasked == MotionEventActions.Move) {
// Ignore move events
return true;
}
if (actionMasked == MotionEventActions.Up) {
// Check if we are still within the same view
if (PointToPosition((int) ev.GetX(), (int) ev.GetY()) == _position) {
base.DispatchTouchEvent(ev);
} else {
// Clear pressed state, cancel the action
Pressed = false;
Invalidate();
return true;
}
}
return base.DispatchTouchEvent(ev);
}
//Make List Flat
public void JustifyListViewHeightBasedOnChildren () {
if (Adapter == null) {
return;
}
var vg = this as ViewGroup;
int totalHeight = 0;
for (int i = 0; i < Adapter.Count; i++) {
View listItem = Adapter.GetView(i, null, vg);
listItem.Measure(0, 0);
totalHeight += listItem.MeasuredHeight;
}
ViewGroup.LayoutParams par = LayoutParameters;
par.Height = totalHeight + (DividerHeight * (Adapter.Count - 1));
LayoutParameters = par;
RequestLayout();
}
}
internal class MyObserver
: DataSetObserver
{
private readonly UnscrollableMvxListView _unscrollableMvxListView;
public MyObserver (UnscrollableMvxListView lst)
{
_unscrollableMvxListView = lst;
}
public override void OnChanged() {
Log.Debug("UnscrollableMvxListView", "OnChanged");
base.OnChanged ();
_unscrollableMvxListView.JustifyListViewHeightBasedOnChildren ();
}
}
}
這是在http://danosipov.com/?p=604上指出的代碼Joe Blow(對OP帖子的評論)但是我在這里發布它以保護它以防鏈接被孤立:
public class ScrollDisabledListView extends ListView {
private int mPosition;
public ScrollDisabledListView(Context context) {
super(context);
}
public ScrollDisabledListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ScrollDisabledListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
final int actionMasked = ev.getActionMasked() & MotionEvent.ACTION_MASK;
if (actionMasked == MotionEvent.ACTION_DOWN) {
// Record the position the list the touch landed on
mPosition = pointToPosition((int) ev.getX(), (int) ev.getY());
return super.dispatchTouchEvent(ev);
}
if (actionMasked == MotionEvent.ACTION_MOVE) {
// Ignore move events
return true;
}
if (actionMasked == MotionEvent.ACTION_UP) {
// Check if we are still within the same view
if (pointToPosition((int) ev.getX(), (int) ev.getY()) == mPosition) {
super.dispatchTouchEvent(ev);
} else {
// Clear pressed state, cancel the action
setPressed(false);
invalidate();
return true;
}
}
return super.dispatchTouchEvent(ev);
}
}
將此ListView添加到布局時,請記住在其前面加上其包名,否則在嘗試對其進行充氣時將引發異常。
試試這個:
listViewObject.setTranscriptMode(0);
適合我。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.