2
我想自定義ExpandableListView,但我無法自定義孩子。我怎樣才能做一個邊框和填充背景(下面的示例代碼SRC)ExpandableListView自定義設計
我有什麼:
ExpandableListView
<ExpandableListView
android:id="@+id/expListviewServices"
android:layout_width="match_parent"
android:divider="@color/white"
android:dividerHeight="10dp"
android:childDivider="@color/white"
android:layout_height="match_parent" />
list_fragment_services_groups.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:paddingLeft="50dp"
android:background="@drawable/expandblelistview_background"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imgServicesGroup"
android:src="@drawable/ic_internet" />
<TextView
android:layout_width="wrap_content"
android:id="@+id/txtServicesGroup"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/black"
android:layout_toRightOf="@id/imgServicesGroup"
android:paddingLeft="15dp"
android:textStyle="bold"
android:layout_height="wrap_content" />
</RelativeLayout>
list_fragment_services_child.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_marginLeft="40dp"
android:background="@color/white"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:id="@+id/txtServicesChild"
android:paddingLeft="50dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:textColor="@color/black"
android:layout_height="wrap_content" />
</RelativeLayout>
expandblelistview_background.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/lightgray" />
<corners android:radius="10dp"/>
</shape>
我想要什麼:
地址:
public class ServicesAdapter extends BaseExpandableListAdapter {
private static final String TAG = ServicesAdapter.class.getSimpleName();
private ArrayList<HashMap<String, String>> group;
ArrayList<ArrayList<HashMap<String, String>>> child;
private Context context;
private static String GROUP_NAME = "group_name";
private static String GROUP_IMAGE = "group_image";
private static String CHILD_NAME = "child_name";
public ServicesAdapter(Context context, ArrayList<HashMap<String, String>> group, ArrayList<ArrayList<HashMap<String, String>>> child) {
this.context = context;
this.group = group;
this.child = child;
}
@Override
public int getGroupCount() {
return group.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return child.get(groupPosition).size();
}
@Override
public Object getGroup(int groupPosition) {
return group.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return child.get(groupPosition).get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_fragment_services_groups, null);
}
if (isExpanded) {
convertView.setBackgroundResource(R.drawable.expandblelistview_background_selected);
} else {
convertView.setBackgroundResource(R.drawable.expandblelistview_background);
}
ImageView imgServicesGroup = (ImageView) convertView.findViewById(R.id.imgServicesGroup);
TextView txtServicesGroup = (TextView) convertView.findViewById(R.id.txtServicesGroup);
txtServicesGroup.setText(group.get(groupPosition).get(GROUP_NAME));
int imageId = context.getResources().getIdentifier("ic_" + group.get(groupPosition).get(GROUP_IMAGE), "drawable", context.getPackageName());
imgServicesGroup.setImageResource(imageId);
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(
R.layout.list_fragment_services_child, null);
}
TextView txtServicesChild = (TextView) convertView.findViewById(R.id.txtServicesChild);
txtServicesChild.setText(child.get(groupPosition).get(childPosition).get(CHILD_NAME));
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
}
你能告訴你的適配器的代碼? –
@MarkBuikema是的,我編輯了我的問題 – WOLVERINE
您可以在'getChildView()'方法中設置子視圖的樣式。 –