单击任何按钮后,RecyclerView中的所有其他按钮均被禁用

时间:2018-09-27 06:27:42

标签: android button android-recyclerview

enter image description here

请参考上图

public class complaintAdapter extends 
RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context context;
String hello, status, type;
private LayoutInflater inflater;
public String service, typeid;
String result_reso;
List<DataComplaint> data= Collections.emptyList();
DataComplaint current;
int currentPos=0;
MyHolder holder;

public complaintAdapter(Context context, List<DataComplaint> data, String service_id, String type_id){
    this.context=context;
    inflater= LayoutInflater.from(context);
    this.data=data;
    this.service = service_id;
    this.typeid = type_id;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = inflater.inflate( R.layout.container_complaint, viewGroup, false );
        holder = new MyHolder( view );
        Log.e( TAG, "holder = " + typeid );
    return holder;
}




public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, final int i) {

    final MyHolder myHolder= (MyHolder) viewHolder;
    final DataComplaint current=data.get(i);

    myHolder.textcomplaint.setText(current.complaint);
    myHolder.textaddress.setText("Reason: " + current.address);
    myHolder.textType.setText("Client   : " + current.complaint_type);
    myHolder.textplace.setText("Location: " + current.location);
    myHolder.textticket.setText( current.ticket );
    final int value = getItemCount();
    if(typeid.equals( "history" )){
        myHolder.btn.setVisibility( View.GONE );
        myHolder.btn1.setVisibility( View.GONE );
        myHolder.btn2.setVisibility( View.GONE );
    }
    //  myHolder.textPrice.setTextColor( ContextCompat.getColor(context, R.color.colorAccent));
    if(typeid.equals( "pending" )) {
        myHolder.btn.setOnClickListener( new View.OnClickListener() {
            @TargetApi(Build.VERSION_CODES.N)
            @Override
            public void onClick(View v) {
                int btn_position = myHolder.getLayoutPosition();
                Log.e( TAG, "total position" +btn_position );

                Button button = (Button) v;
                Calendar calendar = Calendar.getInstance();
                SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss " );
                String time = format.format( calendar.getTime() );
                type = "In-transit";
                String tickeid = current.ticket;
                Log.e( TAG, "hello = " + service );
                new BackgroundWorker().execute( time, service, tickeid, type );
                button.setVisibility( View.GONE );
                myHolder.btn1.setVisibility( View.VISIBLE );
                myHolder.btn2.setVisibility( View.VISIBLE );





            }
        } );
public int getItemCount() {
    return data.size();
}


class MyHolder extends RecyclerView.ViewHolder{

    TextView textcomplaint;
    TextView textaddress;
    TextView textType,textplace, textticket, textreso;
    Button btn, btn1, btn2;
    int value1;

    // create constructor to get widget reference
    public MyHolder(View itemView) {
        super(itemView);
            textcomplaint = (TextView) itemView.findViewById( R.id.textcomplaint );
            textaddress = (TextView) itemView.findViewById( R.id.textaddress );
            textType = (TextView) itemView.findViewById( R.id.textType );
            textticket = (TextView) itemView.findViewById( R.id.ticketid );
            textplace = (TextView) itemView.findViewById( R.id.textplace );
            btn = (Button) itemView.findViewById( R.id.enter );
            btn1 = (Button) itemView.findViewById( R.id.repositry );
            btn2 = (Button) itemView.findViewById( R.id.exit );
    }

}

这是我的XML文件

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:orientation="horizontal"
android:padding="10dp">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:id="@+id/textcomplaint"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:textColor="@android:color/holo_red_dark"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/ticketid"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:visibility="gone" />

        <TextView
            android:id="@+id/textaddress"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:textColor="@android:color/holo_blue_dark"
            android:textSize="14sp" />

        <TextView
            android:id="@+id/textType"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:textColor="@android:color/holo_purple"
            android:textSize="12sp" />
    </LinearLayout>

    <TextView
        android:id="@+id/textplace"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:paddingTop="20dp"
        android:text="TextView"
        android:textColor="@android:color/black" />

</LinearLayout>


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="80dp"
    android:orientation="horizontal">

    <Button
        android:id="@+id/enter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="In-Trainsit" />

    <Button
        android:id="@+id/repositry"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Reached"
        android:visibility="gone" />

    <Button
        android:id="@+id/exit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Logout"
        android:visibility="gone" />
</LinearLayout>

</RelativeLayout>

这里我的Total ItemCount为8,我的按钮位置从0开始

现在,当我单击0定位按钮时,所有其他1,2,3,4 ...定位按钮都应禁用

操作方法

我只想使用“无按钮”复选框,无切换按钮

预先感谢

3 个答案:

答案 0 :(得分:0)

将currentPos变量设置为按钮的onClick中的单击位置,然后调用notifyDatasetChanged。还要在onBindViewHolder中添加以下代码,以基于currentPos启用/禁用按钮

myHolder.btn.setEnabled(position == currentPos)

如果要首先启用所有按钮,则currentPos的初始值必须为-1(值小于0) 还要添加额外的支票,例如

myHolder.btn.setEnabled(position == currentPos || currentPos == -1)

最终代码为

public class complaintAdapter extends 
RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context context;
String hello, status, type;
private LayoutInflater inflater;
public String service, typeid;
String result_reso;
List<DataComplaint> data= Collections.emptyList();
DataComplaint current;
int currentPos=-1;
MyHolder holder;

public complaintAdapter(Context context, List<DataComplaint> data, String service_id, String type_id){
    this.context=context;
    inflater= LayoutInflater.from(context);
    this.data=data;
    this.service = service_id;
    this.typeid = type_id;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = inflater.inflate( R.layout.container_complaint, viewGroup, false );
        holder = new MyHolder( view );
        Log.e( TAG, "holder = " + typeid );
    return holder;
}




public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, final int i) {

    final MyHolder myHolder= (MyHolder) viewHolder;
    final DataComplaint current=data.get(i);

   myHolder.btn.setEnabled(i== currentPos || currentPos == -1)

    myHolder.textcomplaint.setText(current.complaint);
    myHolder.textaddress.setText("Reason: " + current.address);
    myHolder.textType.setText("Client   : " + current.complaint_type);
    myHolder.textplace.setText("Location: " + current.location);
    myHolder.textticket.setText( current.ticket );
    final int value = getItemCount();
    if(typeid.equals( "history" )){
        myHolder.btn.setVisibility( View.GONE );
        myHolder.btn1.setVisibility( View.GONE );
        myHolder.btn2.setVisibility( View.GONE );
    }
    //  myHolder.textPrice.setTextColor( ContextCompat.getColor(context, R.color.colorAccent));




    if(typeid.equals( "pending" )) {
        myHolder.btn.setOnClickListener( new View.OnClickListener() {
            @TargetApi(Build.VERSION_CODES.N)
            @Override
            public void onClick(View v) {
                int btn_position = myHolder.getLayoutPosition();
                Log.e( TAG, "total position" +btn_position );

                Button button = (Button) v;
                Calendar calendar = Calendar.getInstance();
                SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss " );
                String time = format.format( calendar.getTime() );
                type = "In-transit";
                String tickeid = current.ticket;
                Log.e( TAG, "hello = " + service );
                new BackgroundWorker().execute( time, service, tickeid, type );
                button.setVisibility( View.GONE );
                myHolder.btn1.setVisibility( View.VISIBLE );
                myHolder.btn2.setVisibility( View.VISIBLE );
                currentPos = i;
                notifyDataSetChanged();





            }
        } );
public int getItemCount() {
    return data.size();
}


class MyHolder extends RecyclerView.ViewHolder{

    TextView textcomplaint;
    TextView textaddress;
    TextView textType,textplace, textticket, textreso;
    Button btn, btn1, btn2;
    int value1;

    // create constructor to get widget reference
    public MyHolder(View itemView) {
        super(itemView);
            textcomplaint = (TextView) itemView.findViewById( R.id.textcomplaint );
            textaddress = (TextView) itemView.findViewById( R.id.textaddress );
            textType = (TextView) itemView.findViewById( R.id.textType );
            textticket = (TextView) itemView.findViewById( R.id.ticketid );
            textplace = (TextView) itemView.findViewById( R.id.textplace );
            btn = (Button) itemView.findViewById( R.id.enter );
            btn1 = (Button) itemView.findViewById( R.id.repositry );
            btn2 = (Button) itemView.findViewById( R.id.exit );
    }

}

答案 1 :(得分:0)

Onclicklistner的{​​{1}}中,尝试调用一个传递被点击的buttons的位置值的函数。

函数应如下所示:-

button

然后在ArrayList<Bollean) buttonStates = new ArrayList(); void disbleButtonsNotEqualToPos(int pos){ for(int i=0;i<list.size();i++) { if(i!=pos) buttonStates.add(false); } notifyDataSetChanged(); //reflect changes } 中使用列表(将其添加到按钮中)如下:-

onBindViewHolder()

答案 2 :(得分:0)

重写recycleview适配器中的getViewType()方法。仅返回位置,删除任何其他代码。

相关问题