我有一个列表视图,其中listview的一个项目是可扩展的,我想为列表视图的所有项目设置可绘制的左图标。但是对于可扩展的项目,我不知道该怎么做,有人可以帮助我吗?
这是我的代码
private class HelpListAdapter extends ArrayAdapter {
private Holder holder;
private Context context;
public HelpListAdapter(Context context, int resource) {
super(context, resource);
this.context = context;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
if (view == null) {
holder = new Holder();
if (position == 3) {
view = View.inflate(getContext(), R.layout.list_item_with_expandable_new, null);
holder.expandableListView = (ExpandableListView) view.findViewById(R.id.expand_list);
holder.containerList = (LinearLayout) view.findViewById(R.id.container_expandable_list);
} else {
view = View.inflate(getContext(), R.layout.list_item_text, null);
holder.textView = (TextView) view.findViewById(R.id.text);
}
view.setTag(holder);
} else {
holder = (Holder) view.getTag();
}
if (position != 3) {
holder.textView.setText(mHelpLabels.get(position));
Drawable[] iconLeft = {Util.getCustomDrawableColor(getActivity(),R.drawable.ic_favorite_18dp,R.color.beco_logout_red, PorterDuff.Mode.SRC_ATOP),
Util.getCustomDrawableColor(getActivity(), R.drawable.ic_checkin_18dp, R.color.beco_logout_red, PorterDuff.Mode.DST_IN),
Util.getCustomDrawableColor(getActivity(), R.drawable.ic_faq, R.color.beco_white, PorterDuff.Mode.DST_IN),
Util.getCustomDrawableColor(getActivity(), R.drawable.ic_feedback, R.color.beco_white, PorterDuff.Mode.DST_IN),
Util.getCustomDrawableColor(getActivity(), R.drawable.ic_feedback, R.color.beco_white, PorterDuff.Mode.DST_IN),
Util.getCustomDrawableColor(getActivity(), R.drawable.ic_share_app, R.color.beco_white, PorterDuff.Mode.DST_IN),
Util.getCustomDrawableColor(getActivity(), R.drawable.ic_privacy, R.color.beco_white, PorterDuff.Mode.DST_IN),
Util.getCustomDrawableColor(getActivity(), R.drawable.ic_terms, R.color.beco_white, PorterDuff.Mode.DST_IN),
Util.getCustomDrawableColor(getActivity(), R.drawable.ic_faq, R.color.beco_white, PorterDuff.Mode.DST_IN),
Util.getCustomDrawableColor(getActivity(), R.drawable.ic_beco_settings, R.color.beco_white, PorterDuff.Mode.DST_IN),
Util.getCustomDrawableColor(getActivity(), R.drawable.ic_beco_settings, R.color.beco_white, PorterDuff.Mode.DST_IN),
};
holder.textView.setCompoundDrawablesWithIntrinsicBounds(iconLeft[position], null, null, null);
} else {
holder.expandableListView.setAdapter(new ExpandableListAdapter(context, mHelpLabels.get(position), view));
holder.expandableListView.setOnGroupClickListener((parentExpand, v, groupPosition, id) -> {
setListViewHeight(parentExpand, groupPosition);
return false;
});
}
return view;
}
class Holder {
TextView textView;
ExpandableListView expandableListView;
LinearLayout containerList;
}
private void setListViewHeight(ExpandableListView listView, int group) {
ExpandableListAdapter listAdapter = (ExpandableListAdapter) listView.getExpandableListAdapter();
int height = 0;
int groupCount = 0;
int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.EXACTLY);
View groupItem = listAdapter.getGroupView(groupCount, false, null, listView);
groupItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
height += groupItem.getMeasuredHeight();
if (((listView.isGroupExpanded(groupCount)) && (groupCount != group))
|| ((!listView.isGroupExpanded(groupCount)) && (groupCount == group))) {
for (int j = 0; j < listAdapter.getChildrenCount(groupCount); j++) {
View listItem = listAdapter.getChildView(groupCount, j, false, null, listView);
listItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
height += listItem.getMeasuredHeight();
}
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
if (height < 10)
height = 200;
params.height = height;
listView.setLayoutParams(params);
listView.requestLayout();
}
class ExpandableListAdapter extends BaseExpandableListAdapter {
Context context;
String title;
View viewParent;
ExpandableListAdapter(Context context, String title, View view) {
this.context = context;
this.title = title;
this.viewParent = view;
}
@Override
public String getChild(int groupPosition, int childPosititon) {
return mContactLabels.get(childPosititon);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View view, ViewGroup parent) {
if (view == null) {
LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = infalInflater.inflate(R.layout.list_item_text, null);
}
Drawable[] iconLeft = {Util.getCustomDrawableColor(getActivity(), R.drawable.ic_email, R.color.beco_icon_tint_color, PorterDuff.Mode.SRC_ATOP),
Util.getCustomDrawableColor(getActivity(), R.drawable.ic_phone_black, R.color.beco_icon_tint_color, PorterDuff.Mode.DST_IN)};
TextView textView = (TextView) view.findViewById(R.id.text);
textView.setText(mContactLabels.get(childPosition));
textView.setCompoundDrawablesWithIntrinsicBounds(iconLeft[childPosition], null, null, null);
textView.setOnClickListener(v -> {
switch (childPosition) {
case 0:
Intent intentEmail = new Intent(Intent.ACTION_SENDTO);
intentEmail.setData(Uri.fromParts("mailto", mContactLabels.get(childPosition), null));
intentEmail.putExtra(Intent.EXTRA_SUBJECT, "Contact beCo");
startActivity(intentEmail);
break;
case 1:
Intent intentCall = new Intent(Intent.ACTION_DIAL);
intentCall.setData(Uri.parse("tel:" + mContactLabels.get(childPosition)));
startActivity(intentCall);
break;
}
});
return view;
}
@Override
public int getChildrenCount(int groupPosition) {
return mContactLabels.size();
}
@Override
public Object getGroup(int groupPosition) {
return mContactLabels.get(groupPosition);
}
@Override
public int getGroupCount() {
return 1;
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View view, ViewGroup parent) {
if (view == null) {
LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = infalInflater.inflate(R.layout.list_item_text, null);
}
TextView contact = (TextView) view.findViewById(R.id.text);
contact.setText(title);
Drawable iconLeft = Util.getCustomDrawableColor(getActivity(), R.drawable.ic_contact_blue, R.color.beco_white, PorterDuff.Mode.DST_IN);
contact.setCompoundDrawablesWithIntrinsicBounds(null,null,iconLeft,null);
LinearLayout border = (LinearLayout) viewParent.findViewById(R.id.container_expandable_list);
Drawable iconRight = Util.getCustomDrawableColor(getActivity(), R.drawable.ic_keyboard_arrow_down_black, R.color.beco_black, PorterDuff.Mode.DST_IN);
if (isExpanded) {
border.setBackgroundResource(R.drawable.border);
iconRight = Util.getCustomDrawableColor(getActivity(), R.drawable.ic_keyboard_arrow_up_black, R.color.beco_black, PorterDuff.Mode.DST_IN);
contact.setTextColor(ContextCompat.getColor(getActivity(), R.color.beco_primary));
contact.setCompoundDrawablesWithIntrinsicBounds(null, null, iconRight, null);
} else {
border.setBackgroundResource(0);
contact.setTextColor(ContextCompat.getColor(getActivity(), R.color.beco_label_color));
contact.setCompoundDrawablesWithIntrinsicBounds(null, null, iconRight, null);
}
return view;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
}
答案 0 :(得分:0)
我修好了。 这是我的错。这是由于ExpandableListView的groupIndicator图标。 我将groupIndicator设置为null。 那是在 list_item_with_expandable_new.xml
中<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container_expandable_list"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<ExpandableListView
android:id="@+id/expand_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:groupIndicator="@null"
android:divider="@drawable/list_divider"
android:dividerHeight="@dimen/dp1"
/>
这解决了我的问题