java Onclick 监听器到可扩展的列表视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16214021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 22:15:10  来源:igfitidea点击:

Onclick listner to expandable listview

javaandroidonclickexpandablelistview

提问by kittu88

I have created an expandable listview, but the onclick listener to the child list items could not be attached.

我创建了一个可扩展的列表视图,但无法附加到子列表项的 onclick 侦听器。

The activity code:

活动代码:

    public class MyActivity extends Activity {

        private ExpandableListView mExpandableList;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);


            mExpandableList = (ExpandableListView)findViewById(R.id.expandable_list);

            ArrayList<Parent> arrayParents = new ArrayList<Parent>();
            ArrayList<String> arrayChildren = new ArrayList<String>();

            //here we set the parents and the children
            for (int i = 0; i < 2; i++){
                //for each "i" create a new Parent object to set the title and the children
                Parent parent = new Parent();
                parent.setTitle("Parent " + i);

                arrayChildren = new ArrayList<String>();
                for (int j = 0; j < 3; j++) {
                    arrayChildren.add("Child " + j);
                }
                parent.setArrayChildren(arrayChildren);

                //in this array we add the Parent object. We will use the arrayParents at the setAdapter
                arrayParents.add(parent);
            }

            //sets the adapter that provides data to the list.
            mExpandableList.setAdapter(new MyCustomAdapter(MyActivity.this,arrayParents));

        }

    }

The custom adapter for the lists:

列表自定义适配器

public class MyCustomAdapter extends BaseExpandableListAdapter {


    private LayoutInflater inflater;
    private ArrayList<Parent> mParent;

    public MyCustomAdapter(Context context, ArrayList<Parent> parent){
        mParent = parent;
        inflater = LayoutInflater.from(context);
    }


    @Override
    //counts the number of group/parent items so the list knows how many times calls getGroupView() method
    public int getGroupCount() {
        return mParent.size();
    }

    @Override
    //counts the number of children items so the list knows how many times calls getChildView() method
    public int getChildrenCount(int i) {
        return mParent.get(i).getArrayChildren().size();
    }

    @Override
    //gets the title of each parent/group
    public Object getGroup(int i) {
        return mParent.get(i).getTitle();
    }

    @Override
    //gets the name of each item
    public Object getChild(int i, int i1) {
        return mParent.get(i).getArrayChildren().get(i1);
    }

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

    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }

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

    @Override
    //in this method you must set the text to see the parent/group on the list
    public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {

        if (view == null) {
            view = inflater.inflate(R.layout.list_item_parent, viewGroup,false);
        }

        TextView textView = (TextView) view.findViewById(R.id.list_item_text_view);
        //"i" is the position of the parent/group in the list
        textView.setText(getGroup(i).toString());

        //return the entire view
        return view;
    }

    @Override
    //in this method you must set the text to see the children on the list
    public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
        if (view == null) {
            view = inflater.inflate(R.layout.list_item_child, viewGroup,false);
        }

        TextView textView = (TextView) view.findViewById(R.id.list_item_text_child);
        //"i" is the position of the parent/group in the list and 
        //"i1" is the position of the child
        textView.setText(mParent.get(i).getArrayChildren().get(i1));

        //return the entire view
        return view;
    }

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

    @Override
    public void registerDataSetObserver(DataSetObserver observer) {
        /* used to make the notifyDataSetChanged() method work */
        super.registerDataSetObserver(observer);
    }
}

The parent class:

父类

public class Parent {
    private String mTitle;
    private ArrayList<String> mArrayChildren;

    public String getTitle() {
        return mTitle;
    }

    public void setTitle(String mTitle) {
        this.mTitle = mTitle;
    }

    public ArrayList<String> getArrayChildren() {
        return mArrayChildren;
    }

    public void setArrayChildren(ArrayList<String> mArrayChildren) {
        this.mArrayChildren = mArrayChildren;
    }

}

What should I do to add onclick listener to the child list items?

我应该怎么做才能将 onclick 侦听器添加到子列表项中?

回答by SKK

Add this after you setAdapter of the Expandable list

在您设置可扩展列表的适配器之后添加此项

mExpandableList.setOnChildClickListener(new OnChildClickListener() {

   @Override
   public boolean onChildClick(ExpandableListView parent, View v,int groupPosition, int childPosition, long id) {

      /* You must make use of the View v, find the view by id and extract the text as below*/

      TextView tv= (TextView) v.findViewById(R.id.childTextView);

      String data= tv.getText().toString();                 

      return true;  // i missed this
    }
});

回答by Tomer Mor

You need to add

你需要添加

ChildClickListener

子点击监听器

like this : mExpandableList.setOnChildClickListeneradd this line to onCreatemethod read here

像这样:mExpandableList.setOnChildClickListener将此行添加到onCreate方法 读取here

also this is a good example

这也是一个很好的例子

回答by tony gil

Declare your listView with an overridden onChildClick

使用覆盖的 onChildClick 声明您的 listView

ExpandableListView listView = getExpandableListView();
listView.setChoiceMode(ExpandableListView.CHOICE_MODE_SINGLE);
listView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
        @Override
        public boolean onChildClick(ExpandableListView parent, View v,int groupPosition, int childPosition,long id) {
            Log.d(TAG,"I got clicked childPosition:["+childPosition+"] groupPosition:["+groupPosition+"] id:["+id+"]");
            return true;
        }
});