Android的ExpandableListVIew使用BaseExpandableListAdapterAndroid、ExpandableListVIew、BaseExpandableListAd

2023-09-06 05:18:56 作者:文艺的七字大全

我使用的扩展MyExpandableListAdapterBaseExpandableListAdapter和我实现BaseExpandableListAdapter的所有方法在我的适配器,我创建一个扩展ExpandableListActivity MyExpadableListActivity。MyExpandableListAdapter是内部类MyExpadableListActivity的。我使用添加童车和D组UI(过去在下面),一个XML视图。当我运行的应用程序的显示效果细腻。我的问题是这样的这个我重写ExpandableListActivity的方法onChildClick(....)在MyExpadableListActivity。当我运行我的code就不能去onChildClick方法。我怎么能实现使用这种状况,否则建议onChildClick(...)方法..希望有人能指出我什么我做错了....是如果有人点我怎么能实现的解决方案表示感谢。 。

I am using MyExpandableListAdapter that extends BaseExpandableListAdapter and I implement all methods of BaseExpandableListAdapter in my adapter,I create MyExpadableListActivity that extends ExpandableListActivity. MyExpandableListAdapter is the inner class of MyExpadableListActivity. I am using a xml views for adding childs and groupd UI(past in below). As I run application its display fine. My problem is this this I override the method onChildClick (....) of ExpandableListActivity in MyExpadableListActivity .when I run my code it can't go for onChildClick method . how I can implement onChildClick (...) method using this situation or else suggestion .. Hope someone point me what I am doing wrong .... Be grateful for if anyone point how i can achieve solution . .

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观点如下

My xml views are as below

<?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> 

<?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">

    

推荐答案

BaseExpandableListAdapter 是一个函数:公共布尔isChildSelectable(INT groupPosition, INT childPosition)。该函数返回默认为false。将其更改为:返回true。和你的onChildClickListener将被称为!

In the BaseExpandableListAdapter is one function: public boolean isChildSelectable(int groupPosition, int childPosition). This function returns Default false. Change it to: return true. And your onChildClickListener will be called !