gpt4 book ai didi

android - 启用/禁用和可点击属性在 RecyclerView 适配器中不起作用

转载 作者:太空宇宙 更新时间:2023-11-03 12:03:11 25 4
gpt4 key购买 nike

实际上,我想启用/禁用 RecyclerView 的特定行,因此在 Adapter 类中,我在 onBindViewHolder 函数中实现了我的逻辑,但是当我应用 Enabled 属性时,它不会对我的 holder 项目产生任何影响。那么谁能告诉我它是如何完成的。

注意:

  1. 我知道那里有很多类似的问题,但我尝试了其中的大部分,但都没有用。

  2. 可见性属性正在处理我的 holder Item

代码:

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
RegsiteredCarDisplayItem displayItem = displayItems.get(position);

holder.modelName.setText(displayItem.getMyOwnedCar().getCarModelName()); if(selected_usersList.contains(displayItems.get(position).getMyOwnedCar()))
holder.childView.setBackgroundColor(ContextCompat.getColor(context, R.color.buttonbackground));

else
holder.childView.setBackgroundColor(ContextCompat.getColor(context, R.color.textViewBackground));

if (displayItems.get(position).getVisible()){
Log.d("AIAITRUE", String.valueOf(displayItems.get(position)));
holder.childView.setEnabled(true);
holder.childView.setClickable(true);

}else{
// here i want to Disable my Holder View but the below line is not working, but i use another property like alpha instaed of Enabled property than property(refrence alpha) is working
holder.childView.setEnabled(false);
}

}

XML:

 <?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:foreground="?android:attr/selectableItemBackground"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
card_view:cardElevation="0dp"
card_view:cardUseCompatPadding="true"
card_view:cardPreventCornerOverlap="false"

>
<RelativeLayout
android:id="@+id/parentView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="false">
<RelativeLayout
android:id="@+id/relativelayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/padding"
android:layout_marginBottom="@dimen/padding"
android:orientation="vertical"
android:clickable="false"
tools:ignore="UselessParent">


<ImageView
android:id="@+id/icons"
android:layout_width="50dp"
android:layout_height="30dp"


android:layout_alignBottom="@id/linearlayout"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_centerHorizontal="false"
android:layout_marginEnd="@dimen/margin_five"
android:layout_marginRight="@dimen/margin_five"
android:layout_marginBottom="-15dp"
android:background="@color/colorAccent"
android:clickable="false"
android:adjustViewBounds="true"
tools:ignore="RtlHardcoded"/>
<LinearLayout
android:id="@+id/linearlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/icons"
android:layout_marginBottom="@dimen/padding"
android:orientation="horizontal"
android:clickable="false"
tools:ignore="RtlHardcoded">
<TextView
android:id="@+id/modelNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/padding"
android:gravity="start"
android:clickable="false"
android:textSize="@dimen/text_size"
android:drawablePadding="@dimen/drawable_padding"
android:drawableTint="@color/buttonbackground"
tools:ignore="RtlSymmetry"
tools:targetApi="m"
android:textColor="@color/colorAccent"/>
</LinearLayout>


<TextView
android:id="@+id/registration_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/linearlayout"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="@dimen/left_margin_registration_number"
android:layout_marginStart="@dimen/left_margin_registration_number"
android:clickable="false"
android:textColor="@color/text_color"/>


<ImageView
android:id="@+id/next_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/linearlayout"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_centerHorizontal="false"
android:layout_marginBottom="-10dp"
android:clickable="false"
android:foregroundGravity="right"
android:tint="@color/buttonbackground"
app:srcCompat="@drawable/ic_next"/>



</RelativeLayout>


</RelativeLayout>


</android.support.v7.widget.CardView>

VieHolder 类:

class ViewHolder extends RecyclerView.ViewHolder{
TextView carName,modelName,registrationNumber; View cardView;
RelativeLayout childView;ImageView imageView; ImageView imageicon;
public ViewHolder(View itemView) {
super(itemView);
Typeface custom_font = Typeface.createFromAsset(context.getAssets(), "fonts/helvetica-neue-ce-55-roman.ttf");
Typeface custom_fonts = Typeface.createFromAsset(context.getAssets(), "fonts/helvetica-neue-ce-35-thin.ttf");
cardView = itemView.findViewById(R.id.cardView);
modelName=(TextView)itemView.findViewById(R.id.modelNumber);
registrationNumber=(TextView)itemView.findViewById(R.id.registration_number);
childView =(RelativeLayout)itemView.findViewById(R.id.parentView);
imageView=(ImageView) itemView.findViewById(R.id.next_icon);
imageicon=(ImageView)itemView.findViewById(R.id.icons);
modelName.setTypeface(custom_font);
registrationNumber.setTypeface(custom_fonts);


}


}

最佳答案

检查您是否确实点击了您认为正在点击的 View 。您可能会覆盖您试图用另一个 View 抑制点击的 View ,因此,即使您禁用了您感兴趣的 View ,您也会看到来自覆盖 View 的点击。您可以检查传递给点击处理程序的 View 的 ID 来检查这一点。

如果是这种情况,那么设置其他属性(如背景颜色和 alpha)仍然有效。

关于android - 启用/禁用和可点击属性在 RecyclerView 适配器中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46073385/

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