[英]Clear selected item on multiple radio group with onCheckedChangeListener
[英]Chip Group OnCheckedChangeListener() not triggered
我正在嘗試制作一個基於 ChipGroup & Chip 的 recyclerview 過濾器
我在我的應用程序上使用片段,因此,包含 RecyclerView 的片段包含一個 frameLayout,它會膨脹 ChipGroup 過濾器片段
當用戶取消選擇 ChipGroup 內的所有芯片時,我試圖觸發一個監聽器(當用戶檢查芯片時,我已經將監聽器放在芯片上用於觸發)
我已經在芯片組上放置了一些監聽器,但沒有人被觸發
FilterFragment.java
public class FilterFragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
public ChipGroup chipGroup;
// TODO: Rename and change types of parameters
public View.OnClickListener chipClickListener;
private OnFragmentInteractionListener mListener;
public FilterFragment() {
// Required empty public constructor
}
public static FilterFragment newInstance(View.OnClickListener
param1) {
CoachFilterFragment fragment = new CoachFilterFragment();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup
container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_filter,
container, false);
this.chipGroup = view.findViewById(R.id.chipGroup);
for(Skill skill : ((MainActivity)getContext()).api.skills){
Chip chip = new Chip(getContext());
chip.setId(skill.getId());
chip.setText(skill.getName());
chip.setOnClickListener(chipClickListener);
chip.setCheckable(true);
chipGroup.addView(chip);
}
chipGroup.setOnCheckedChangeListener((chipGroup, id) -> {
Log.d("test","ok");
});
return view;
}
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}
FilterFragment.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
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:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".Fragment.FilterFragment">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.chip.ChipGroup
android:id="@+id/chipGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</com.google.android.material.chip.ChipGroup>
</androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout>
有人知道為什么我的 ChipGroup 沒有觸發任何偵聽器嗎?也許我缺少某些參數或其他東西?
您的代碼很好,唯一的問題是setOnCheckedChangeListener()
僅在您的ChipGroup
用於singleSelection
閱讀ChipGroup
文檔
另請閱讀
調用 setOnCheckedChangeListener(OnCheckedChangeListener) 注冊一個回調,當被檢查芯片在該組中發生變化時調用。 此回調僅在單選模式下調用。
如果你想使用setOnCheckedChangeListener()
的ChipGroup
比你需要app:singleSelection="true"
根據您的以下評論,我添加了示例代碼以在選擇 ChipGroup 時進行處理
用於在ChipGroup
保持選擇的示例代碼
布局.xml
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.chip.ChipGroup
android:id="@+id/chipGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
>
</com.google.android.material.chip.ChipGroup>
</androidx.constraintlayout.widget.ConstraintLayout>
<Button
android:layout_width="match_parent"
android:text="Get Result"
android:id="@+id/btnShowResult"
android:layout_gravity="bottom"
android:layout_height="wrap_content" />
</LinearLayout>
活動代碼
public class Main3Activity extends AppCompatActivity {
public ChipGroup chipGroup;
public Button btnShowResult;
public ArrayList<Boolean> booleanArrayList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
chipGroup = findViewById(R.id.chipGroup);
btnShowResult = findViewById(R.id.btnShowResult);
btnShowResult.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
for (int i = 0; i < booleanArrayList.size(); i++) {
Log.e("RESULT", i + " :" + booleanArrayList.get(i));
}
}
});
for (int i = 0; i < 5; i++) {
Chip chip = new Chip(this);
chip.setId(i);
chip.setTag(i);
booleanArrayList.add(false);
chip.setText("Chip No : " + i);
chip.setCheckable(true);
chip.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
int tag = (int) compoundButton.getTag();
booleanArrayList.set(tag, b);
}
});
chipGroup.addView(chip);
}
chipGroup.invalidate();
chipGroup.setOnCheckedChangeListener(new ChipGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(ChipGroup chipGroup, int i) {
Chip chip = chipGroup.findViewById(i);
if (chip != null)
Toast.makeText(getApplicationContext(), "Chip is " + chip.getText().toString(), Toast.LENGTH_SHORT).show();
Log.e("OnCheckedChangeListener", "Called");
}
});
}
ChipGroup.OnCheckedChangeListener onCheckedChangeListener = new ChipGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(ChipGroup chipGroup, int i) {
}
};
}
有關更多信息,請查看以下文章
所有芯片都需要具有android:checkable="true"
屬性才能在其上存在觸發器。
除了設置任何芯片的初始檢查(無需點擊),執行順序必須首先使用chipGroup.setOnCheckedChangeListener()
創建監聽器,然后使用chip.isChecked = true
。
因此,您可以動態處理 xml 和 isChecked 中的 checkable 屬性。
<com.google.android.material.chip.ChipGroup
..
app:singleSelection="true">
<com.google.android.material.chip.Chip
..
android:checkable="true"/>
binding.chipGroup.setOnCheckedChangeListener { chipGroup, i ->
}
binding.firstChip.isChecked = true //default
GL
Chip Click 偵聽器僅在chipGroup 中的單個選擇設置為true 時才起作用。
app:singleSelection="true"
如果您使用如下樣式,請確保也將其分配給每個單獨的芯片。
style="@style/Widget.MaterialComponents.Chip.Choice"
像這樣:
<com.google.android.material.chip.ChipGroup
android:id="@+id/chip_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
app:singleSelection="true"
style="@style/Widget.MaterialComponents.Chip.Choice">
<com.google.android.material.chip.Chip
android:id="@+id/chip_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/Widget.MaterialComponents.Chip.Choice"
android:text="@string/announcements"
android:textAppearance="@style/ChipThemeBold"/>
<com.google.android.material.chip.Chip
android:id="@+id/chip_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/Widget.MaterialComponents.Chip.Choice"
android:text="@string/fyi"
android:textAppearance="@style/ChipThemeBold"/>
<com.google.android.material.chip.Chip
android:id="@+id/chip_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/Widget.MaterialComponents.Chip.Choice"
android:text="@string/heartbeat"
android:textAppearance="@style/ChipThemeBold" />
<com.google.android.material.chip.Chip
android:id="@+id/chip_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/Widget.MaterialComponents.Chip.Choice"
android:text="@string/pitch"
android:textAppearance="@style/ChipThemeBold" />
<com.google.android.material.chip.Chip
android:id="@+id/chip_6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/Widget.MaterialComponents.Chip.Choice"
android:text="@string/question"
android:textAppearance="@style/ChipThemeBold"
android:layout_marginEnd="15dp" />
</com.google.android.material.chip.ChipGroup>
它是這樣工作的。
<com.google.android.material.chip.ChipGroup
...
...
style="@style/Widget.MaterialComponents.Chip.Choice"
app:singleSelection="true">
<!-- Declare your chips or add them programmatically -->
</com.google.android.material.chip.ChipGroup>
// Inflate instead of NEW instance
val chip = layoutInflater.inflate(R.layout.chip, chipGroup, false) as Chip
chip.text = "Text"
chipGroup.addView(chip)
// setOnCheckedChangeListener
chipGroup.setOnCheckedChangeListener { group, checkedId ->
// The same checked chip
if (checkedId == -1) {
return@setOnCheckedChangeListener
}
// TODO
}
您可以為所有芯片組子項添加setOnCheckedChangeListener
。 (在科特林)
for (index in 0 until chipGroup!!.childCount) {
val chip: Chip = chipGroup!!.getChildAt(index) as Chip
// Set the chip checked change listener
chip.setOnCheckedChangeListener{view, isChecked ->
if (isChecked){
list.add(view.text.toString())
}else{
list.remove(view.text.toString())
}
if (list.isNotEmpty()){
// Show the selection
Log.d(LOGTAG,"Selected $list")
}
}
}
我有同樣的問題,但它與ChipGroup
因為我將ChipGroup
放在錯誤的位置,如下所示:
<androidx.constraintlayout.widget.ConstraintLayout>
<com.google.android.material.chip.ChipGroup />
<other full screen view />
<androidx.constraintlayout.widget.ConstraintLayout/>
它使我的ChipGroup
聚焦,所以只需將ChipGroup
移動到父視圖的底部
<androidx.constraintlayout.widget.ConstraintLayout>
<other full screen view />
<-- change here -->
<com.google.android.material.chip.ChipGroup />
<androidx.constraintlayout.widget.ConstraintLayout/>
它有效,希望它有所幫助
如果您想要水平滾動,我建議不要使用帶有芯片的 recyclerview,因為您有
HorizontalScrollView
並且它與芯片組配合得很好:
基於 Nilesh Rathod 的回答。 要動態添加籌碼,您需要以下代碼:
活動文件代碼:
String[] fileNameChip = getIntent().getStringArrayExtra(Constants.CHIPS_NAME_ARRAY);
for (final String chipName : fileNameChip) {
LayoutInflater layoutInflater = getLayoutInflater();
Chip chip = (Chip)
layoutInflater.inflate(R.layout.cat_chip_group_item_choice, chipGroup, false);
chip.setText(chipName);
chip.setId(i); //HERE SET ID THAT WILL BE CHECKED_ID ON setOnCheckedChangeListener
chip.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "onClick: " + chipName );
}
});
chipGroup.addView(chip);
}
chipGroup.setOnCheckedChangeListener(new ChipGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(ChipGroup group, int checkedId) {
Log.d(TAG, "onCheckedChanged: " + checkedId);
}
});
活動 XML 文件:
<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<HorizontalScrollView
android:layout_width="match_parent"
android:scrollbars="none"
android:layout_height="wrap_content">
<com.google.android.material.chip.ChipGroup
android:id="@+id/chip_group_code_files"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:singleLine="true"
app:singleSelection="true" />
</HorizontalScrollView>
cat_chip_group_item_choice.xml :(指定芯片類型:Choice,filter,Action,Normal)
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.chip.Chip
xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/Widget.MaterialComponents.Chip.Choice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
發現這是使用多選芯片組最簡單的方法:
chipGroup.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View view, int i, int i1, int i2, int i3, int i4, int i5, int i6, int i7) {
List<Integer> ids = chipGroup.getCheckedChipIds();
if (ids.contains(R.id.chip1)) {
// do your stuff
}
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.