点击监听器就像onchildclick,onitemlongclick没有在列表视图扩展,除了GroupClickListener工作? [英] Click Listener like onchildclick, onitemlongclick is not Working in Expandable Listview except GroupClickListener?

查看:131
本文介绍了点击监听器就像onchildclick,onitemlongclick没有在列表视图扩展,除了GroupClickListener工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

喜的朋友,我在扩展列表视图的工作...

Hi friends I am working in expandable listview ...

我取从数据库中显示它很好的价值,
但是,没有监听器是在它的工作
下面是我的源......

I fetch values from database it display very well, But no listener were working in it below is My source ......

MainActivity.java

MainActivity.java

package shenhengbin.practice;

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

import shenhengbin.practice.entity.Constants;
import shenhengbin.practice.entity.DBSQLite;
import shenhengbin.practice.entity.GroupEntity;
import shenhengbin.practice.entity.GroupEntity.GroupItemEntity;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.SimpleCursorTreeAdapter;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ListView;

public class MainActivity extends Activity {

    private ExpandableListView  mExpandableListView;
    private ListView            listView;
    private List<GroupEntity>   mGroupCollection;
    private BaseExpandableListAdapter adapter ;



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

        init();
        setupDefaults();
        setupEvents();


    }


    private void init() {
        listView=(ListView) findViewById(R.id.listView);
        mExpandableListView=(ExpandableListView) findViewById(R.id.expandableListView);
    }

    private void setupDefaults() {
        prepareResource();
    }

    private void setupEvents() {
        adapter =new ExpandableListAdapter  (this,mExpandableListView, mGroupCollection);
        mExpandableListView.setAdapter(adapter);
        mExpandableListView.setOnChildClickListener(new OnChildClickListener() {

            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                    int groupPosition, int childPosition, long id) {
                Log.e("child","clicked");
                return false;
            }
        });
        mExpandableListView.setOnItemLongClickListener(new OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                Log.e("Long","Click ENabled");
                return false;
            }
        });
    }

    private void prepareResource() {
        mGroupCollection = new ArrayList<GroupEntity>();
        DBSQLite.openDatabase(getApplicationContext());
        Cursor favoriteResultSet=Constants.SQLITE_CONNECTION.select("select * from names");
        if(favoriteResultSet.getCount()>0)
        {while(favoriteResultSet.moveToNext()){
            String name     =favoriteResultSet.getString(favoriteResultSet.getColumnIndex("names"));
            Log.e("google_",""+name);
            GroupEntity ge = new GroupEntity();
            ge.Name = name;
            Cursor favoriteResultSetChild=Constants.SQLITE_CONNECTION.select("select friends from friends where names ='"+name+"';");
            if(favoriteResultSetChild.getCount()>0)
            {while(favoriteResultSetChild.moveToNext()){
                String childName        =favoriteResultSetChild.getString(favoriteResultSetChild.getColumnIndex("friends"));
                Log.e("google_child",""+childName);
                GroupItemEntity gi = ge.new GroupItemEntity();
                gi.Name = childName;
                ge.GroupItemCollection.add(gi);
            }}
            favoriteResultSetChild.close();
            mGroupCollection.add(ge);
        }}
        favoriteResultSet.close();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        DBSQLite.closeDatabase();
    }
}

ExpandableListAdapter.java

ExpandableListAdapter.java

package shenhengbin.practice;

import java.util.List;

import shenhengbin.practice.entity.GroupEntity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.ImageView;
import android.widget.TextView;

public class ExpandableListAdapter extends BaseExpandableListAdapter {

    private Context                 mContext;
    private ExpandableListView      mExpandableListView;
    private List<GroupEntity>       mGroupCollection;
    private int[]                   groupStatus;

    public ExpandableListAdapter(Context pContext,ExpandableListView pExpandableListView,List<GroupEntity> pGroupCollection) {
        mContext                =   pContext;
        mGroupCollection        =   pGroupCollection;
        mExpandableListView     =   pExpandableListView;
        groupStatus             =   new int[mGroupCollection.size()];

        setListEvent();
    }

    private void setListEvent() {

        mExpandableListView.setOnGroupExpandListener(new OnGroupExpandListener() {

            @Override
            public void onGroupExpand(int arg0) {
                groupStatus[arg0] = 1;
            }});

        mExpandableListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {

            @Override
            public void onGroupCollapse(int arg0) {
                groupStatus[arg0] = 0;
            }});
    }

    @Override
    public String getChild(int arg0, int arg1) {
        return mGroupCollection.get(arg0).GroupItemCollection.get(arg1).Name;
    }

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

    @Override
    public int getChildType(int groupPosition, int childPosition) {
        return super.getChildType(groupPosition, childPosition);
    }

    @Override
    public long getCombinedChildId(long groupId, long childId) {
        return super.getCombinedChildId(groupId, childId);
    }

    @Override
    public long getCombinedGroupId(long groupId) {
        return super.getCombinedGroupId(groupId);
    }

    @Override
    public View getChildView(int arg0, int arg1, boolean arg2, View arg3,
            ViewGroup arg4) {
        ChildHolder childHolder;
        if (arg3 == null) {
            arg3 = LayoutInflater.from(mContext).inflate(R.layout.list_group_item, null);
            childHolder = new ChildHolder();
            childHolder.title = (TextView) arg3.findViewById(R.id.item_title);
            arg3.setTag(childHolder);
        }
        else {childHolder = (ChildHolder) arg3.getTag();}

        childHolder.title.setText(mGroupCollection.get(arg0).GroupItemCollection.get(arg1).Name);
        return arg3;
    }

    @Override
    public int getChildrenCount(int arg0) {

        return mGroupCollection.get(arg0).GroupItemCollection.size();
    }

    @Override
    public Object getGroup(int arg0) {

        return mGroupCollection.get(arg0);
    }

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

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

    @Override
    public View getGroupView(int arg0, boolean arg1, View arg2, ViewGroup arg3) {
        GroupHolder groupHolder;
        if (arg2 == null) {

            arg2 = LayoutInflater.from(mContext).inflate(R.layout.list_group,null);
            groupHolder = new GroupHolder();
            groupHolder.img = (ImageView) arg2.findViewById(R.id.tag_img);
            groupHolder.title = (TextView) arg2.findViewById(R.id.group_title);
            arg2.setTag(groupHolder);

        } 

        else {
            groupHolder = (GroupHolder) arg2.getTag();
        }

        if (groupStatus[arg0] == 0) {
            groupHolder.img.setImageResource(R.drawable.group_down);
        } 
        else
        {
            groupHolder.img.setImageResource(R.drawable.group_up);
        }
        groupHolder.title.setText(mGroupCollection.get(arg0).Name);
        return arg2;
    }

    class GroupHolder {
        ImageView img;
        TextView title;
    }

    class ChildHolder {
        TextView title;
    }

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

    @Override
    public boolean isChildSelectable(int arg0, int arg1) {
        return false;
    }

}

帮助我。
如何启用扩展列表视图中的所有点击听众。
先谢谢了。

Help me. How to enable all the click listeners in expandable list view . Thanks in advance.

推荐答案

如果你正在谈论的是OnChildClickListener听众,问题就在这里:

If the listener you are talking about is OnChildClickListener, the problem is here:

@Override
    public boolean isChildSelectable(int arg0, int arg1) {
        return false;
    }

它需要

@Override
        public boolean isChildSelectable(int arg0, int arg1) {
            return true;
        }

这篇关于点击监听器就像onchildclick,onitemlongclick没有在列表视图扩展,除了GroupClickListener工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆