[英]Android - How to add Another view under a childview in Expandable ListView in Android?
I'm developing an Android Application where I'm using an Expandable ListView. 我正在开发一个使用Expandable ListView的Android应用程序。
The Activity A has the following snippets, 活动A具有以下片段,
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.Toast;
public class A extends Activity {
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.bussinessproductmanagement);
getWindow().setWindowAnimations(0);
// get the listview
expListView = (ExpandableListView) findViewById(R.id.list);
expListView.setGroupIndicator(null);
expListView.setSelector(R.drawable.clientprojecthover);
// preparing list data
prepareListData();
listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
// Listview Group click listener
expListView.setOnGroupClickListener(new OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
return false;
}
});
// Listview Group expanded listener
expListView.setOnGroupExpandListener(new OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
int len = listAdapter.getGroupCount();
for (int i = 0; i < len; i++) {
if (i != groupPosition) {
expListView.collapseGroup(i);
}
}
}
});
// Listview Group collasped listener
expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
@Override
public void onGroupCollapse(int groupPosition) {
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition) + " Collapsed",
Toast.LENGTH_SHORT).show();
}
});
// Listview on child click listener
expListView.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
Toast.makeText(
getApplicationContext(),
listDataHeader.get(groupPosition)
+ " : "
+ listDataChild.get(
listDataHeader.get(groupPosition)).get(
childPosition), Toast.LENGTH_SHORT)
.show();
return false;
}
});
}
/*
* Preparing the list data
*/
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
ArrayList<List<String>> childObjects=new ArrayList<List<String>>();
for(int i=1;i<11;i++)
{
listDataHeader.add("Client "+ i);
List<String> childLists = new ArrayList<String>();
for(int childCount=1;childCount<11;childCount++)
{
childLists.add("Project "+childCount);
}
childObjects.add(childLists);
}
for(int j=0;j<listDataHeader.size();j++)
{
listDataChild.put(listDataHeader.get(j), childObjects.get(j));
}
}
}
The Expandable List Adapter class has the following snippets, Expandable List Adapter类具有以下片段,
import java.util.HashMap;
import java.util.List;
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.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context _context;
private List<String> _listDataHeader;
private HashMap<String, List<String>> _listDataChild;
public ExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}
@Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
}
TextView txtListChild = (TextView) convertView
.findViewById(R.id.lblListItem);
txtListChild.setText(childText);
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
}
@Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}
@Override
public int getGroupCount() {
return this._listDataHeader.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_group, null);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.lblListHeader);
ImageView imgView=(ImageView)convertView.findViewById(R.id.indicator);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
if(isExpanded)
{
imgView.setImageResource(R.drawable.clientbuttonarrowt);
}
else
{
imgView.setImageResource(R.drawable.arrowrght);
}
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
Here I can get the Expandable ListView with row and their childs. 在这里,我可以获取带有row及其子元素的Expandable ListView。 But I want to add another view under each child .
但我想在每个孩子的下方添加另一个视图。
add the additional views as footer view in your expandalble view as follows 在扩展视图中将其他视图添加为页脚视图,如下所示
ExpandableListView list = getListView();
View headerView = View.inflate(this, R.layout.header, null);
list.addHeaderView(headerView);
View footerView = View.inflate(this, R.layout.footer, null);
list.addFooterView(footerView);
Here is what I did to solve the issue 这是我为解决此问题所做的工作
public class MyExpandableAdapter extends BaseExpandableListAdapter {
Context context;
List<ParentObject> parentObjects;
public MyExpandableAdapter(Context context, List<ParentObject> parentObjects)
{
this.context = context;
this.parentObjects = parentObjects;
}
@Override
public int getGroupCount() {
return parentObjects.size();
}
//Add 2 to childcount. The first row and the last row are used as header and footer to childview
@Override
public int getChildrenCount(int i) {
return parentObjects.get(i).childObjects.size() +2;
}
@Override
public ParentObject getGroup(int i) {
return parentObjects.get(i);
}
@Override
public ChildObject getChild(int i, int i2) {
return parentObjects.get(i).childObjects.get(i2);
}
@Override
public long getGroupId(int i) {
return i;
}
@Override
public long getChildId(int i, int i2) {
return 0;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup)
{
ParentObject currentParent = parentObjects.get(i);
if(view ==null)
{
LayoutInflater inflater =(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.parent_row,null);
}
TextView txtMother = (TextView)view.findViewById(R.id.txtMother);
TextView txtFather = (TextView)view.findViewById(R.id.txtFather);
txtMother.setText(currentParent.mother);
txtFather.setText(currentParent.father);
return view;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean b, View view, ViewGroup viewGroup) {
ParentObject currentParent = getGroup(groupPosition);
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//the first row is used as header
if(childPosition ==0)
{
view = inflater.inflate(R.layout.child_header, null);
TextView txtHeader = (TextView)view.findViewById(R.id.txtHeader);
txtHeader.setText(currentParent.textToHeader);
}
//Here is the ListView of the ChildView
if(childPosition>0 && childPosition<getChildrenCount(groupPosition)-1)
{
ChildObject currentChild = getChild(groupPosition,childPosition-1);
view = inflater.inflate(R.layout.child_row,null);
TextView txtChildName = (TextView)view.findViewById(R.id.txtChildName);
TextView txtChildAge = (TextView)view.findViewById(R.id.txtChildAge);
txtChildName.setText(currentChild.childName);
txtChildAge.setText("Age: " + String.valueOf(currentChild.age));
}
//the last row is used as footer
if(childPosition == getChildrenCount(groupPosition)-1)
{
view = inflater.inflate(R.layout.child_footer,null);
TextView txtFooter = (TextView)view.findViewById(R.id.txtFooter);
txtFooter.setText(currentParent.textToFooter);
}
return view;
}
@Override
public boolean isChildSelectable(int i, int i2) {
return false;
}
} }
You can take a look at the tutorial here: http://robusttechhouse.com/how-to-add-header-footer-to-expandablelistview-childview/ Hope it helps! 您可以在此处查看该教程: http : //robusttechhouse.com/how-to-add-header-footer-to-expandablelistview-childview/希望对您有所帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.