Android 如何禁止单击RecyclerView项
我使用浮动动作按钮。我想在按下FAB按钮时禁止点击Recyclerview项目。我尝试了这个方法,但没有工作Android 如何禁止单击RecyclerView项,android,android-recyclerview,floating-action-button,Android,Android Recyclerview,Floating Action Button,我使用浮动动作按钮。我想在按下FAB按钮时禁止点击Recyclerview项目。我尝试了这个方法,但没有工作setClickable(true) 我的布局 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:fab="http://schemas.android.com/t
setClickable(true)代码>
我的布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:fab="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#fff"
tools:context="com.hartwintech.socialchat.activity.IconTabsActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
>
</android.support.v7.widget.RecyclerView>
<com.github.clans.fab.FloatingActionMenu
android:id="@+id/floatmenu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="60dp"
android:layout_marginRight="16dp"
fab:fab_showAnimation="@anim/show_from_bottom"
fab:fab_hideAnimation="@anim/hide_to_bottom"
fab:menu_labels_style="@style/MenuLabelsStyle"
fab:menu_shadowColor="#444"
fab:menu_colorNormal="#FFB805"
fab:menu_colorPressed="#F2AB00"
fab:menu_colorRipple="#D99200"/>
</RelativeLayout>
您可以向适配器添加一个简单的布尔值,如下所示:
public boolean isClickable = true;
并将其设置在晶圆厂中单击:
mAdapter.isClickable = true/false;
在适配器的OnClickListener中,仅当可单击时才执行操作:
public void onClick(View view) {
if(!isClickable)
return;
// do your click stuff
}
您需要将click listener设置为everyFloatingActionButton
请看上的这个问题,比约恩·凯切尔的回答对我有帮助。正如他所说,我只是添加了布尔值。当我点击fab菜单时,布尔值被激活。然后必须在mrecyclerview.addonimtouchlistener上编写条件
public Boolean fabClick = false;
floatMenu.setOnMenuToggleListener(new FloatingActionMenu.OnMenuToggleListener() {
@Override
public void onMenuToggle(boolean opened) {
if (opened) {
final int color = R.color.transp;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
fabClick = true;
mrecyclerview.setClickable(false);
mrecyclerview.setEnabled(false);
mrecyclerview.setForeground(new ColorDrawable(ContextCompat.getColor(getContext(), color)));
}
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
fabClick = false;
mrecyclerview.setClickable(true);
mrecyclerview.setEnabled(true);
mrecyclerview.setForeground(null);
}
}
}
});
要禁用RecyclerView,请执行以下步骤:
一,。将以下视图添加到布局文件中
<View
android:id="@+id/viewDisableLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#40000000"
android:clickable="true"
android:focusable="true"
android:visibility="gone"/>
二,。设置视图可见性`View.VISIBLE当您想禁用RecyclerView else时,您可以简单地使用递归来禁用/启用对视图的单击
public static void setClickable(View view, boolean clickable) {
if (view != null) {
if (view instanceof ViewGroup) {
ViewGroup viewGroup = (ViewGroup) view;
for (int i = 0; i < viewGroup.getChildCount(); i++) {
setClickable(viewGroup.getChildAt(i), clickable);
}
}
view.setClickable(clickable);
}
}
publicstaticvoidsetclickable(视图,布尔值可点击){
如果(视图!=null){
if(视图组的视图实例){
ViewGroup ViewGroup=(ViewGroup)视图;
对于(int i=0;i
使用RecyclerView.OnItemTouchListener的工作解决方案:
@SuppressLint("ClickableViewAccessibility")
@BindingAdapter("itemsClickable")
fun setRecyclerViewClickable(view: RecyclerView, clickable: Boolean) {
view.isEnabled = clickable
if (!clickable) {
val itemTouchListener = object : RecyclerView.OnItemTouchListener {
override fun onTouchEvent(rv: RecyclerView?, e: MotionEvent?) {
}
override fun onInterceptTouchEvent(rv: RecyclerView?, e: MotionEvent?): Boolean {
return rv?.isEnabled == false
}
override fun onRequestDisallowInterceptTouchEvent(disallowIntercept: Boolean) {
}
}
view.addOnItemTouchListener(itemTouchListener)
view.tag = itemTouchListener
} else {
(view.tag as? RecyclerView.OnItemTouchListener)?.let {
view.requestDisallowInterceptTouchEvent(true)
view.removeOnItemTouchListener(it)
}
}
}
您可以禁用触摸recyclerview
recyclerView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
在xml文件中,将FloatingActionMenu的布局宽度和布局高度设置为匹配父项,并将clickable设置为false:
android:layout_width="match_parent "
android:layout_height="match_parent "
android:clickable="false"
在java类中
floatMenu.setOnMenuToggleListener(new FloatingActionMenu.OnMenuToggleListener() {
@Override
public void onMenuToggle(boolean opened) {
if (opened) {
floatMenu.setClickable(true);
} else {
floatMenu.setClickable(false);
}
}
});
这应该行。我用非常简单的逻辑解决了这个问题。这将防止双击回收视图的单个项目和多个项目
在活动中声明一个变量
private long mLastClickTime = 0;
然后在任何OnClickListener
中使用
@Override
public void onClick(View v) {
if(SystemClock.elapsedRealtime() - mLastClickTime < 1000){//You can reclick after 1 second
return;//Before 1 seconds from first click this onclick will return from here
}
mLastClickTime = SystemClock.elapsedRealtime();
//Do stuff here
}
@覆盖
公共void onClick(视图v){
如果(SystemClock.elapsedRealtime()-mLastClickTime<1000){//您可以在1秒后重新单击
return;//在第一次单击1秒钟之前,此单击将从此处返回
}
mLastClickTime=SystemClock.elapsedRealtime();
//在这里做事
}
实际上,当我搜索防止双击按钮时,我在StackoverFlow中找到了这个解决方案。我写这一行是为了确认实际答案是由某人发布的(不幸的是,我无法找到该答案来链接他/她的答案。)
希望这能解决您的问题。:)您想禁用还是启用<代码>可设置可点击(真)代码>将使其可单击我想禁用trymrecyclerview.setEnabled(false)代码>试过了兄弟。它不是禁用的。你可以发布你的布局吗?问题显示与浮动操作按钮相关。但是我想禁用在浮动操作菜单上单击recyclerview,然后您可以将视图放置在recyclerview
上,并将其打开,不要忘记在重叠视图上添加setClickable=“true”
,我已经发布了答案。谢谢你的支持。打得好@Rajan,你做了有史以来最好的肮脏的事情,但它奏效了。很好!我自己尝试过,但缺少了“安卓:clickable=“true”安卓:focusable=“true”部分!谢谢你,天才!:)
private long mLastClickTime = 0;
@Override
public void onClick(View v) {
if(SystemClock.elapsedRealtime() - mLastClickTime < 1000){//You can reclick after 1 second
return;//Before 1 seconds from first click this onclick will return from here
}
mLastClickTime = SystemClock.elapsedRealtime();
//Do stuff here
}