Và có thể tùy chỉnh trẻ trong danh sách có thể mở rộng không?Làm thế nào để thêm hình ảnh trong danh sách mở rộng trong phụ huynh trong android?
Trả lời
Làm việc với SimpleExpandableListAdapter
là mọi thứ nhưng đơn giản. Dưới đây là một số mã sẽ giúp bạn bắt đầu, giả sử bạn đang sử dụng ExpandableListActivity
.
Trong ví dụ này, chúng tôi sử dụng tiêu chuẩn android.R.layout.simple_expandable_list_item_1
cho chế độ xem tiêu đề nhóm của chúng tôi, nhưng chúng tôi sử dụng bố cục tùy chỉnh của riêng chúng tôi cho trẻ em.
// Construct Expandable List
final String NAME = "name";
final String IMAGE = "image";
final LayoutInflater layoutInflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final ArrayList<HashMap<String, String>> headerData = new ArrayList<HashMap<String, String>>();
final HashMap<String, String> group1 = new HashMap<String, String>();
group1.put(NAME, "Group 1");
headerData.add(group1);
final HashMap<String, String> group2 = new HashMap<String, String>();
group2.put(NAME, "Group 2");
headerData.add(group2);
final ArrayList<ArrayList<HashMap<String, Object>>> childData = new ArrayList<ArrayList<HashMap<String, Object>>>();
final ArrayList<HashMap<String, Object>> group1data = new ArrayList<HashMap<String, Object>>();
childData.add(group1data);
final ArrayList<HashMap<String, Object>> group2data = new ArrayList<HashMap<String, Object>>();
childData.add(group2data);
// Set up some sample data in both groups
for(int i=0; i<10; ++i) {
final HashMap<String, Object> map = new HashMap<String,Object>();
map.put(NAME, "Child " + i);
map.put(IMAGE, getResources().getDrawable(R.drawable.icon));
(i%2==0 ? group1data : group2data).add(map);
}
setListAdapter(new SimpleExpandableListAdapter(
this,
headerData,
android.R.layout.simple_expandable_list_item_1,
new String[] { NAME }, // the name of the field data
new int[] { android.R.id.text1 }, // the text field to populate with the field data
childData,
0,
null,
new int[] {}
) {
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
final View v = super.getChildView(groupPosition, childPosition, isLastChild, convertView, parent);
// Populate your custom view here
((TextView)v.findViewById(R.id.name)).setText((String) ((Map<String,Object>)getChild(groupPosition, childPosition)).get(NAME));
((ImageView)v.findViewById(R.id.image)).setImageDrawable((Drawable) ((Map<String,Object>)getChild(groupPosition, childPosition)).get(IMAGE));
return v;
}
@Override
public View newChildView(boolean isLastChild, ViewGroup parent) {
return layoutInflater.inflate(R.layout.expandable_list_item_with_image, null, false);
}
}
);
Và bên trong bố trí con tùy chỉnh của bạn, tên expandable_list_item_with_image.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right" />
</RelativeLayout>
Bạn có thể thử làm bộ chuyển đổi danh sách của riêng bạn mà kéo dài BaseExpandableListAdapter chỉ thích được mô tả trong documentation.
Sau đó ghi đè hàm getGroupView(..)
(cho mục cha hoặc getChildView cho mục con) và trong hàm này, bạn có thể tăng bố cục xml của riêng mình.
một cái gì đó như thế này:
public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
ViewGroup parent)
{
View v = convertView;
if (v == null) {
//sender is activity from where you call this adapter. Set it with construktor.
LayoutInflater vi = (LayoutInflater)sender.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row, null);
}
//children = arraylists of Child
Child c = children.get(childPosition);
if (c != null) {
TextView tt = (TextView) v.findViewById(R.id.toptext);
TextView bt = (TextView) v.findViewById(R.id.bottomtext);
ImageView icon = (ImageView) v.findViewById(R.id.rowicon);
if (tt != null) {
tt.setText(c.text1); }
if(bt != null){
bt.setText(c.text2);
}
if (icon != null)
{
icon.setImageResource(R.drawable.rowicon);
}
}
return v;
}
layout xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip">
<ImageView
android:id="@+id/rowicon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="6dip" />
<LinearLayout
android:orientation="vertical"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="fill_parent">
<TextView
android:id="@+id/toptext"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:gravity="center_vertical"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:id="@+id/bottomtext"
android:singleLine="true"
android:ellipsize="marquee"
/>
</LinearLayout>
</LinearLayout>
Sau khi bạn ghi đè getGroupView, bạn có thể sử dụng chức năng setCompoundDrawablesWithIntrinsicBounds
để thêm hình ảnh.
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
{
View v = super.getGroupView(groupPosition, isExpanded, convertView, parent);
TextView tv = (TextView) v.findViewById(R.id.textViewId);
tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.imageToAdd, 0, 0, 0);
return v;
}
tôi chỉ tìm thấy một cách tốt hơn, bạn chỉ có thể thêm
android:drawableRight="@drawable/icon"
dòng để bố trí tập tin xml của nhóm danh sách.
hoạt động như một nét duyên dáng..Đơn giản và tốt nhất – Prabs
Hãy thử điều này >>> Tutorial
Và bạn có thể cập nhật nó để thêm hình ảnh thay vì văn bản trong tập tin xml con
- 1. Biểu tượng hình ảnh ở chế độ xem danh sách có thể mở rộng trong android
- 2. Android Danh sách mở rộng
- 3. Làm cách nào để mở rộng Danh sách trong Dart?
- 4. Mở rộng hình ảnh bên trong DIV
- 5. Làm cách nào để mở rộng danh sách điều hướng trong thanh tác vụ?
- 6. Phụ thêm mục vào danh sách trong danh sách hiểu
- 7. Trượt mở rộng hoạt ảnh trong android
- 8. Android: ImageView mở rộng hình ảnh nguồn
- 9. Mở rộng hàng nhóm theo lập trình trong danh sách mở rộng
- 10. Làm cách nào để tìm chỉ mục của danh sách phụ trong danh sách?
- 11. hình ảnh Scale trong tương android để sàng lọc rộng
- 12. Chế độ xem danh sách mở rộng
- 13. Làm thế nào để cắt một hình ảnh trong Android?
- 14. Hình ảnh trong danh sách thả xuống
- 15. Sử dụng JQuery để chọn hộp kiểm trong danh sách mục phụ huynh?
- 16. Làm thế nào để hình dung một danh sách các danh sách ... trong R?
- 17. Làm thế nào để mở rộng một mô hình từ mô hình tùy chỉnh trong CodeIgniter
- 18. Làm cách nào để mở rộng phạm vi vào danh sách trong vimscript?
- 19. tệp hình ảnh mở rộng không hiển thị trong IIS7.5
- 20. Làm thế nào để làm cho con vượt quá chiều rộng của phụ huynh đúng cách?
- 21. Ẩn chế độ xem Nhóm trong Danh sách mở rộng
- 22. Làm thế nào để kết hợp nhiều hình ảnh thành một hình ảnh trong Android?
- 23. Làm thế nào để phóng to hình ảnh thu nhỏ trong hình ảnh zoom android?
- 24. Mở rộng danh sách các định dạng ảnh được hỗ trợ trong GDI +
- 25. xóa danh sách phụ khỏi danh sách trong tcl
- 26. Cách thêm văn bản dưới nút hình ảnh trong Android?
- 27. gọi Fancybox trong phụ huynh từ iframe
- 28. Làm thế nào để tạo ra một danh sách với sự mở rộng cú đúp để
- 29. Làm thế nào để thêm mục trong danh sách thả xuống trong tập tin cs
- 30. Trình nghe nhấp chuột nhóm trong chế độ xem danh sách mở rộng android
câu trả lời tuyệt vời, nên được chấp nhận – tomash
Chỉ muốn nói rằng đây là một ví dụ tuyệt vời, nó thực sự hữu ích! Cảm ơn bạn –
@emmby xin chào cách thêm hình ảnh và văn bản khác nhau cho từng đứa con của cha mẹ, tức là – Sultan