首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在onChildClickListener,ExpandableListView上更改groupName

在onChildClickListener,ExpandableListView上更改groupName
EN

Stack Overflow用户
提问于 2017-11-29 20:27:07
回答 0查看 130关注 0票数 1

我在ExpandableListView中有一个包含2个孩子的组,我想为单击的孩子的名称设置组名称。

如何访问在getGroupView()方法中生成的TextView

顺便说一下,如果能以更好的方式编写代码,我将很高兴听到一些关于代码的提示

代码语言:javascript
复制
CustomAdapter customAdapter = new CustomAdapter(this);
    expandableListView.setAdapter(customAdapter);

    expandableListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

        }
    });
    expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
        @Override
        public boolean onChildClick(ExpandableListView parent, View v, int group, int position, long id) {

            String tekst = (String) parent.getExpandableListAdapter().getChild(group,position);
            parent.collapseGroup(group);

            Toast.makeText(getApplicationContext(), tekst ,Toast.LENGTH_SHORT).show();
            return false;
        }
 });

CustomAdapter.class:

代码语言:javascript
复制
private Context context;
public CustomAdapter(Context context)
{
    this.context = context;
}

private String[] grupa = {"Płeć"};
private String[][] dzieci = {{"Kobieta","Mężczyzna"}};

//region gettersFolded
@Override
public int getGroupCount() {
    return grupa.length;
}

@Override
public int getChildrenCount(int position) {
    return dzieci[position].length;
}

@Override
public Object getGroup(int groupPosition) {
    return grupa[groupPosition];
}

@Override
public Object getChild(int group, int position) {
    return dzieci[group][position];
}

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

@Override
public long getChildId(int group, int position) {
    return position;
}

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

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View view, ViewGroup parent) {
    TextView textView = new TextView(context);
    textView.setText(grupa[groupPosition]);
    textView.setPadding(10,10,10,10);
    textView.setBackgroundResource(R.drawable.btm_line_shape);
    textView.setTextSize(14);
    return textView;
}

@Override
public View getChildView(int groupPosition, int childPosition, boolean isExpanded, View view, ViewGroup parent) {
    TextView textView = new TextView(context);
    textView.setText(dzieci[groupPosition][childPosition]);
    textView.setTextSize(12);
    textView.setPadding(10,10,10,10);
    return textView;
}

@Override
public boolean isChildSelectable(int i, int i1) {
    return true;
}
//endregion Region
EN

回答

页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47552879

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档