Reputation: 531
I'm trying to display a list of files and folders and I know how to do it in listview. However, for better ui usage, I would rather do it like the image below.
This is what I initially did with just ListView:
I have 2 activities. On the 1st activity,
not only that, on the 2nd activity,
so what I hope to do is, following the image shown above..
how do I do that ? as I've never 'touched' an expandable listview before.
Upvotes: 2
Views: 4259
Reputation: 531
create a custom ExpandableListView adapter:
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
then, reference it using:
setContentView(R.layout.preferedlayout);
// Set up our adapter
listAdapter = new customListAdapter();
expLV = (ExpandableListView) findViewById(R.id.expList);
expLV.setAdapter(listAdapter);
hope this help other people. =)
Upvotes: 1
Reputation: 3497
If i understood your question correctly, you can set an adapter in your ListView that will be inflate some sort of layout you want.
example:
listView = (ListView)findViewById(R.id.feedList);
MyAdapter adapter = new MyAdapter(this, items, Color.WHITE);
listView.setAdapter(adapter);
items
is ArrayList
of some sort entities
adapter Code:
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;
}
}
Upvotes: 0