我有一个简单的方法RecyclerView
,当您进入某行时LongClick
,RecyclerView
我会LinearLayout
在该行上显示隐藏的内容。为此,public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
我正在使用以下
LinearLayout REACTION_LAYOUT = RecyclerItem.FindViewById<LinearLayout>(Resource.Id.reaction_layout);
RecyclerItem.LongClick += (sender, e) => { OnLongClick(REACTION_LAYOUT); };
REACTION_LAYOUT
是我使用以下内容显示的布局
private void OnLongClick(LinearLayout _ReactionLayout)
{
try
{
_ReactionLayout.Visibility = ViewStates.Visible;
}
catch (Exception X)
{
Log.Info("1022585", "CLICK (ERROR) : " + X.Message);
}
}
这按预期工作,现在我要隐藏的是,REACTION_LAYOUT
当用户触摸行外的任何位置时,这意味着在RecyclerView中的其他行或其他任何位置
我怎么做?
我尝试使用的方法是检测parent
适配器内部的触摸OnCreateViewHolder
,如下所示
parent.Click += (sender, e) => { OnParentClick(REACTION_LAYOUT); };
这可以工作,但是会禁用“回收者”视图上的滚动。
实现此目标的最佳方法是什么?
编辑:活动RecyclerView是以下
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/main_message_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/colorPrimary"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:orientation="vertical">
<include layout="@layout/toolbar_messages"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="4.5dp"
android:layout_marginTop="3dp"
android:background="@drawable/shadow"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="fill_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvMessages"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:layout_alignParentTop="true"
android:paddingBottom="70dp"/> .......
编辑2:
还尝试设置OnTouchLisener,但仍然没有运气
public class _OnRecylerTouch_Listener : Java.Lang.Object, View.IOnTouchListener
{
private LinearLayout _L;
public _OnRecylerTouch_Listener(LinearLayout L)
{
_L = L;
}
public bool OnTouch(View v, MotionEvent e)
{
_L.Visibility = ViewStates.Gone;
return true;
}
}
上面是非常有线的代码。从我得到的结果来看,如果您的解决方案工作正常,但是在这种情况下嵌套滚动不起作用。
public class _OnRecylerTouch_Listener : Java.Lang.Object, View.IOnTouchListener
{
private LinearLayout _L;
public _OnRecylerTouch_Listener(LinearLayout L)
{
_L = L;
}
public bool OnTouch(View v, MotionEvent e)
{
if (_L.Visibility == ViewStates.VISIBLE) {
_L.Visibility = ViewStates.Gone;
return true;
}
return false;
}
}
接下来将您更改onLongClickListener
为下一个。
private void OnLongClick(LinearLayout _ReactionLayout)
{
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
@Override
public void run()
{
try {
_ReactionLayout.Visibility = ViewStates.Visible;
}
catch (Exception X)
{
Log.Info("1022585", "CLICK (ERROR) : " + X.Message);
}
}, 100);
}
}
而且您的问题应该得到解决。
说明
您所需要的就是不拦截触摸事件。内部滚动应该消耗哪些内容。并且onLongClickListener
应采取措施以在onTouch
正常工作后触发可见性更改。这不是最佳解决方案,但这是您的问题,因此您可以找出原因。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句