Android 自定义ExpandableListView不工作
我创建了自定义Android 自定义ExpandableListView不工作,android,expandablelistview,Android,Expandablelistview,我创建了自定义ExpandableListView,但单击它时它不会展开 活动\u main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" a
ExpandableListView
,但单击它时它不会展开
活动\u main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ExpandableListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/exp_lv"
></ExpandableListView>
<?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="horizontal" >
<TextView
android:layout_width="270sp"
android:layout_height="30sp"
android:layout_marginLeft="30sp"
android:id="@+id/title"
android:textIsSelectable="true"
/>
<CheckBox
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/chk"
/>
<?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:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/child"
android:textIsSelectable="true"
/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="120sp"
android:layout_height="wrap_content"
android:textIsSelectable="true"
android:id="@+id/dd"
android:textStyle="bold"
/>
<TextView
android:layout_width="200sp"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/dd"
android:textIsSelectable="true"
android:id="@+id/date"
/>
</RelativeLayout>
MyBaseAdapter.java
package com.example.expandablelistview;
import java.util.ArrayList;
public class group {
private String Name;
private boolean isSelected;
private ArrayList<child> Items;
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public boolean isSelected() {
return isSelected;
}
public void setSelected(boolean isSelected) {
this.isSelected = isSelected;
}
public ArrayList<child> getItems() {
return Items;
}
public void setItems(ArrayList<child> items) {
Items = items;
}
}
package com.example.expandablelistview;
public class child {
String child_title;
String dd;
String date;
public String getChild_title() {
return child_title;
}
public void setChild_title(String child_title) {
this.child_title = child_title;
}
public String getDd() {
return dd;
}
public void setDd(String dd) {
this.dd = dd;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
}
package com.example.expandablelistview;
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.TextView;
public class MyBaseAdapter extends BaseExpandableListAdapter{
Context context;
ArrayList<group> group_al;
public MyBaseAdapter(Context context,ArrayList<group> group_al) {
this.context=context;
this.group_al=group_al;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
ArrayList<child> chList = group_al.get(groupPosition).getItems();
return chList.get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
ViewGroup parent) {
child ch = (child) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.child_items, null);
}
TextView child = (TextView) convertView.findViewById(R.id.child);
TextView dd = (TextView) convertView.findViewById(R.id.dd);
TextView date= (TextView) convertView.findViewById(R.id.date);
child.setText(ch.getChild_title().toString());
dd.setText(ch.getChild_title().toString());
date.setText(ch.getChild_title().toString());
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
ArrayList<child> chList = group_al.get(groupPosition).getItems();
return chList.size();
}
@Override
public Object getGroup(int groupPosition) {
return group_al.get(groupPosition);
}
@Override
public int getGroupCount() {
return group_al.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
group gr = (group) getGroup(groupPosition);
long group_id = getGroupId(groupPosition);
if (convertView == null) {
LayoutInflater inf = (LayoutInflater) context
.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = inf.inflate(R.layout.group_items, null);
}
TextView title = (TextView) convertView.findViewById(R.id.title);
title.setText(gr.getName());
CheckBox chk=(CheckBox) convertView.findViewById(R.id.chk);
chk.setFocusable(false);
return convertView;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int arg0, int arg1) {
return false;
}
}
package com.example.expandablelistview;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.widget.ExpandableListView;
public class MainActivity extends Activity {
private MyBaseAdapter ExpAdapter;
private ArrayList<group> ExpListItems;
private ExpandableListView ExpandList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ExpandList = (ExpandableListView) findViewById(R.id.exp_lv);
ExpListItems = SetStandardGroups();
ExpAdapter = new MyBaseAdapter(MainActivity.this, ExpListItems);
ExpandList.setAdapter(ExpAdapter);
}
private ArrayList<group> SetStandardGroups() {
ArrayList<group> list = new ArrayList<group>();
group gru= new group();
gru.setName("Tax 1");
ArrayList<child> ch_list= new ArrayList<child>();
child ch = new child();
ch.setChild_title("Information of tax 1");
ch.setDd("Due Date:");
ch.setDate("22/02/2016");
ch_list.add(ch);
gru.setItems(ch_list);
list.add(gru);
return list;
}
}
package com.example.expandablelistview;
导入java.util.ArrayList;
导入android.content.Context;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.BaseExpandableListAdapter;
导入android.widget.CheckBox;
导入android.widget.TextView;
公共类MyBaseAdapter扩展了BaseExpandableListAdapter{
语境;
ArrayList group_al;
公共MyBaseAdapter(上下文,ArrayList组){
this.context=context;
this.group_al=group_al;
}
@凌驾
公共对象getChild(int-groupPosition,int-childPosition){
ArrayList chList=group_al.get(groupPosition.getItems();
返回chList.get(childPosition);
}
@凌驾
公共长getChildId(int-groupPosition,int-childPosition){
返回子位置;
}
@凌驾
公共视图getChildView(int groupPosition、int childPosition、boolean isLastChild、View convertView、,
视图组(父级){
child ch=(child)getChild(groupPosition,childPosition);
if(convertView==null){
LayoutInflater infalInflater=(LayoutInflater)上下文
.getSystemService(上下文布局\充气机\服务);
convertView=infalInflater.flate(R.layout.child_项,空);
}
TextView child=(TextView)convertView.findViewById(R.id.child);
TextView dd=(TextView)convertView.findViewById(R.id.dd);
TextView日期=(TextView)convertView.findViewById(R.id.date);
setText(ch.getChild_title().toString());
dd.setText(ch.getChild_title().toString());
date.setText(ch.getChild_title().toString());
返回视图;
}
@凌驾
公共整数getChildrenCount(整数组位置){
ArrayList chList=group_al.get(groupPosition.getItems();
返回chList.size();
}
@凌驾
公共对象getGroup(int-groupPosition){
返回组获取(groupPosition);
}
@凌驾
public int getGroupCount(){
返回组总大小();
}
@凌驾
公共长getGroupId(int-groupPosition){
返回组位置;
}
@凌驾
公共视图getGroupView(int groupPosition、布尔isExpanded、视图convertView、视图组父级){
组gr=(组)getGroup(组位置);
长组_id=getGroupId(groupPosition);
if(convertView==null){
LayoutInflater inf=(LayoutInflater)上下文
.getSystemService(上下文布局\充气机\服务);
convertView=inf.inflate(R.layout.group_项目,空);
}
TextView title=(TextView)convertView.findViewById(R.id.title);
title.setText(gr.getName());
复选框chk=(复选框)convertView.findViewById(R.id.chk);
chk.setFocusable(假);
返回视图;
}
@凌驾
公共布尔表ID(){
返回true;
}
@凌驾
公共布尔值isChildSelectable(int arg0,int arg1){
返回false;
}
}
MainActivity.java
package com.example.expandablelistview;
import java.util.ArrayList;
public class group {
private String Name;
private boolean isSelected;
private ArrayList<child> Items;
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public boolean isSelected() {
return isSelected;
}
public void setSelected(boolean isSelected) {
this.isSelected = isSelected;
}
public ArrayList<child> getItems() {
return Items;
}
public void setItems(ArrayList<child> items) {
Items = items;
}
}
package com.example.expandablelistview;
public class child {
String child_title;
String dd;
String date;
public String getChild_title() {
return child_title;
}
public void setChild_title(String child_title) {
this.child_title = child_title;
}
public String getDd() {
return dd;
}
public void setDd(String dd) {
this.dd = dd;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
}
package com.example.expandablelistview;
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.TextView;
public class MyBaseAdapter extends BaseExpandableListAdapter{
Context context;
ArrayList<group> group_al;
public MyBaseAdapter(Context context,ArrayList<group> group_al) {
this.context=context;
this.group_al=group_al;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
ArrayList<child> chList = group_al.get(groupPosition).getItems();
return chList.get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
ViewGroup parent) {
child ch = (child) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.child_items, null);
}
TextView child = (TextView) convertView.findViewById(R.id.child);
TextView dd = (TextView) convertView.findViewById(R.id.dd);
TextView date= (TextView) convertView.findViewById(R.id.date);
child.setText(ch.getChild_title().toString());
dd.setText(ch.getChild_title().toString());
date.setText(ch.getChild_title().toString());
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
ArrayList<child> chList = group_al.get(groupPosition).getItems();
return chList.size();
}
@Override
public Object getGroup(int groupPosition) {
return group_al.get(groupPosition);
}
@Override
public int getGroupCount() {
return group_al.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
group gr = (group) getGroup(groupPosition);
long group_id = getGroupId(groupPosition);
if (convertView == null) {
LayoutInflater inf = (LayoutInflater) context
.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = inf.inflate(R.layout.group_items, null);
}
TextView title = (TextView) convertView.findViewById(R.id.title);
title.setText(gr.getName());
CheckBox chk=(CheckBox) convertView.findViewById(R.id.chk);
chk.setFocusable(false);
return convertView;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int arg0, int arg1) {
return false;
}
}
package com.example.expandablelistview;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.widget.ExpandableListView;
public class MainActivity extends Activity {
private MyBaseAdapter ExpAdapter;
private ArrayList<group> ExpListItems;
private ExpandableListView ExpandList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ExpandList = (ExpandableListView) findViewById(R.id.exp_lv);
ExpListItems = SetStandardGroups();
ExpAdapter = new MyBaseAdapter(MainActivity.this, ExpListItems);
ExpandList.setAdapter(ExpAdapter);
}
private ArrayList<group> SetStandardGroups() {
ArrayList<group> list = new ArrayList<group>();
group gru= new group();
gru.setName("Tax 1");
ArrayList<child> ch_list= new ArrayList<child>();
child ch = new child();
ch.setChild_title("Information of tax 1");
ch.setDd("Due Date:");
ch.setDate("22/02/2016");
ch_list.add(ch);
gru.setItems(ch_list);
list.add(gru);
return list;
}
}
package com.example.expandablelistview;
导入java.util.ArrayList;
导入android.os.Bundle;
导入android.app.Activity;
导入android.widget.ExpandableListView;
公共类MainActivity扩展了活动{
私有MyBaseAdapter;
私有ArrayList解释项;
私有可扩展列表视图可扩展列表;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ExpandList=(ExpandableListView)findViewById(R.id.exp\u lv);
ExpListItems=SetStandardGroups();
ExpAdapter=新的MyBaseAdapter(MainActivity.this,ExpListItems);
ExpandList.setAdapter(ExpAdapter);
}
专用ArrayList集合标准组(){
ArrayList=新建ArrayList();
gru组=新组();
gru.setName(“税1”);
ArrayList CHU list=新的ArrayList();
child ch=新的child();
ch.setChild_标题(“税务信息1”);
ch.setDd(“到期日:”);
总设置日期(“2016年2月22日”);
CHU列表。添加(ch);
gru.集合项目(CHU列表);
列表。添加(gru);
退货清单;
}
}
运行应用程序时,我可以看到ExpandableListView
的第一行,即旁边带有复选框的Tax1,但当我单击该行时,它不会展开
请大家帮帮我。
提前感谢。请添加:
android:focusable="false"
android:focusableInTouchMode="false"
到您的复选框
同时从textView中删除:
android:textIsSelectable="true"
**
**当我添加
android:focusable="false"
android:focusableInTouchMode="false"
到您的复选框和
android:textIsSelectable="true"
如果要查看文本,那么它工作得很好
但现在我想将选中的复选框值(仅组标题和日期文本视图的值)存储到数据库中,因此,我在复选框上添加clickListener稍等片刻,我将进行检查我尝试过,但也可扩展列表视图未扩展我必须将其设置为您的复选框OK它工作正常,但我遇到了新问题dude。。。我在子项中使用三种不同的文本视图,我想为其设置以下参数ch.setChild_title(“税务信息1”)代码>ch.setDd(“到期日:”)代码>ch.setDate(“22/02/2016”)代码>但我可以在所有三个文本视图中看到税1的信息,它们是setting child.setText(ch.getChild_title().toString());dd.setText(ch.getChild_title().toString());date.setText(ch.getChild_title().toString());所有文本查看与whyya@Zeeshan Khan相同的数据我进行了更改,但我面临新问题我在子项中使用了三个不同的文本查看,我想为其设置以下参数ch.setChild_title(“税务1信息”)代码>ch.setDd(“到期日:”)代码>ch.setDate(“22/02/2016”)代码>但我可以在所有三个文本视图中看到税1的信息您在所有子项文本视图中得到相同的值更改child.setText(ch.getChild_title().toString());dd.setText(ch.getDd().toString());date.setText(ch.getDate().toString())