Android可扩展列表视图洗牌子对象
您好,我正在使用Android可扩展列表视图,并在其中使用不同的视图对儿童进行充气。我遇到的问题是,当我展开一个视图,然后打开另一个父视图时,布局中的子视图会变得混乱,并在代码中膨胀错误的布局。这是我的两个项目的示例代码 这是我的活动Android可扩展列表视图洗牌子对象,android,expandablelistview,Android,Expandablelistview,您好,我正在使用Android可扩展列表视图,并在其中使用不同的视图对儿童进行充气。我遇到的问题是,当我展开一个视图,然后打开另一个父视图时,布局中的子视图会变得混乱,并在代码中膨胀错误的布局。这是我的两个项目的示例代码 这是我的活动 public class MainActivity extends Activity { List<String> groupList; List<Integer> childList; Map<String, List<I
public class MainActivity extends Activity {
List<String> groupList;
List<Integer> childList;
Map<String, List<Integer>> laptopCollection;
ExpandableListView expListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
createGroupList();
createCollection();
expListView = (ExpandableListView) findViewById(R.id.laptop_list);
final ExpandableListAdapter expListAdapter = new ExpandableListAdapter(
this, groupList, laptopCollection);
expListView.setAdapter(expListAdapter);
// setGroupIndicatorToRight();
// setGroupIndicatorToRight();
expListView.setOnChildClickListener(new OnChildClickListener() {
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
final String selected = (String) expListAdapter.getChild(
groupPosition, childPosition);
Toast.makeText(getBaseContext(), selected,
Toast.LENGTH_LONG)
.show();
return true;
}
});
expListView.setOnGroupExpandListener(new OnGroupExpandListener() {
private int lastExpandedGroupPosition;
@Override
public void onGroupExpand(int groupPosition) {
// TODO Auto-generated method stub
if (groupPosition != lastExpandedGroupPosition) {
expListView.collapseGroup(lastExpandedGroupPosition);
}
lastExpandedGroupPosition = groupPosition;
expListAdapter.notifyDataSetChanged();
};
});
}
private void createGroupList() {
groupList = new ArrayList<String>();
groupList.add("General Settings");
groupList.add("Name");
groupList.add("Password");
groupList.add("Notifications");
groupList.add("Profile Settings");
groupList.add("Change Picture");
groupList.add("Disable Account");
/*
* groupList.add("Sony"); groupList.add("HCL");
* groupList.add("Samsung");
*/
}
private void createCollection() {
// preparing laptops collection(child)
// int[] hpModels = { R.layout.settings_notification,
// R.layout.settings_newpassword,
// R.layout.settings_name};
// int[] dellModels = { R.layout.settings_name ,
// R.layout.settings_newpassword , R.layout.settings_notification};
// String[] hclModels = { "HCL S2101", "HCL L2102", "HCL V2002" };
//
//
int[] emptyList = {};
int[] password = { R.layout.settings_password,
R.layout.settings_newpassword, R.layout.settings_repeatpassword };
int[] noti = { R.layout.settings_notis };
int[] pic = { R.layout.settings_displaypicture };
int[] dellModels = { R.layout.settings_name,
R.layout.settings_newpassword, R.layout.settings_notification };
// String[] lenovoModels = { "IdeaPad Z Series", "Essential G Series",
// "ThinkPad X Series", "Ideapad Z Series" };
// String[] sonyModels = { "VAIO E Series", "VAIO Z Series",
// "VAIO S Series", "VAIO YB Series" };
//
// String[] samsungModels = { "NP Series", "Series 5", "SF Series" };
laptopCollection = new LinkedHashMap<String, List<Integer>>();
for (String laptop : groupList) {
if (laptop.equals("General Settings")) {
loadChild(emptyList);
} else if (laptop.equals("Name"))
loadChild(emptyList);
else if (laptop.equals("Password"))
loadChild(password);
else if (laptop.equals("Notifications"))
loadChild(noti);
else {
loadChild(emptyList);
}
/*
* else if (laptop.equals("Sony")) loadChild(sonyModels); else if
* (laptop.equals("HCL")) loadChild(hclModels); else if
* (laptop.equals("Samsung")) loadChild(samsungModels); else
* loadChild(lenovoModels);
*/
laptopCollection.put(laptop, childList);
}
}
private void loadChild(int[] laptopModels) {
childList = new ArrayList<Integer>();
for (Integer model : laptopModels)
childList.add(model);
}
/*
* private void setGroupIndicatorToRight() { Get the screen width
* DisplayMetrics dm = new DisplayMetrics();
* getWindowManager().getDefaultDisplay().getMetrics(dm); int width =
* dm.widthPixels;
*
* expListView.setIndicatorBounds(width - getDipsFromPixel(35), width -
* getDipsFromPixel(5)); }
*/
// Convert pixel to dip
public int getDipsFromPixel(float pixels) {
// Get the screen's density scale
final float scale = getResources().getDisplayMetrics().density;
// Convert the dps to pixels, based on density scale
return (int) (pixels * scale + 0.5f);
}
}
公共类MainActivity扩展活动{
列表组列表;
儿童名单;
地图收集;
ExpandableListView解释视图;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
createGroupList();
createCollection();
expListView=(ExpandableListView)findViewById(R.id.laptop\u列表);
最终ExpandableListAdapter expListAdapter=新ExpandableListAdapter(
这是(组列表、laptopCollection);
expListView.setAdapter(expListAdapter);
//setGroupIndicatorToRight();
//setGroupIndicatorToRight();
setOnChildClickListener(新的OnChildClickListener(){
公共布尔onChildClick(ExpandableListView父视图,视图v,
int groupPosition、int childPosition、long id){
选择的最终字符串=(字符串)expListAdapter.getChild(
组位置、子位置);
Toast.makeText(getBaseContext(),已选中,
吐司长度(长)
.show();
返回true;
}
});
expListView.setOnGroupExpandListener(新的OnGroupExpandListener(){
私人int lastExpandedGroupPosition;
@凌驾
public void onGroupExpand(int groupPosition){
//TODO自动生成的方法存根
如果(groupPosition!=lastExpandedGroupPosition){
expListView.collapseGroup(lastExpandedGroupPosition);
}
lastExpandedGroupPosition=groupPosition;
expListAdapter.notifyDataSetChanged();
};
});
}
私有void createGroupList(){
groupList=newarraylist();
组列表。添加(“常规设置”);
组列表。添加(“名称”);
添加(“密码”);
组列表。添加(“通知”);
添加(“配置文件设置”);
组列表。添加(“更改图片”);
组列表。添加(“禁用帐户”);
/*
*groupList.add(“索尼”);groupList.add(“HCL”);
*groupList.add(“三星”);
*/
}
私有void createCollection(){
//准备笔记本电脑系列(儿童版)
//int[]hpModels={R.layout.settings\u通知,
//R.layout.settings\u新密码,
//R.layout.settings_name};
//int[]dellModels={R.layout.settings\u name,
//R.layout.settings\u newpassword,R.layout.settings\u notification};
//字符串[]hclModels={“HCL S2101”、“HCL L2102”、“HCL V2002”};
//
//
int[]emptyList={};
int[]password={R.layout.settings\u password,
R.layout.settings\u newpassword,R.layout.settings\u repeatpassword};
int[]noti={R.layout.settings\u notis};
int[]pic={R.layout.settings\u displaypicture};
int[]dellModels={R.layout.settings\u name,
R.layout.settings\u newpassword,R.layout.settings\u notification};
//字符串[]lenovoModels={“IdeaPad Z系列”,“基本G系列”,
//“ThinkPad X系列”、“Ideapad Z系列”};
//字符串[]sonyModels={“VAIO E系列”、“VAIO Z系列”,
//“VAIO S系列”、“VAIO YB系列”};
//
//字符串[]三星模型={“NP系列”、“系列5”、“SF系列”};
LaptoCollection=新LinkedHashMap();
用于(字符串笔记本电脑:组列表){
if(笔记本电脑等同(“一般设置”)){
loadChild(空列表);
}else if(laptop.equals(“Name”))
loadChild(空列表);
else if(laptop.equals(“密码”))
loadChild(密码);
else if(laptop.equals(“通知”))
loadChild(noti);
否则{
loadChild(空列表);
}
/*
*else if(笔记本电脑等于(“索尼”))loadChild(sonyModels);else if
*(笔记本电脑等于(“HCL”))loadChild(HCL型号);如果
*(laptop.equals(“三星”))loadChild(三星型号);其他
*loadChild(lenovoModels);
*/
laptopCollection.put(笔记本电脑、儿童列表);
}
}
私有void loadChild(int[]laptopModels){
childList=newarraylist();
对于(整数模型:laptopModels)
添加(模型);
}
/*
*私有void setGroupIndicatorToRight(){获取屏幕宽度
*DisplayMetrics dm=新的DisplayMetrics();
*getWindowManager().getDefaultDisplay().getMetrics(dm);整数宽度=
*dm.1像素;
*
*expListView.setIndicatorBounds(宽度-getDipsFromPixel(35),宽度-
*getDipsFromPixel(5));}
*/
//将像素转换为倾斜
公共整型getDipsFromPixel(浮点像素){
//获取屏幕的密度刻度
最终浮动比例=getResources().getDisplayMetrics().density;
//根据密度比例将dps转换为像素
返回(int)(像素*比例+0.5f);
}
}
这是我的可扩展列表视图适配器
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Activity context;
private Map<String, List<Integer>> laptopCollections;
private List<String> laptops;
public ExpandableListAdapter(Activity context, List<String> laptops,
Map<String, List<Integer>> laptopCollection) {
this.context = context;
this.laptopCollections = laptopCollection;
this.laptops = laptops;
}
public Object getChild(int groupPosition, int childPosition) {
return laptopCollections.get(laptops.get(groupPosition)).get(childPosition);
}
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
public View getChildView(final int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final int laptop = (Integer) getChild(groupPosition, childPosition);
LayoutInflater inflater = context.getLayoutInflater();
if (convertView == null) {
convertView = inflater.inflate(laptop ,null);
}
TextView item = (TextView) convertView.findViewById(R.id.laptop);
item.setText(laptop);
return convertView;
}
public int getChildrenCount(int groupPosition) {
return laptopCollections.get(laptops.get(groupPosition)).size();
}
public Object getGroup(int groupPosition) {
return laptops.get(groupPosition);
}
public int getGroupCount() {
return laptops.size();
}
public long getGroupId(int groupPosition) {
return groupPosition;
}
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String laptopName = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.grouup_item,
null);
}
TextView item = (TextView) convertView.findViewById(R.id.laptop);
item.setTypeface(null, Typeface.BOLD);
item.setText(laptopName);
return convertView;
}
public boolean hasStableIds() {
return true;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
公共类ExpandableListAdapter扩展了BaseExpandableListAdapter{
私人活动语境;
私人地图收藏;
私人名单笔记本电脑;
公共ExpandableListAdapter(活动上下文、笔记本电脑列表、,
地图(收集){
this.context=上下文;
this.laptopCollections=laptopCollection;
this.laptops=笔记本电脑;
}
公共对象getChild(int-groupPosition,int-childPosition){
返回laptopCollections.get(laptops.get(groupPosition)).get(childPosition);
}
公共长getChildId(int-groupPosition,int-childPosition){
返回子位置;
}
公共视图getChildView(最终int groupPosition,最终int childPosition,
布尔值isLastChild、视图转换视图、视图组父级){
final int=(整数)getChild(groupPosition,childPosition);
LayoutInflater充气器=上下文。getLayoutInflater();
如果(con
public int getViewTypeCount()
{
return 2;//layout number
}
public int getItemViewType(int groupposition,int childposition) {
if (getChildId(groupposition, childposition)!=3) //set condition when it changed
return 1;//retun layout number
else
return 0;//retun layout number
}