【发布时间】:2015-02-23 06:35:13
【问题描述】:
我有一个导航抽屉,并试图在 android 中实现一个可扩展的 LisView,其中包含 3 个级别:
趋势
外观
男人
--服装
--------西服
-------休闲
--鞋类
-------靴子
-------人字拖
女性
--服装
-------西服
-------休闲
--鞋类
-------靴子
-------人字拖
我正在使用一个可扩展的 ListView1 并将另一个可扩展的 ListView2 设置为其子级,这将扩展第三级子类别。 但我没有得到想要的结果。 2 级类别没有到来,所以 3 级是无法到达的。 任何想法或帮助将不胜感激。我无法在任何地方找到解决方案。
`public void prepareListData() { // TODO 自动生成的方法存根 listDataHeader=new ArrayList(); listDataChild=new HashMap>();
listDataHeader.add("Trending");
listDataHeader.add("Looks");
listDataHeader.add("Men");
listDataHeader.add("Women");
listDataHeader.add("Leaderboard");
List<String> Men=new ArrayList<>();
Men.add("Clothing");
Men.add("Footwear");
Men.add("Accesories");
List<String> Women=new ArrayList<>();
Women.add("Western wear");
Women.add("Footwear");
Women.add("Ethnic wear");
Women.add("Lingerie");
Women.add("Accesories");
listDataChild.put(listDataHeader.get(2),Men); // Header, Child data
listDataChild.put(listDataHeader.get(3),Women);
//Men sub sub categories
//listDataSubChildHeader.add("Clothing");
//listDataSubChildHeader.add("Clothing");
List<String> menClothing=new ArrayList<>();
menClothing.add("Casuals");
menClothing.add("Ethnic wear");
List<String> menFootwear=new ArrayList<>();
menFootwear.add("Boots");
menFootwear.add("Casual Shoes");
List<String> menAccesories=new ArrayList<>();
menAccesories.add("Bags and wallets");
menAccesories.add("Caps and hats");
//sub sub categories
//listDataSubChildHeader=new ArrayList<String>();
menListDataSubChild=new HashMap<String,List<String>>();
womenListDataSubChild=new HashMap<String,List<String>>();
menListDataSubChild.put("Clothing",menClothing);//0
menListDataSubChild.put("Footwear", menFootwear);//1
menListDataSubChild.put("Accesories", menAccesories);//2
//Women sub sub categories
List<String> WomenWestern=new ArrayList<>();
menClothing.add("Bottom Wear");
menClothing.add("Dresses");
List<String> WomenFootwear=new ArrayList<>();
menFootwear.add("Bellies and Loafers");
menFootwear.add("Foots");
List<String> WomenEthnic=new ArrayList<>();
menAccesories.add("Kurtas and Kurtis");
menAccesories.add("Leggings");
List<String> WomenAccesories=new ArrayList<>();
menAccesories.add("Bags and wallets");
menAccesories.add("Belts");
List<String> WomenLingerie=new ArrayList<>();
menAccesories.add("Night wear");
menAccesories.add("Swim wear");
womenListDataSubChild.put("Western wear",WomenWestern);//0
womenListDataSubChild.put("Footwear",WomenFootwear);//1
womenListDataSubChild.put("Ethnic wear",WomenEthnic);//2
womenListDataSubChild.put("Lingerie",WomenLingerie);//3
womenListDataSubChild.put("Accesories",WomenAccesories);//4
}`
`public void expandableListAdapter() { // TODO 自动生成的方法存根 //listAdapter 实例 listAdapter = new ExpandableListAdapter(getApplicationContext(), listDataHeader, listDataChild,menListDataSubChild,womenListDataSubChild); // 设置leftMenu列表视图的适配器
expListView.setAdapter(listAdapter);
//Calling fuction for adding icon for Navigation Bar
addingNavIconOnActionBar();
// Listview Group click listener
expListView.setOnGroupClickListener(new OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
// Toast.makeText(getApplicationContext(),
//"Group Clicked " + listDataHeader.get(groupPosition),
//Toast.LENGTH_SHORT).show();
if(groupPosition==0)
{
Intent trendingIntent=new Intent(getApplicationContext(), Trending.class);
startActivity(trendingIntent);
}
else if(groupPosition==1)
{
Intent trendingIntent=new Intent(getApplicationContext(), Looks.class);
startActivity(trendingIntent);
}
return false;
}
});
// Listview Group expanded listener
expListView.setOnGroupExpandListener(new OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition) + " Expanded",
Toast.LENGTH_SHORT).show();
}
});
// 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();
}
});
// mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
// 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();`enter code here`
return false;
}
});
}`
enter code here
公共类 ExpandableListAdapter 扩展 BaseExpandableListAdapter {
private Context _context;
List<String> _listDataHeader; // header titles
// child data in format of header title, child title
HashMap<String, List<String>> _listDataChild;
//sub sub category of men and women
private HashMap<String, List<String>> _menListDatasubChild,_womenListDatasubChild;
ExpandableListView expChildList;
ChildExpandableListAdapter childAdapter;
public ExpandableListAdapter(Context context,
List<String> listDataHeader,
HashMap<String, List<String>> listDataChild, HashMap<String, List<String>> menListDataSubChild, HashMap<String, List<String>> womenListDataSubChild) {
// TODO Auto-generated constructor stub
this._context=context;
this._listDataChild=listDataChild;
this._listDataHeader=listDataHeader;
this._menListDatasubChild=menListDataSubChild;
this._womenListDatasubChild=womenListDataSubChild;
}
@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.ListHeader1);
//calling sub level Adapter
Log.e("groupPos", String.valueOf(groupPosition));
Log.e("childText", childText);
expChildList=(ExpandableListView) convertView.findViewById(R.id.expChildList);
childAdapter=new ChildExpandableListAdapter(this._context,groupPosition);
expChildList.setAdapter(childAdapter);
// Listview Group click listener
expChildList.setOnGroupClickListener(new OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
// Toast.makeText(getApplicationContext(),
//"Group Clicked " + listDataHeader.get(groupPosition),
//Toast.LENGTH_SHORT).show();
return false;
}
});
// Listview Group expanded listener
expChildList.setOnGroupExpandListener(new OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
}
});
// Listview Group collasped listener
expChildList.setOnGroupCollapseListener(new OnGroupCollapseListener() {
@Override
public void onGroupCollapse(int groupPosition) {
}
});
// mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
// Listview on child click listener
expChildList.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
return false;
}
});
//txtListChild.setText(childText);
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
if(groupPosition==0)
return 0;
else if(groupPosition==1)
return 0;
else if(groupPosition==4)
return 0;
else
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;
}
@SuppressLint("InflateParams")
@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.ListHeader);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
ImageView iconExpand = (ImageView) convertView.findViewById(R.id.icon_expand);
ImageView iconCollapse = (ImageView) convertView.findViewById(R.id.icon_collapse);
if(getChildrenCount(groupPosition)==0)
{
iconExpand.setVisibility(View.INVISIBLE);
iconCollapse.setVisibility(View.INVISIBLE);
}
else if(isExpanded)
{
iconExpand.setVisibility(View.INVISIBLE);
iconCollapse.setVisibility(View.VISIBLE);
}
else
{
iconExpand.setVisibility(View.VISIBLE);
iconCollapse.setVisibility(View.INVISIBLE);
}
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
//********sub sub child adapter
public class ChildExpandableListAdapter extends BaseExpandableListAdapter {
Context context;
int _groupPosition;
public ChildExpandableListAdapter(
Context context, int groupPosition) {
// TODO Auto-generated constructor stub
this.context=context;
this._groupPosition=groupPosition;
Log.e("gP",""+_groupPosition);
}
@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return 0;
}
@Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return 0;
}
@Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
if(_groupPosition==2)//men sub category
{
if(groupPosition==0)
return "Clothing";
else if(groupPosition==1)
return "Footwear";
else
return "Accesories";
}
else if(_groupPosition==3)//female sub category
{
if(groupPosition==0)
return "Western wear";
else if(groupPosition==1)
return "Footwear";
else if(groupPosition==2)
return "Ethnic wear";
else if(groupPosition==2)
return "Lingerie";
else
return "Accesories";
}
return null;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
//return this.__listDataChild.get(groupPosition)
if(_groupPosition==2)
{
return _menListDatasubChild.get(getGroup(groupPosition)).get(childPosition);
}
else if(_groupPosition==3)
{
return _womenListDatasubChild.get(getGroup(groupPosition)).get(childPosition);
}
return null;
}
@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return 0;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return 0;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
String headerTitle =(String)getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_group1,null);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.ListHeader1);
Log.e("HeaderTitle",headerTitle);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
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.sub_explist_row, null);
}
TextView txtListChild = (TextView) convertView
.findViewById(R.id.textView1);
txtListChild.setText(childText);
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}
enter code here
}
}
【问题讨论】:
标签: android