gpt4 book ai didi

java - 子单击监听器在可扩展 ListView 中不起作用

转载 作者:行者123 更新时间:2023-12-01 18:00:57 25 4
gpt4 key购买 nike

我的 Activity 中有可扩展 ListView ,但问题是当我单击子元素时,它没有播种 toast 消息。下面是我的 MainActivity 类和适配器类:

MainActivity.class

public class MainActivity extends AppCompatActivity {

ExpandableListView expand;
List<String> category = new ArrayList<>();
Map<String,List<String>> child = new HashMap<>();
MyExListAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

expand = findViewById(R.id.expand);

List<String> mobiles = new ArrayList<>();
List<String> jewel = new ArrayList<>();

category.add("Mobile Covers");
category.add("T-Shirts");
category.add("Notebooks");
category.add("Jewellery");
category.add("Honey");
category.add("Corporate Gifts");
category.add("Packaging");
category.add("Deals");

mobiles.add("Apple");
mobiles.add("Samsung");
mobiles.add("OnePlus");
mobiles.add("Vivo");
mobiles.add("Oppo");
mobiles.add("Redmi");

jewel.add("Designer");
jewel.add("Classic");

child.put(category.get(0),mobiles);
child.put(category.get(1),jewel);

adapter = new MyExListAdapter(this,category,child);
expand.setAdapter(adapter);

expand.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {

Toast.makeText(getApplicationContext(),"" +child.get(category.get(groupPosition)).get(childPosition),Toast.LENGTH_SHORT).show();
return false;
}
});

}

MyExListAdapter.java

package com.app.aamkuapp.Adapters;

public class MyExListAdapter extends BaseExpandableListAdapter {

Context context;
List<String> category;
Map<String ,List<String>> child;

public MyExListAdapter(Context context, List<String> category, Map<String, List<String>> child) {
this.context = context;
this.category = category;
this.child = child;
}

@Override
public int getGroupCount() {
return category.size();
}

@Override
public int getChildrenCount(int groupPosition) {
return child.get(category.get(groupPosition)).size();
}

@Override
public Object getGroup(int groupPosition) {
return category.get(groupPosition);
}

@Override
public Object getChild(int groupPosition, int childPosition) {
return child.get(category.get(groupPosition)).get(childPosition);
}

@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}

@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}

@Override
public boolean hasStableIds() {
return false;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

String cat = (String) getGroup(groupPosition);

if(convertView == null){

LayoutInflater inflater = (LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_parent,null);
}

TextView txt1 = convertView.findViewById(R.id.textParent);
txt1.setText(cat);

return convertView;
}

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

String chi = (String) getChild(groupPosition,childPosition);

if(convertView == null){

LayoutInflater inflater = (LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_child,null);
}

TextView txt2 = convertView.findViewById(R.id.textChild);
txt2.setText(chi);

return convertView;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
}

有人请告诉我为什么 Toast 在单击子元素时不显示。任何帮助将不胜感激。

谢谢

最佳答案

从此更改您的适配器代码..

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}

对此..

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}

希望这有帮助。

关于java - 子单击监听器在可扩展 ListView 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60631125/

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