gpt4 book ai didi

java - RecyclerView throw 和项目点击

转载 作者:行者123 更新时间:2023-12-01 08:51:58 25 4
gpt4 key购买 nike

我有一个带有项目网格的RecyclerView。单击某个项目后,它会突出显示。
我还希望当用户向右滑动时调用“下一个”方法,当用户向左滑动时调用“上一个”方法。
然而,两者不能协同工作,因为各自拦截对方的事件。
我如何让他们一起工作?

这是我的代码:

RecyclerView适配器

public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
myHolder = holder as MyView;
myHolder.mMainView.SetOnClickListener(this);
if (selected_position == position)
{
holder.ItemView.SetBackgroundColor(Color.LightGray);
}
else
{
holder.ItemView.SetBackgroundColor(Color.Transparent);
}
}

public void OnClick(View v)
{
int position = mRecyclerView.GetChildLayoutPosition((View)sender);

// Updating old as well as new positions
NotifyItemChanged(selected_position);
selected_position = position;
NotifyItemChanged(selected_position);
}

包含 RecyclerView 的 fragment

calendarRecyclerView.SetOnTouchListener(this);

public bool OnTouch(View v, MotionEvent e)
{
switch (e.Action)
{
case MotionEventActions.Down:
x1 = e.GetX();
break;
case MotionEventActions.Up:
x2 = e.GetX();
float deltaX = x2 - x1;
if (Math.Abs(deltaX) > MIN_DISTANCE)
{
// Left to Right swipe action
if (x2 > x1)
{
NextMonth();
}

// Right to left swipe action
else
{
PreviousMonth();
}
}


break;
}
return false;
}

因为我将 return false 放入 OnTouch 事件中,所以会触发该项目的单击事件。但是,MouseDown 事件不会在 OnTouch 中触发,从而阻止向后滑动检测(因为 x1 始终为 0)。

最佳答案

OnTouch event gets called on the first click, and the OnClick gets called only on the second click

因为MotionEventActions.DownOnClick冲突。作为解决方法,我建议您在 MotionEventActions.Down 事件中更改背景颜色。

  1. 创建您自己的点击监听器
  2. 当您放下元素时调用监听器。
  3. 监听器将回调到 MainActivity 以通知项目发生更改。
  4. 同时将调用触摸事件。

我已经在viewholder中设置了OnTouchListener:

public class MyViewHolder:RecyclerView.ViewHolder,IOnTouchListener
{
private TextView textView;
private MyItemClickListener mListener;
private Context myContext;
float x1 = 0;
float x2 = 0;
public TextView TextView { get { return textView; } }
public MyViewHolder(View v, MyItemClickListener mItemClickListener) : base(v)
{
textView = v.FindViewById<TextView>(Resource.Id.itemText);
mListener = mItemClickListener;
v.SetOnTouchListener(this);
}

public MyViewHolder(View v, MyItemClickListener mItemClickListener, Context myContext) : this(v, mItemClickListener)
{
this.myContext = myContext;
}
public bool OnTouch(View v, MotionEvent e)
{

switch (e.Action)
{
case MotionEventActions.Down:
x1 = e.GetX();
if (mListener != null)
{
mListener.OnItemClick(v, Position);
}
break;
case MotionEventActions.Up:
x2 = e.GetX();
float deltaX = x2 - x1;
if (Math.Abs(deltaX) > 5)
{
// Left to Right swipe action
if (x2 > x1)
{
NextMonth(v);
}

// Right to left swipe action
else
{
PreviousMonth(v);
}
}
break;
}
return true;
}

public Boolean NextMonth(View v)
{
Toast.MakeText(myContext, "NextMonth called", ToastLength.Short).Show();
return true;
}

public Boolean PreviousMonth(View v)
{
Toast.MakeText(myContext, "PreviousMonth called", ToastLength.Short).Show();
return true;
}
}

定义点击界面:

public interface MyItemClickListener
{
void OnItemClick(View view, int postion);
}

MainActivity中设置点击回调改变背景颜色:

  public class MainActivity : Activity,MyItemClickListener
{
RecyclerView mRecyclerView;
RecyclerView.LayoutManager mLayoutManager;
CustomAdapter mAdapter;
string[] dataSet;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
InitDataSet();
SetContentView(Resource.Layout.Main);

mLayoutManager = new LinearLayoutManager(this);

mRecyclerView = FindViewById<RecyclerView>(Resource.Id.recyclerView);
mRecyclerView.SetLayoutManager(mLayoutManager);
mAdapter = new CustomAdapter(dataSet,this);
mAdapter.setOnItemClickListener(this);
mRecyclerView.SetAdapter(mAdapter);

//mRecyclerView.SetOnTouchListener(this);
}

public void InitDataSet()
{
dataSet = new string[60];
for (int i = 0; i < 60; i++)
{
dataSet[i] = "This is element #" + i;
}
}

public void OnItemClick(View view, int postion)
{
mAdapter.NotifyItemChanged(CustomAdapter.selected_position);
CustomAdapter.selected_position = postion;
mAdapter.NotifyItemChanged(postion);
}
}
}

注意:保持手指快速移动,如果速度足够慢,则不会调用 MotionEventActions.Down

Github souce code

屏幕截图:

enter image description here

关于java - RecyclerView throw 和项目点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42332072/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com