我正在尝试显示文件和文件夹列表,我知道如何在listview中执行此操作。但是,为了更好地使用ui,我宁愿这样做,如下图所示。
这是我最初对ListView做的事情:
我有2项活动。在第1次活动中,
不仅如此,在第二项活动中,
所以我希望按照上面显示的图像进行操作。
我该怎么做?因为我之前从未“触及过”可扩展的列表视图。
答案 0 :(得分:1)
创建自定义ExpandableListView适配器:
public class customListAdapter extends BaseExpandableListAdapter {
private File folder1;
private File folder2;
private String[] groups = {};
private String[][] children = {};
public customListAdapter() {
// Sample data set. children[i] contains the children (String[]) for groups[i].
folder1 = new File (Environment.getExternalStorageDirectory(),"/Folder1");
folder2 = new File (Environment.getExternalStorageDirectory(),"/Folder2");
String[] fileList1 = folder1.list();
String[] fileList2 = folder2.list();
Arrays.sort(fileList1);
Arrays.sort(fileList2);
groups = new String[] { "Folder1" , "Folder2" };
children = new String[][] { fileList1, fileList2 };
}//constructor
public Object getChild(int groupPosition, int childPosition) {
return children[groupPosition][childPosition];
}
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
public int getChildrenCount(int groupPosition) {
return children[groupPosition].length;
}
public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
View convertView, ViewGroup parent) {
TextView textView = new TextView(this);
textView.setBackgroundColor(Color.BLACK);
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
textView.setPadding(100, 5, 0, 5);
textView.setTextColor(Color.WHITE);
textView.setTextSize(23);
textView.setId(1000);
textView.setText(getChild(groupPosition, childPosition).toString());
return textView;
}//getChildView
public Object getGroup(int groupPosition) {
return groups[groupPosition];
}
public int getGroupCount() {
return groups.length;
}
public long getGroupId(int groupPosition) {
return groupPosition;
}
public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
ViewGroup parent) {
TextView textView = new TextView(this);
textView.setBackgroundColor(Color.WHITE);
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
textView.setPadding(100, 0, 0, 0);
textView.setTextColor(Color.BLACK);
textView.setTextSize(25);
textView.setText(getGroup(groupPosition).toString());
return textView;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
public boolean hasStableIds() {
return true;
}
}//customListAdapter
然后,使用:
引用它setContentView(R.layout.preferedlayout);
// Set up our adapter
listAdapter = new customListAdapter();
expLV = (ExpandableListView) findViewById(R.id.expList);
expLV.setAdapter(listAdapter);
希望这有助于其他人。 =)
答案 1 :(得分:0)
如果我正确理解了你的问题,你可以在ListView中设置一个适配器来扩展你想要的某种布局。
示例:
listView = (ListView)findViewById(R.id.feedList);
MyAdapter adapter = new MyAdapter(this, items, Color.WHITE);
listView.setAdapter(adapter);
items
是某些排序实体的ArrayList
适配器代码:
public class MyAdapter extends BaseAdapter {
private ArrayList<MyItem> items = null;
private LayoutInflater layoutInflater = null;
MyAdapter(Context context, ArrayList<MyItem> items, int bgColor){
this.items = items;
layoutInflater = LayoutInflater.from(context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = layoutInflater.inflate(R.layout.my_layout, null);
ImageView imageView = (ImageView) row.findViewById(R.id.image);
imageView.setImageResource(R.drawable.my_image);
TextView text = (TextView) row.findViewById(R.id.title);
text.setText("Hi");
return row;
}
}