ExpandableListView中的onClick子项

问题描述 投票:1回答:1

我需要将onClick添加到ExpandableListView中的子项。我已经回顾了其他有关此问题的帖子,但我无法将代码集成到我的中,可能是由于ExpandableListView代码的不同变化。

如果你能提供一些代码解释,那将是很棒的。非常感谢。

这是我的源代码:

activity_main.xml中

`<?xml version="1.0" encoding="utf-8"?>
RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<ExpandableListView
    android:id="@+id/expLV"
    android:layout_width="match_parent"
    android:layout_height="match_parent"></ExpandableListView>


 </RelativeLayout>`

list_parent.xml

<?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="wrap_content"
android:orientation="vertical">

<TextView
    android:id="@+id/listP"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="? 
android:attr/expandableListPreferredItemPaddingLeft"
    android:textSize="20dp"
    android:paddingTop="20dp"
    android:paddingBottom="20dp"
    />


</LinearLayout>

list_child.xml

<?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/listC"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="? 
android:attr/expandableListPreferredChildPaddingLeft"
    android:textSize="14dp"
    android:paddingBottom="20dp"
    android:paddingTop="20dp"/>

</LinearLayout>

expandable list adapter.Java

package com.example.ehsan.myexplistview;

import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

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

public class ExpandableListAdapter extends BaseExpandableListAdapter {

private Context context;
private List<String> listDataHeader;
private HashMap<String, List<String>> listHashMap;

public ExpandableListAdapter(Context context, List<String> listDataHeader, HashMap<String, List<String>> listHashMap) {
    this.context = context;
    this.listDataHeader = listDataHeader;
    this.listHashMap = listHashMap;
}

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

@Override
public int getChildrenCount(int i) {
    return listHashMap.get(listDataHeader.get(i)).size();
}

@Override
public Object getGroup(int i) {
    return listDataHeader.get(i);
}

@Override
public Object getChild(int i, int i1) {
    return listHashMap.get(listDataHeader.get(i)).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 false;
}

@Override
public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
    String parentText = (String)getGroup(i);
    if (view == null)
    {
        LayoutInflater inflater = (LayoutInflater)this.context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
        view=inflater.inflate(R.layout.list_parent, null);
    }
    TextView listP = (TextView)view.findViewById(R.id.listP);
    listP.setTypeface(null, Typeface.BOLD);
    listP.setText(parentText);
    return view;
}

@Override
public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
  final String childText = (String)getChild(i,i1);
  if (view == null)
  {
      LayoutInflater inflater = (LayoutInflater)this.context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
      view=inflater.inflate(R.layout.list_child, null);
  }
    TextView listC = (TextView)view.findViewById(R.id.listC);
    listC.setText(childText);

     view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(i1==0){
                Intent intent = new Intent(activity,OneTwoThree.class);
                activity.startActivity(intent);
            }
            else if (i1 ==1){
                Intent intent = new Intent(activity,FourFiveSix.class);
                activity.startActivity(intent);
            }
            else if (i1 ==2){
                Intent intent = new Intent(activity,SevenEightNine.class);
                activity.startActivity(intent);}
            else if (i1 ==3){
                Intent intent = new Intent(activity,TenElevenTwelve.class);
                activity.startActivity(intent);}
        }
    });
    return view;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}
}

main activity.Java

package com.example.ehsan.myexplistview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ExpandableListView;

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

public class MainActivity extends AppCompatActivity {

private ExpandableListView expandableListView;
private ExpandableListAdapter expandableListAdapter;
private List<String> listP;
private HashMap<String, List<String>> listC;

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

    expandableListView = (ExpandableListView)findViewById(R.id.expLV);
    initData();
    expandableListAdapter = new ExpandableListAdapter(this, listP, listC);
    expandableListView.setAdapter(expandableListAdapter);
}

private void initData(){
listP = new ArrayList<>();
listC = new HashMap<>();

    listP.add("ABC");
    listP.add("DEF");
    listP.add("GHI");
    listP.add("JKL");

List <String> abc = new ArrayList<>();
abc.add("123");

List <String> def = new ArrayList<>();
def.add("456");
def.add("789");

List <String> ghi = new ArrayList<>();
ghi.add("101112");
ghi.add("131415");
ghi.add("161718");

List <String> jkl = new ArrayList<>();
jkl.add("192021");
jkl.add("222324");
jkl.add("252627");
jkl.add("282930");

listC.put(listP.get(0),abc);
listC.put(listP.get(1),def);
listC.put(listP.get(2),ghi);
listC.put(listP.get(3),jkl);
}
}
java android onclick expandablelistview
1个回答
1
投票

您可以通过两种方式设置可扩展列表的子单击

1.在getChildView()方法中编写子点击事件。

@Override
public View getChildView(final int groupPosition, final int childPosition,
        boolean isLastChild, View convertView, ViewGroup parent) {
    Page page =(Page) getChild(groupPosition, childPosition);
    convertView = inflater.inflate(R.layout.child_list_layout, null);
    Button mButton=(Button)convertView.findViewById(R.id.button1);
     mButton.setOnClickListener(new OnClickListener() {    
        @Override
        public void onClick(View v) {    
                Your code goes here ....      
        }
    });

    return convertView;
}  

2.直接从可扩展列表视图中单击。

        mExpandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {

                    @Override
                    public boolean onChildClick(
                            ExpandableListView parent, View v,
                            int groupPosition, int childPosition,
                            long id) {
                        GoCategory(mainMenusList.get(groupPosition)
                                .getPagesList().get(childPosition));
                        return false;
                    }
                });
© www.soinside.com 2019 - 2024. All rights reserved.