gpt4 book ai didi

android - RecyclerView 中的 CheckBox 在按下时不起作用

转载 作者:行者123 更新时间:2023-11-29 14:44:08 26 4
gpt4 key购买 nike

我有带有 TextView 和 CheckBox 的基本 RecyclerView 项目。但是当我单击 CheckBox 时,它不起作用。当我长按时,它的效果更加惊人。

我做了一个研究,发现 android:descendantFocusability="blocksDescendants" , android:clickable="false", android:focusable="false" , android:longClickable="false" 但他们没有帮助我

这是我的 RecyclerView 项目布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants">

<TextView
android:id="@+id/item_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:textColor="#000000"/>

<android.support.v7.widget.AppCompatCheckBox
android:id="@+id/item_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_marginEnd="10dp"
app:buttonTint="@color/colorAccent"
android:focusable="false"
android:focusableInTouchMode="false"/>

</LinearLayout>

这是我的适配器类

public class FilterTypeAdapter extends RecyclerView.Adapter<FilterTypeAdapter.MyViewHolder> {

private ArrayList<String> mData;
private Context mContext;

public FilterTypeAdapter() {
}

public FilterTypeAdapter(Context mContext, ArrayList<String> mData) {
this.mContext = mContext;
this.mData = mData;
}


@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int i) {

View v = LayoutInflater.from(mContext).inflate(R.layout.recycler_view_single_row, parent, false);
MyViewHolder vh = new MyViewHolder(v);

return vh;

}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {

holder.mText.setText(mData.get(position));
holder.mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Toast.makeText(mContext, "Status is: " + isChecked + "", Toast.LENGTH_SHORT).show();
}
});

}

@Override
public int getItemCount() {
return mData.size();
}


// View Holder
public class MyViewHolder extends RecyclerView.ViewHolder {

private TextView mText;
private CheckBox mCheckBox;

public MyViewHolder(View itemView) {
super(itemView);

mText = itemView.findViewById(R.id.item_text);
mCheckBox = itemView.findViewById(R.id.item_checkbox);

}
}

}

最佳答案

我发现这里一切正常。

package <your_package>;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.AppCompatCheckBox;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;

import <your_package>.R;

import java.util.ArrayList;

public class TestActivity extends AppCompatActivity {

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.test_recyclerview);

RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
ArrayList<String> data = new ArrayList<>();
for(int i =0; i < 1000; i++) {
data.add(String.valueOf(i));
}
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false));
recyclerView.setAdapter(new FilterTypeAdapter(getApplicationContext(), data));
}

public class FilterTypeAdapter extends RecyclerView.Adapter<MyViewHolder> {

private ArrayList<String> mData;
private HashMap<String, Boolean> mChecked;
private Context mContext;

public FilterTypeAdapter() {
}

public FilterTypeAdapter(Context mContext, ArrayList<String> mData) {
this.mContext = mContext;
this.mData = mData;
this.mChecked = new HashMap<>();
}


@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int i) {

View v = TestActivity.this.getLayoutInflater().inflate(R.layout.test_recyclerview_item, parent, false);
MyViewHolder vh = new MyViewHolder(v);

return vh;

}

@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {

holder.mText.setText(mData.get(position));
holder.mCheckBox.setOnCheckedChangeListener(null);
if(mChecked.containsKey(mData.get(position))) {
holder.mCheckBox.setChecked(mChecked.get(mData.get(position)));
}
else {
holder.mCheckBox.setChecked(false);
}
holder.mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mChecked.put(mData.get(holder.getAdapterPosition()), isChecked);
Toast.makeText(mContext, "Status is: " + isChecked + "", Toast.LENGTH_SHORT).show();
}
});

}

@Override
public int getItemCount() {
return mData.size();
}
}

// View Holder
public class MyViewHolder extends RecyclerView.ViewHolder {

private TextView mText;
private AppCompatCheckBox mCheckBox;

public MyViewHolder(View itemView) {
super(itemView);

mText = (TextView) itemView.findViewById(R.id.item_text);
mCheckBox = (AppCompatCheckBox) itemView.findViewById(R.id.item_checkbox);

}
}
}

R.layout.test_recyclerview

<android.support.v7.widget.RecyclerView android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="30dp"
android:layout_marginEnd="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"

xmlns:android="http://schemas.android.com/apk/res/android" />

R.layout.testrecyclerview_item

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:id="@+id/item_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:textColor="#000000"/>

<android.support.v7.widget.AppCompatCheckBox
android:id="@+id/item_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_marginEnd="10dp"
app:buttonTint="@color/colorAccent"
/>

</LinearLayout>

关于android - RecyclerView 中的 CheckBox 在按下时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47052009/

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