ExpandableListView中的onClick子项

时间:2018-09-18 05:20:27

标签: java android onclick expandablelistview

我需要将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>

ExpandableListAdapter.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;
}
}

MainActivity.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);
}
}

1 个答案:

答案 0 :(得分:0)

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

{。{1}}方法内的1.write子点击事件。

getChildView()

2.write直接从展开式列表视图中单击。

@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;
}