Android - 如何在 Android 的 Expandable ListView 的子视图下添加另一个视图?
Posted
技术标签:
【中文标题】Android - 如何在 Android 的 Expandable ListView 的子视图下添加另一个视图?【英文标题】:Android - How to add Another view under a childview in Expandable ListView in Android? 【发布时间】:2013-09-20 03:49:59 【问题描述】:我正在开发一个使用 Expandable ListView 的 android 应用程序。
Activity A有以下sn-ps,
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));
可扩展列表适配器类有以下sn-ps,
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;
在这里,我可以获得带有行及其子项的 Expandable ListView。但我想在每个孩子下添加另一个视图。
【问题讨论】:
检查这个...这将有助于你...allappsdevelopers.com/…@Rakesh L 你有解决方案吗,上面的链接不起作用 【参考方案1】:在您的可展开视图中添加其他视图作为页脚视图,如下所示
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);
【讨论】:
这可以将页眉和页脚添加到列表视图,但我们如何添加到子级仅意味着所有子级都有一个共同视图【参考方案2】:这是我为解决问题所做的工作
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;
您可以在这里查看教程:http://robusttechhouse.com/how-to-add-header-footer-to-expandablelistview-childview/ 希望对您有所帮助!
【讨论】:
以上是关于Android - 如何在 Android 的 Expandable ListView 的子视图下添加另一个视图?的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Adobe Air Mobile 的 Android 原生扩展中包含额外的 Jar
如何在 Android 中处理 NullPointerException?
如何修复:E/AndroidRuntime:致命例外:主进程:com.android.youtubedownloader,PID:9291
如何使用 Adobe AIR 在 Android 中使用原生 java 代码制作应用程序?