Bài viết phổ biến của tác giả
- Siêu lớp và danh mục iOS/Objective-C
- object-c - -1001 lỗi khi NSURLSession vượt qua httpproxy và /etc/hosts
- java - Nhận địa chỉ url bằng lớp mạng
- ios - Âm thanh không phát trong thông báo đẩy
Làm cách nào để áp dụng nền đơn cho nhóm và trẻ em trong ListView có thể mở rộng?
sản lượng dự kiến
biên tập:
MainActivity.java
thông tin gói.androidhive.expandablelistview;
nhập java.util.ArrayList;
nhập java.util.HashMap;
nhập java.util.List;
import android.app.Activity;
nhập android.os.Bundle;
nhập android.view.View;
nhập android.widget.ExpandableListView;
nhập android.widget.ExpandableListView.OnChildClickListener;
nhập android.widget.ExpandableListView.OnGroupClickListener;
nhập android.widget.ExpandableListView.OnGroupCollapseListener;
nhập android.widget.ExpandableListView.OnGroupExpandListener;
nhập android.widget.Toast;
lớp công khai MainActivity mở rộng Hoạt động {
Danh sách có thể mở rộngListAdapter;
ExpandableListView expListView;
Danh sách listDataHeader;
HashMap> listDataChild;
@Ghi đè
khoảng trống được bảo vệ trênCreate(Gói đã lưuInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// lấy danh sách xem
expListView = (ExpandableListView) findViewById(R.id.lvExp);
//chuẩn bị dữ liệu danh sách
chuẩn bịListData();
listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
// bộ chuyển đổi danh sách cài đặt
expListView.setAdapter(listAdapter);
// Trình nghe nhấp chuột của Nhóm Listview
expListView.setOnGroupClickListener(new OnGroupClickListener() {
@Ghi đè
boolean công khai onGroupClick(ExpandableListView parent, View v,
int nhómPosition, id dài) {
// Toast.makeText(getApplicationContext(),
// "Nhóm được nhấp vào " + listDataHeader.get(groupPosition),
// Toast.LENGTH_SHORT).show();
trả về sai;
}
});
// Trình nghe mở rộng của Nhóm Listview
expListView.setOnGroupExpandListener(new OnGroupExpandListener() {
@Ghi đè
khoảng trống công khai trênGroupExpand(int groupPosition) {
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition) + "Mở rộng",
Toast.LENGTH_SHORT).show();
}
});
expListView.setDivider(null);
expListView.setChildDivider(null);
// Trình nghe thu gọn của Nhóm Listview
expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
@Ghi đè
khoảng trống công khai trênGroupCollapse(int groupPosition) {
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition) + "Đã thu gọn",
Toast.LENGTH_SHORT).show();
}
});
// Listview trên trình nghe nhấp chuột con
expListView.setOnChildClickListener(OnChildClickListener mới() {
@Ghi đè
boolean công khai onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, id dài) {
// TODO sơ khai phương thức được tạo tự động
Toast.makeText(
getApplicationContext(),
listDataHeader.get(groupPosition)
+ " : "
+ listDataChild.get(
listDataHeader.get(groupPosition)).get(
childPosition), Toast.LENGTH_SHORT)
.trình diễn();
trả về sai;
}
});
}
/*
* Chuẩn bị dữ liệu danh sách
*/
khoảng trống riêng tư chuẩn bịListData() {
listDataHeader = new ArrayList();
listDataChild = new HashMap>();
// Thêm dữ liệu con
listDataHeader.add("Top 250");
listDataHeader.add("Đang hiển thị");
listDataHeader.add("Sắp ra mắt..");
// Thêm dữ liệu con
Danh sách top250 = new ArrayList();
top250.add("Sự chuộc lỗi của Shawshank");
top250.add("Bố già");
top250.add("Bố già: Phần II");
top250.add("Tiểu thuyết bột giấy");
top250.add("Tốt, Xấu và Xấu");
top250.add("Hiệp sĩ bóng đêm");
top250.add("12 Người Đàn Ông Nổi Giận");
List nowShowing = new ArrayList();
nowShowing.add("The Conjuring");
nowShowing.add("Despicable Me 2");
nowShowing.add("Turbo");
nowShowing.add("Người lớn 2");
nowShowing.add("Đỏ 2");
nowShowing.add("Người Sói");
Danh sách sắp tới = new ArrayList();
sắpSoon.add("2 khẩu súng");
sắpSoon.add("The Smurfs 2");
sắpSoon.add("Bây giờ ngoạn mục");
sắpSoon.add("The Canyons");
sắpSoon.add("Báo cáo của Châu Âu");
listDataChild.put(listDataHeader.get(0), top250); // Tiêu đề, Dữ liệu con
listDataChild.put(listDataHeader.get(1), nowShowing);
listDataChild.put(listDataHeader.get(2), sắp ra mắt);
}
}
Có thể mở rộngListAdapter.java
thông tin gói.androidhive.expandablelistview;
nhập java.util.HashMap;
nhập java.util.List;
nhập android.content.Context;
nhập android.graphics.Typeface;
nhập android.view.LayoutInflater;
nhập android.view.View;
nhập android.view.ViewGroup;
nhập android.widget.BaseExpandableListAdapter;
nhập android.widget.ExpandableListView;
nhập android.widget.TextView;
lớp công khai ExpandableListAdapter mở rộng BaseExpandableListAdapter {
Bối cảnh riêng tư _context;
Danh sách riêng _listDataHeader // tiêu đề tiêu đề
// dữ liệu con ở dạng tiêu đề tiêu đề, tiêu đề con
HashMap riêng tư> _listDataChild;
public ExpandableListAdapter(Context context, List listDataHeader,
HashMap> listChildData) {
this._context = bối cảnh;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}
@Ghi đè
Đối tượng công khai getChild(int groupPosition, int childPosititon) {
trả về this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
}
@Ghi đè
công khai getChildId(int groupPosition, int childPosition) {
trả về vị trí con;
}
@Ghi đè
Chế độ xem công khai getChildView(int groupPosition, int childPosition cuối cùng,
boolean isLastChild, Xem ConvertView, ViewGroup gốc) {
Chuỗi cuối cùng childText = (Chuỗi) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ConvertView = infalInflater.inflate(R.layout.list_item, null);
}
TextView txtListChild = (TextView) ConvertView
.findViewById(R.id.lblListItem);
txtListChild.setText(childText);
trả về ConvertView;
}
@Ghi đè
public int getChildrenCount(int groupPosition) {
trả về this._listDataChild.get(this._listDataHeader.get(groupPosition))
.kích cỡ();
}
@Ghi đè
Đối tượng công khai getGroup(int groupPosition) {
trả về cái này._listDataHeader.get(groupPosition);
}
@Ghi đè
công khai int getGroupCount() {
trả lại cái này._listDataHeader.size();
}
@Ghi đè
công khai getGroupId(int groupPosition) {
trả về vị trí nhóm;
}
@Ghi đè
Chế độ xem công khai getGroupView(int groupPosition, boolean isExpanded,
Xem ConvertView, ViewGroup gốc) {
Chuỗi headerTitle = (Chuỗi) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ConvertView = infalInflater.inflate(R.layout.list_group, null);
ConvertView.setBackgroundResource(R.drawable.bubble_green);
ExpandableListView mExpandableListView = cha mẹ (ExpandableListView);
mExpandableListView.expandGroup(groupPosition);
}
TextView lblListHeader = (TextView) ConvertView
.findViewById(R.id.lblListHeader);
lblListHeader.setTypeface(null, Typeface. BÓNG);
lblListHeader.setText(headerTitle);
trả về ConvertView;
}
@Ghi đè
boolean công khai hasStableIds() {
trả về sai;
}
@Ghi đè
boolean công khai isChildSelectable(int groupPosition, int childPosition) {
trả về đúng sự thật;
}
}
hoạt động_main.xml
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="dọc"
>
android:id="@+id/lvExp"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:groupIndicator="@null"
/>
list_item.xml
android:layout_width="match_parent"
android:layout_height="55dip"
android:orientation="vertical" >
android:id="@+id/lblListItem"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="17dip"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:textColor="#000000"
android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft" />
list_group.xml
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="dọc"
android: đệm = "8dp"
>
android:id="@+id/lblListHeader"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
android:textSize="17dp"
/>
AndroidManifest.xml
gói="thông tin.androidhive.expandablelistview"
android:versionCode="1"
android:versionName="1.0" >
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<ứng>ứng>
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="Có thể mở rộng"
>
android:name="info.androidhive.expandablelistview.MainActivity"
>
Đây là hình 9 miếng
câu trả lời hay nhất
Bạn có thể đặt nền con giống với nền nhóm có thể vẽ được
và thêm của bạn Chế độ xem danh sách có thể mở rộng
Bộ chia được đặt thành null
.
expListView.setDivider(null);
expListView.setChildDivider(null);
Để xóa các chỉ báo nhóm khỏi phần tử gốc, bạn cũng cần đặt chúng thành trống:
android:groupIndicator="@null" />
Về android - Cách áp dụng một nền duy nhất cho nhóm và trẻ em trong ListView có thể mở rộng, chúng tôi đã tìm thấy một câu hỏi tương tự trên Stack Overflow: https://stackoverflow.com/questions/27593618/
Tôi là một lập trình viên xuất sắc, rất giỏi!