gpt4 book ai didi

Android 可扩展 ListView 随机播放子项

转载 作者:行者123 更新时间:2023-11-30 02:59:48 29 4
gpt4 key购买 nike

您好,我正在使用 Android Expandable listview 并在其中用不同的 View 膨胀 child 。我遇到的问题是,当我展开一个 View 然后打开另一个父 View 时,布局中的 subview 变得困惑并在代码中膨胀错误的布局。这是我为这两个项目编写的示例代码。

这是我的 Activity 。

 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);
}

}

这是我的可扩展 ListView 适配器

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 extends BaseExpandablelistAdapter 下写这个

  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
}

关于Android 可扩展 ListView 随机播放子项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22712061/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com