本文介绍了onChildClickListener不是在扩展列表视图的工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个应用程序,它具有可扩展列表视图我能够得到扩展列表视图,但我的孩子的点击不工作。我想就点击扩展列表视图中的子显示敬酒消息。请帮帮我,让我知道我在做什么错误。先谢谢了。

 我的活动code:    进口android.content.Context;
    进口android.database.Cursor;
    进口android.graphics.Color;
    进口android.os.Bundle;
    进口android.support.v7.app.AppCompatActivity;
    进口android.support.v7.widget.Toolbar;
    进口android.view.Menu;
    进口android.view.MenuItem;
    进口android.view.View;
    进口android.widget.ExpandableListAdapter;
    进口android.widget.ExpandableListView;
    进口android.widget.SimpleCursorTreeAdapter;
    进口android.widget.Toast;    进口的java.util.ArrayList;
    进口的java.util.HashMap;
    进口的java.util.List;
    公共类AdminActivity扩展AppCompatActivity {        工具栏工具栏;
        ExpandableListAdapter listAdapter;
        清单<串GT; titleText;
        SQLiteDataBaseAdapter分贝;        HashMap的<字符串列表<串GT;> listDataChild;        ExpandableListView登录,安卓,IOS,测试,JAVA,dotNet的,操作系统,HR,别人;        @覆盖
        保护无效的onCreate(捆绑savedInstanceState){
            super.onCreate(savedInstanceState);
            的setContentView(R.layout.activity_admin);            工具栏=(栏)findViewById(R.id.appBar);
            toolbar.setTitle(管理屏);
            toolbar.setTitleTextColor(Color.WHITE);            登录=(ExpandableListView)findViewById(R.id.expandableListViewLogin);            titleText =新的ArrayList<>();
            titleText.add(一个用户身份认证);
            titleText.add(Android的帖子认证);
            titleText.add(iOS的帖子认证);
            titleText.add(测试验证的帖子);
            titleText.add(Java的帖子认证);
            titleText.add(点网帖认证);
            titleText.add(OS帖子认证);
            titleText.add(HR帖子认证);
            titleText.add(其他帖子认证);
            SQLiteDataBaseAdapter DB =新SQLiteDataBaseAdapter(本);
            清单<串GT; childDataLogin = db.getLoginList();
            清单<串GT; childDataAndroid = db.getAndroidList();
            清单<串GT; childDataIos = db.getIosList();
            清单<串GT; childDataTesting = db.getTestingList();
            清单<串GT; childDataJava = db.getJavaList();
            清单<串GT; childDataDotNet = db.getDotNetList();
            清单<串GT; childDataOs = db.getOSList();
            清单<串GT; childDataHr = db.getHRList();
            清单<串GT; childDataOthers = db.getOthersList();            listDataChild =新的HashMap<>();
            listDataChild.put(titleText.get(0),childDataLogin); //头,儿童数据
            listDataChild.put(titleText.get(1),childDataAndroid);
            listDataChild.put(titleText.get(2),childDataIos);
            listDataChild.put(titleText.get(3),childDataTesting);
            listDataChild.put(titleText.get(4),childDataJava);
            listDataChild.put(titleText.get(5),childDataDotNet);
            listDataChild.put(titleText.get(6),childDataOs);
            listDataChild.put(titleText.get(7),childDataHr);
            listDataChild.put(titleText.get(8),childDataOthers);            listAdapter =新MyExpandableListAdapter(这一点,titleText,listDataChild);
            login.setAdapter(listAdapter);            //列表视图儿童点击监听器
            login.setOnChildClickListener(新ExpandableListView.OnChildClickListener(){                @覆盖
                公共布尔onChildClick(ExpandableListView父视图V,
                                            INT groupPosition,诠释childPosition,长ID){
                    Toast.makeText(
                            getApplicationContext(),
                           titleText.get(groupPosition)
                                    +:
                                    + listDataChild.get(
                                    titleText.get(groupPosition))。得到(
                                    childPosition),Toast.LENGTH_SHORT)
                            。显示();
                    返回true;
                }
            });        }        @覆盖
        公共布尔onCreateOptionsMenu(菜单菜单){
            //充气菜单;如果是present这增加了项目操作栏。
            。getMenuInflater()膨胀(R.menu.menu_admin,菜单);
            返回true;
        }        @覆盖
        公共布尔onOptionsItemSelected(菜单项项){
            //处理动作栏项目点击这里。操作栏会
            //自动处理上点击主页/向上按钮,只要
            //你在AndroidManifest.xml中指定一个父活动。
            INT ID = item.getItemId();            // noinspection SimplifiableIfStatement
            如果(ID == R.id.action_settings){
                返回true;
            }            返回super.onOptionsItemSelected(项目);
        }    }孩子的布局: < XML版本=1.0编码=UTF-8&GT?;
    < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:方向=垂直>        <的TextView
            机器人:ID =@ + ID / lblListItem
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =WRAP_CONTENT
            机器人:TEXTSIZE =17sp
            机器人:paddingTop =5DP
            机器人:paddingBottom会=5DP
            机器人:paddingLeft =机器人:ATTR / expandableList preferredChildPaddingLeft/>    < / LinearLayout中>适配器code:     公共类MyExpandableListAdapter扩展BaseExpandableListAdapter {
            私人语境_context;
            私人列表<串GT; _listDataHeader; //头标题
            //子数据头标题格式,标题子
            私人的HashMap<字符串列表<串GT;> _listDataChild;            公共MyExpandableListAdapter(上下文的背景下,列表与LT;弦乐> listDataHeader,HashMap的<字符串列表<串GT;> listChildData){
                this._context =背景;
                this._listDataHeader = listDataHeader;
                this._listDataChild = listChildData;
            }            @覆盖
            公众诠释getGroupCount(){
                返回this._listDataHeader.size();
            }            @覆盖
            公众诠释getChildrenCount(INT groupPosition){
                返回this._listDataChild.get(this._listDataHeader.get(groupPosition))
                        。尺寸()​​;
            }            @覆盖
            公共对象getGroup(INT groupPosition){
                返回this._listDataHeader.get(groupPosition);
            }            @覆盖
            公共对象getChild(INT groupPosition,诠释childPosititon){
                返回this._listDataChild.get(this._listDataHeader.get(groupPosition))
                        获得(childPosititon);
            }            @覆盖
            众长getGroupId(INT groupPosition){
                返回groupPosition;
            }            @覆盖
            众长getChildId(INT groupPosition,诠释childPosition){
                返回childPosition;
            }            @覆盖
            公共布尔hasStableIds(){
                返回false;
            }            @覆盖
            公共查看getGroupView(INT groupPosition,布尔isExpanded,
                                     查看convertView,父母的ViewGroup){
                字符串headerTitle =(字符串)getGroup(groupPosition);
                如果(convertView == NULL){
                    LayoutInflater infalInflater =(LayoutInflater)this._context
                            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    convertView = infalInflater.inflate(R.layout.list_group,NULL);
                }                TextView的lblListHeader =(TextView中)convertView
                        .findViewById(R.id.expandableListHeader);
                lblListHeader.setTypeface(NULL,Typeface.BOLD);
                lblListHeader.setText(headerTitle);                返回convertView;
            }            @覆盖
            公共查看getChildView(INT groupPosition,最终诠释childPosition,
                                     布尔isLastChild,查看convertView,父母的ViewGroup){                最后弦乐childText =(字符串)getChild(groupPosition,childPosition);                如果(convertView == NULL){
                    LayoutInflater infalInflater =(LayoutInflater)this._context
                            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    convertView = infalInflater.inflate(R.layout.list_item,NULL);
                }                TextView的txtListChild =(TextView中)convertView
                        .findViewById(R.id.lblListItem);                txtListChild.setText(childText);
                返回convertView;            }            @覆盖
            公共布尔isChildSelectable(INT I,INT I1){
                返回false;
            }
        }


解决方案

一定要重写您的扩展列表适配器的 isChildSelectable 方法并返回true,像这样:

 公共类MyExpandableListAdapter扩展BaseExpandableListAdapter {
    @覆盖
    公共布尔isChildSelectable(INT groupPosition,诠释childPosition){
        返回true;
    }
   ...
}

 机器人:可聚焦=假

expandlist_child_item.xml 文件中的复选框部分。

我希望这可以帮助别人。

I am creating an application, which has an expandable list view I am able to get the expandable list view, but my child click is not working. I am trying to display a toast message on clicking a child in expandable list view. Please help me and let me know what mistake am I doing. Thanks in advance.

    My Activity code:

    import android.content.Context;
    import android.database.Cursor;
    import android.graphics.Color;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.Toolbar;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.ExpandableListAdapter;
    import android.widget.ExpandableListView;
    import android.widget.SimpleCursorTreeAdapter;
    import android.widget.Toast;

    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;


    public class AdminActivity extends AppCompatActivity {

        Toolbar toolbar;
        ExpandableListAdapter listAdapter;
        List<String> titleText;
        SQLiteDataBaseAdapter db;

        HashMap<String, List<String>> listDataChild;

        ExpandableListView login, android, ios, testing, java, dotNet, os, hr, others;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_admin);

            toolbar = (Toolbar) findViewById(R.id.appBar);
            toolbar.setTitle(" Admin Screen");
            toolbar.setTitleTextColor(Color.WHITE);

            login = (ExpandableListView) findViewById(R.id.expandableListViewLogin);

            titleText = new ArrayList<>();
            titleText.add("User Id Authentication");
            titleText.add("Android Posts Authentication");
            titleText.add("iOS Posts Authentication");
            titleText.add("Testing Posts Authentication");
            titleText.add("Java Posts Authentication");
            titleText.add("Dot Net Posts Authentication");
            titleText.add("OS Posts Authentication");
            titleText.add("HR Posts Authentication");
            titleText.add("Others Posts Authentication");






            SQLiteDataBaseAdapter db = new SQLiteDataBaseAdapter(this);
            List<String> childDataLogin =  db.getLoginList();
            List<String> childDataAndroid =  db.getAndroidList();
            List<String> childDataIos =  db.getIosList();
            List<String> childDataTesting =  db.getTestingList();
            List<String> childDataJava =  db.getJavaList();
            List<String> childDataDotNet =  db.getDotNetList();
            List<String> childDataOs =  db.getOSList();
            List<String> childDataHr =  db.getHRList();
            List<String> childDataOthers =  db.getOthersList();

            listDataChild = new HashMap<>();
            listDataChild.put(titleText.get(0), childDataLogin); // Header, Child data
            listDataChild.put(titleText.get(1), childDataAndroid);
            listDataChild.put(titleText.get(2), childDataIos);
            listDataChild.put(titleText.get(3), childDataTesting);
            listDataChild.put(titleText.get(4), childDataJava);
            listDataChild.put(titleText.get(5), childDataDotNet);
            listDataChild.put(titleText.get(6), childDataOs);
            listDataChild.put(titleText.get(7), childDataHr);
            listDataChild.put(titleText.get(8), childDataOthers);



            listAdapter = new MyExpandableListAdapter(this, titleText, listDataChild);
            login.setAdapter(listAdapter);

            // Listview on child click listener
            login.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {

                @Override
                public boolean onChildClick(ExpandableListView parent, View v,
                                            int groupPosition, int childPosition, long id) {
                    Toast.makeText(
                            getApplicationContext(),
                           titleText.get(groupPosition)
                                    + " : "
                                    + listDataChild.get(
                                    titleText.get(groupPosition)).get(
                                    childPosition), Toast.LENGTH_SHORT)
                            .show();
                    return true;
                }
            });

        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.menu_admin, menu);
            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();

            //noinspection SimplifiableIfStatement
            if (id == R.id.action_settings) {
                return true;
            }

            return super.onOptionsItemSelected(item);
        }



    }

Layout of child:



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

        <TextView
            android:id="@+id/lblListItem"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textSize="17sp"
            android:paddingTop="5dp"
            android:paddingBottom="5dp"
            android:paddingLeft="?                                                                               android:attr/expandableListPreferredChildPaddingLeft" />

    </LinearLayout>

Adapter Code:



     public class MyExpandableListAdapter extends BaseExpandableListAdapter {
            private Context _context;
            private List<String> _listDataHeader; // header titles
            // child data in format of header title, child title
            private HashMap<String, List<String>> _listDataChild;

            public MyExpandableListAdapter(Context context, List<String>            listDataHeader, HashMap<String, List<String>> listChildData) {
                this._context = context;
                this._listDataHeader = listDataHeader;
                this._listDataChild = listChildData;
            }

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

            @Override
            public int getChildrenCount(int groupPosition) {
                return     this._listDataChild.get(this._listDataHeader.get(groupPosition))
                        .size();
            }

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

            @Override
            public Object getChild(int groupPosition, int childPosititon) {
                return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                        .get(childPosititon);
            }

            @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 headerTitle = (String) getGroup(groupPosition);
                if (convertView == null) {
                    LayoutInflater infalInflater = (LayoutInflater) this._context
                            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    convertView = infalInflater.inflate(R.layout.list_group, null);
                }

                TextView lblListHeader = (TextView) convertView
                        .findViewById(R.id.expandableListHeader);
                lblListHeader.setTypeface(null, Typeface.BOLD);
                lblListHeader.setText(headerTitle);

                return convertView;
            }

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

                final String childText = (String) getChild(groupPosition,   childPosition);

                if (convertView == null) {
                    LayoutInflater infalInflater = (LayoutInflater) this._context
                            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    convertView = infalInflater.inflate(R.layout.list_item, null);
                }

                TextView txtListChild = (TextView) convertView
                        .findViewById(R.id.lblListItem);

                txtListChild.setText(childText);
                return convertView;

            }

            @Override
            public boolean isChildSelectable(int i, int i1) {
                return false;
            }
        }
解决方案

Be sure to override the isChildSelectable method of your expandable list adapter and return true, like so:

public class MyExpandableListAdapter extends BaseExpandableListAdapter {
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
   ...
}

Or

  android:focusable="false"

within the CheckBox section of your expandlist_child_item.xml file.

I hope that this helps somebody.

这篇关于onChildClickListener不是在扩展列表视图的工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 22:33