使用BaseExpandableListAdapter的Android ExpandableListVIew

使用BaseExpandableListAdapter的Android ExpandableListVIew,android,Android,我正在使用MyExpandableListAdapter扩展 BaseExpandableListAdapter和我在适配器中实现BaseExpandableListAdapter的所有方法,我创建扩展ExpandableListActivity的MyExpadableListActivity。 MyExpandableListAdapter是MyExpadableListActivity的内部类。 我使用一个xml视图来添加childs和groupd UI(在下面的过去)。 当我运行应用程序时

我正在使用MyExpandableListAdapter扩展 BaseExpandableListAdapter和我在适配器中实现BaseExpandableListAdapter的所有方法,我创建扩展ExpandableListActivity的MyExpadableListActivity。 MyExpandableListAdapter是MyExpadableListActivity的内部类。 我使用一个xml视图来添加childs和groupd UI(在下面的过去)。 当我运行应用程序时,它的显示很好。 我的问题是,我在MyExpadableListActivity中重写了ExpandableListActivity的onChildClick(..)方法。当我运行代码时,它不能使用onChildClick方法。 如何使用这种情况或其他建议实现onChildClick(…)方法。。 希望有人指出我做错了什么。。。。 如果有人指点我如何解决问题,我将不胜感激

public class MyExpadableListActivity extends ExpandableListActivity implements
    ExpandableListView.OnChildClickListener {
    ExpandableListAdapter mAdapter;
    Context mContext;   
    static final String groups[] = { "Chats", "Contacts (289)",
        "Group Chat (7)", "e-Card  (137)", "Pending  (37)",
        "Bookmarks" };

static final String children[][] = {
        { "ListofChats" },
        { "Group", "groupt01", "groupt02", "groupt03" },
        { "Groupd 01", "", "Group  02", "", "Group  03", "" },
        { "E-Card-01", "", "E-Card-02", "",
                "Business Card Received", "" },
        { " Req", "", " Req", "", " Req", "" } };

private static final String TAG = "Hookup";
XMPPConnection connection = SignInMainActivity.connection;

@Override
public void onCreate(Bundle savedInstanceState) {       
        super.onCreate(savedInstanceState);                 
    mAdapter = new MyExpandableListAdapter(this);
    setListAdapter(mAdapter);
    registerForContextMenu(getExpandableListView());        
}
@Override
public boolean onChildClick(ExpandableListView parent, View v,
        int groupPosition, int childPosition, long id) {
    // TODO Auto-generated method stub
    System.out.println("Insidded onContextItemSelected");
    return super.onChildClick(parent, v, groupPosition, childPosition, id);
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
    menu.setHeaderTitle("Hookup Menu");
    menu.add(0, 0, 0, R.string.hello);
}

public boolean onContextItemSelected(MenuItem item) {
    System.out.println("Insidded onContextItemSelected");

    Log.i(TAG, "onContextItemSelected");
    ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) item
            .getMenuInfo();

    String title = ((TextView) info.targetView).getText().toString();
    int type = ExpandableListView
            .getPackedPositionType(info.packedPosition);

    if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
        int groupPos = ExpandableListView
                .getPackedPositionGroup(info.packedPosition);
        int childPos = ExpandableListView
                .getPackedPositionChild(info.packedPosition);
        Toast.makeText(this,title + ": Child " + childPos + " clicked in                    group " + groupPos, Toast.LENGTH_SHORT).show();
        return true;
    } else if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
        int groupPos = ExpandableListView
                .getPackedPositionGroup(info.packedPosition);
        Toast.makeText(this, title + ": Group " + groupPos + " clicked",
                Toast.LENGTH_SHORT).show();
        return true;
    }
    return false;
}

public class MyExpandableListAdapter extends BaseExpandableListAdapter {
    Context mContext;

    public MyExpandableListAdapter() {
        // TODO Auto-generated constructor stub
    }

    public MyExpandableListAdapter(Context context) {
        mContext = context;

    }

    public Object getChild(int groupPosition, int childPosition) {
        Log.i(TAG, "getChild");
        return children[groupPosition][childPosition];
    }

    public long getChildId(int groupPosition, int childPosition) {
        Log.i(TAG, "getChildId");
        return childPosition;
    }

    public int getChildrenCount(int groupPosition) {
        Log.i(TAG, "getChildId");
        return children[groupPosition].length;
    }

    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        Log.i(TAG, "getChildView");

        LayoutInflater layoutInflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = null;

        TextView tt = null;
        if (groupPosition == 4) {
            v = layoutInflater.inflate(R.layout.button_group, null);
        } else {
            v = layoutInflater.inflate(R.layout.album_row, null);
            tt = (TextView) v.findViewById(R.id.text1);

            String myText = this.getChild(groupPosition, childPosition)
                    .toString();

            tt.setText(myText);
            CheckBox cb = (CheckBox) v.findViewById(R.id.checkbox);
            cb.setVisibility(View.VISIBLE);

            if (groupPosition == 0) {
                ImageView icon = (ImageView) v.findViewById(R.id.rowicon);
                icon.setImageResource(R.drawable.add_picture);
            } else {
                ImageView icon = (ImageView) v.findViewById(R.id.rowicon);
                icon.setImageResource(R.drawable.add_picture);

            }
        }

                    return v;
    }

    public Object getGroup(int groupPosition) {
        Log.i(TAG, "getGroup");
        return groups[groupPosition];
    }

    public int getGroupCount() {
        Log.i(TAG, "getGroupCount");
        return groups.length;
    }

    public long getGroupId(int groupPosition) {
        Log.i(TAG, "getGroupId");
        return groupPosition;
    }

    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        TextView groupTitle = null;
        ImageView imgDot;
        LayoutInflater layoutInflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = null;
        v = layoutInflater.inflate(R.layout.button_group, null);
        groupTitle = (TextView) v.findViewById(R.id.TextView01);
        String myText = this.getGroup(groupPosition).toString();
        groupTitle.setText(myText);
        imgDot = (ImageView) v.findViewById(R.id.ImageView01);
        imgDot.setVisibility(View.VISIBLE);
        return v;
    }
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        Log.i(TAG, "isChildSelectable");
        return true;
    }
    public boolean hasStableIds() {
        Log.i(TAG, "hasStableIds");
        return true;
    }
    public void registerDataSetObserver(DataSetObserver observer) {
    }
} // closing of MyExpandableListAdapter 
} // closing of MyExpadableListActivity
我的xml视图如下所示

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   
android:layout_width="fill_parent" android:layout_height="wrap_content" 
android:minHeight="?android:attr/listPreferredItemHeight" 
android:gravity="center_vertical" 
android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft" 
android:paddingRight="?android:attr/scrollbarSize"
android:layout_weight="1" android:background="#fafafa">
<ImageView android:id="@+id/rowicon" android:layout_width="wrap_content"  
android:layout_height="fill_parent" android:layout_marginRight="6dip" /> <TextView   
android:id="@+id/text1"
android:layout_width="wrap_content" android:layout_height="wrap_content" 
android:layout_weight="1"
/>
<CheckBox android:id="@+id/checkbox" android:layout_width="wrap_content" 
android:layout_height="wrap_content" android:layout_marginRight="2dip" 
android:focusable="true" android:clickable="true" android:gravity="center_vertical" 
android:orientation="vertical" android:duplicateParentState="true" 
android:visibility="visible"
android:text="myTest"
/> </LinearLayout> 



您忘记了register
setOnChildClickListener(…)
,它无法运行
onChildClick()
方法

编辑1:只需在
onCreate
方法中添加一行:

@Override
public void onCreate(Bundle savedInstanceState) {       
    super.onCreate(savedInstanceState);                 
    mAdapter = new MyExpandableListAdapter(this);
    setListAdapter(mAdapter);
    getExpandableListView().setOnChildClickListener(this); //This should fix issue.
    registerForContextMenu(getExpandableListView());   //ContextMenu event override onChildClick and onGroupClick. Remove this line    
}
编辑2:尝试链接。如果对您没有帮助,我建议您不要使用BaseExpandableListAdapter。使用SimpleExpandableListAdapter,重写4个方法:getChildView()、getGroupView()、getChildrenCount()和getGroupCount()。一切都应该是好的


别忘了写评论。如果这没有帮助,我将尝试寻找更多的想法。

BaseExpandableListAdapter
中有一个函数:
公共布尔值是ChildSelectable(int-groupPosition,int-childPosition)
。此函数返回默认值false。将其更改为:returntrue。您的onChildClickListener将被调用

我曾尝试在Kotlin中使用BaseExpandableListAdapter实现Android ExpandableListView

我没有发布布局的XML代码

科特林的主要活动:

import android.app.Activity
import android.os.Bundle
import android.widget.ExpandableListAdapter
import android.widget.ExpandableListView
import java.util.ArrayList

class expandable_sticky_kotlin: Activity() {
     var listAdapter: ExpandableListAdapter? = null
     var exp_view: ExpandableListView? = null
     var list: MutableList<String>? = null
     val childList:HashMap<String,List<String>>?=null

    override fun onCreate(savedInstanceState: Bundle?)
    {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_expandable_sticky_header)
        reloadData()
        exp_view = findViewById(R.id.lvExp)
        listAdapter = ExpandableAda(this,list,childList)
        exp_view!!.setAdapter(listAdapter)

    }

    fun reloadData()
    {
        list!!.add("top250")
        list!!.add("nowShowing")
        list!!.add("comingSoon")

        // Adding child data
        val top250 = ArrayList<String>()
        top250.add("The Shawshank Redemption")
        top250.add("The Godfather")
        top250.add("The Godfather: Part II")
        top250.add("Pulp Fiction")
        top250.add("The Good, the Bad and the Ugly")
        top250.add("The Dark Knight")
        top250.add("12 Angry Men")
        top250.add("Dark knight")
        top250.add("POTC")

        val nowShowing = ArrayList<String>()
        nowShowing.add("The Conjuring")
        nowShowing.add("Despicable Me 2")
        nowShowing.add("Turbo")
        nowShowing.add("Grown Ups 2")
        nowShowing.add("Red 2")
        nowShowing.add("The Wolverine")
        nowShowing.add("Deadpool2")
        nowShowing.add("Raazi")


        val comingSoon = ArrayList<String>()
        comingSoon.add("2 Guns")
        comingSoon.add("The Smurfs 2")
        comingSoon.add("The Spectacular Now")
        comingSoon.add("The Canyons")
        comingSoon.add("Europa Report")
        comingSoon.add("Avengers4")
        comingSoon.add("Meri apni movie")

        childList!!.put(list!!.get(0), top250) // Header, Child data
        childList!!.put(list!!.get(1), nowShowing)
        childList!!.put(list!!.get(2), comingSoon)

    }
}
导入android.app.Activity
导入android.os.Bundle
导入android.widget.ExpandableListAdapter
导入android.widget.ExpandableListView
导入java.util.ArrayList
类可扩展\u粘性\u kotlin:Activity(){
变量listAdapter:ExpandableListAdapter?=null
var exp_视图:ExpandableListView?=null
变量列表:可变列表?=null
val childList:HashMap?=null
重写创建时的乐趣(savedInstanceState:Bundle?)
{
super.onCreate(savedInstanceState)
setContentView(R.layout.activity\u可扩展\u粘性\u标题)
重新加载数据()
exp_view=findviewbyd(R.id.lvExp)
listAdapter=ExpandableAda(此、列表、子列表)
exp\u视图!!.setAdapter(listAdapter)
}
乐趣重新加载数据()
{
列表!!.add(“top250”)
列表!!.add(“正在显示”)
列表!!.add(“即将到来”)
//添加子数据
val top250=ArrayList()
top250.添加(“肖申克的救赎”)
添加(“教父”)
top250.添加(“教父:第二部分”)
top250.添加(“低俗小说”)
添加(“好的、坏的和丑陋的”)
top250.添加(“黑暗骑士”)
top250.添加(“12个愤怒的男人”)
top250.添加(“黑暗骑士”)
top250.添加(“POTC”)
val nowshiving=ArrayList()
现在显示。添加(“魔术师”)
现在显示。添加(“卑鄙的我2”)
现在显示。添加(“Turbo”)
现在显示。添加(“成年人2”)
现在显示。添加(“红色2”)
现在显示。添加(“狼獾”)
现在显示。添加(“死池2”)
现在显示。添加(“Raazi”)
val comingSoon=ArrayList()
很快就来。添加(“2支枪”)
即将到来。添加(“蓝精灵2”)
即将到来。添加(“壮观的现在”)
即将到来。添加(“峡谷”)
即将发布。添加(“欧罗巴报告”)
即将到来。添加(“复仇者4”)
即将上映。添加(“Meri apni电影”)
childList!!.put(list!!.get(0),top250)//头,子数据
childList!!.put(list!!.get(1),现在显示)
childList!!.put(list!!.get(2),马上就来)
}
}
ExpandableView的适配器:

import android.content.Context
import android.database.DataSetObserver
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseExpandableListAdapter
import android.widget.ExpandableListAdapter
import android.widget.TextView
import java.util.concurrent.ConcurrentLinkedQueue

class ExpandableAda(val context:Context,val list:List<String>?,val Child:HashMap<String,List<String>>?) : BaseExpandableListAdapter()
{


    var mContext = context
       var mparent_list = list
    var mChild = Child


    override fun onGroupCollapsed(p0: Int) {
        println("collpased")
    }
    override fun isEmpty(): Boolean {
        return false
    }

    override fun registerDataSetObserver(p0: DataSetObserver?)
    {
        println("here")
        val observers = ConcurrentLinkedQueue<DataSetObserver>()
        println("here")
        if (p0!=null) {
            observers.add(p0)
            println("here")
        }
    }

    override fun getChild(p0: Int, p1: Int): Any {
        return mChild!!.get(mparent_list!!.get(p0))!!.get(p1)
    }

    override fun onGroupExpanded(p0: Int) {
       print("Expanded")
    }

    override fun getCombinedChildId(p0: Long, p1: Long): Long
    {
        return p0
    }

    override fun getGroupId(p0: Int): Long {

        return p0.toLong()
    }

    override fun isChildSelectable(p0: Int, p1: Int): Boolean {
        return true
    }

    override fun hasStableIds(): Boolean {
        return true
    }

    override fun getChildView(p0: Int, p1: Int, p2: Boolean, p3: View?, p4: ViewGroup?): View {
        var title:String = getChild(p0,p1) as String
        var myTest:View = p4!!


            var infalInflater = this.mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
            myTest = infalInflater.inflate(R.layout.list_item, null)
        var lblListItem:TextView = myTest!!.findViewById(R.id.lblListItem)
        lblListItem.text = title
        return myTest
    }

    override fun areAllItemsEnabled(): Boolean {
        return true
    }

    override fun getChildId(p0: Int, p1: Int): Long {
        return p1.toLong()//To change body of created functions use File | Settings | File Templates.
    }

    override fun getCombinedGroupId(p0: Long): Long {
        return p0
    }

    override fun getGroupView(p0: Int, p1: Boolean, p2: View?, p3: ViewGroup?): View {
    var title:String = mparent_list!!.get(p0)
        var myTest:View = p3!!

            var inflater = this.mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        myTest = inflater.inflate(R.layout.list_group, null)
          var lblListHeader:TextView = myTest!!.findViewById(R.id.lblListHeader)

        lblListHeader.text = title
        return myTest!!
    }

    override fun unregisterDataSetObserver(p0: DataSetObserver?) {
        val observers = ConcurrentLinkedQueue<DataSetObserver>()
        if (p0!=null)
        {
            observers.remove(p0)
        }
    }

    override fun getGroupCount(): Int {

        return mparent_list!!.size
    }

    override fun getGroup(p0: Int): Any {
        return mparent_list!!.get(p0)
    }

    override fun getChildrenCount(p0: Int): Int {
        return mChild!!.get(mparent_list!!.get(p0))!!.size
    }

}
导入android.content.Context
导入android.database.DataSetObserver
导入android.view.LayoutInflater
导入android.view.view
导入android.view.ViewGroup
导入android.widget.BaseExpandableListAdapter
导入android.widget.ExpandableListAdapter
导入android.widget.TextView
导入java.util.concurrent.ConcurrentLinkedQueue
类ExpandableAda(val-context:context,val-list:list?,val-Child:HashMap?):BaseExpandableListAdapter()
{
var mContext=上下文
var mparent_list=list
var mChild=Child
覆盖已折叠的组(p0:Int){
println(“collpased”)
}
override fun isEmpty():布尔值{
返回错误
}
重写fun registerDataSetObserver(p0:DataSetObserver?)
{
println(“此处”)
val观察员=ConcurrentLinkedQueue()
println(“此处”)
如果(p0!=null){
观察员:增加(p0)
println(“此处”)
}
}
覆盖fun getChild(p0:Int,p1:Int):任意{
返回mChild!!.get(mparent\u list!!.get(p0))!!.get(p1)
}
覆盖已扩展的组(p0:Int){
打印(“扩展”)
}
覆盖有趣的getCombinedChildId(p0:Long,p1:Long):Long
{
返回p0
}
覆盖有趣的getGroupId(p0:Int):长{
返回p0.toLong()
}
覆盖乐趣是可选择的(p0:Int,p1:Int):布尔值{
返回真值
}
重写表ID():布尔值{
返回真值
}
覆盖趣味getChildView(p0:Int,p1:Int,p2:Boolean,p3:View?,p4:ViewGroup?):视图{
变量标题:String=getChild(p0,p1)作为字符串
var myTest:View=p4!!
var infalInflater=this.mContext
.getSystemService(Context.LAYOUT\u INFLATER\u SERVICE)作为LayoutInflater
myTest=infalInflater.充气(R.layout.list_项,空)
var lblListItem:TextView=myTest!!.findViewById(R.id.lblListItem)
import android.content.Context
import android.database.DataSetObserver
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseExpandableListAdapter
import android.widget.ExpandableListAdapter
import android.widget.TextView
import java.util.concurrent.ConcurrentLinkedQueue

class ExpandableAda(val context:Context,val list:List<String>?,val Child:HashMap<String,List<String>>?) : BaseExpandableListAdapter()
{


    var mContext = context
       var mparent_list = list
    var mChild = Child


    override fun onGroupCollapsed(p0: Int) {
        println("collpased")
    }
    override fun isEmpty(): Boolean {
        return false
    }

    override fun registerDataSetObserver(p0: DataSetObserver?)
    {
        println("here")
        val observers = ConcurrentLinkedQueue<DataSetObserver>()
        println("here")
        if (p0!=null) {
            observers.add(p0)
            println("here")
        }
    }

    override fun getChild(p0: Int, p1: Int): Any {
        return mChild!!.get(mparent_list!!.get(p0))!!.get(p1)
    }

    override fun onGroupExpanded(p0: Int) {
       print("Expanded")
    }

    override fun getCombinedChildId(p0: Long, p1: Long): Long
    {
        return p0
    }

    override fun getGroupId(p0: Int): Long {

        return p0.toLong()
    }

    override fun isChildSelectable(p0: Int, p1: Int): Boolean {
        return true
    }

    override fun hasStableIds(): Boolean {
        return true
    }

    override fun getChildView(p0: Int, p1: Int, p2: Boolean, p3: View?, p4: ViewGroup?): View {
        var title:String = getChild(p0,p1) as String
        var myTest:View = p4!!


            var infalInflater = this.mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
            myTest = infalInflater.inflate(R.layout.list_item, null)
        var lblListItem:TextView = myTest!!.findViewById(R.id.lblListItem)
        lblListItem.text = title
        return myTest
    }

    override fun areAllItemsEnabled(): Boolean {
        return true
    }

    override fun getChildId(p0: Int, p1: Int): Long {
        return p1.toLong()//To change body of created functions use File | Settings | File Templates.
    }

    override fun getCombinedGroupId(p0: Long): Long {
        return p0
    }

    override fun getGroupView(p0: Int, p1: Boolean, p2: View?, p3: ViewGroup?): View {
    var title:String = mparent_list!!.get(p0)
        var myTest:View = p3!!

            var inflater = this.mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        myTest = inflater.inflate(R.layout.list_group, null)
          var lblListHeader:TextView = myTest!!.findViewById(R.id.lblListHeader)

        lblListHeader.text = title
        return myTest!!
    }

    override fun unregisterDataSetObserver(p0: DataSetObserver?) {
        val observers = ConcurrentLinkedQueue<DataSetObserver>()
        if (p0!=null)
        {
            observers.remove(p0)
        }
    }

    override fun getGroupCount(): Int {

        return mparent_list!!.size
    }

    override fun getGroup(p0: Int): Any {
        return mparent_list!!.get(p0)
    }

    override fun getChildrenCount(p0: Int): Int {
        return mChild!!.get(mparent_list!!.get(p0))!!.size
    }

}