如何在expandable listview中制作textview MATCH_PARENT

时间:2017-04-07 10:07:06

标签: android textview expandablelistview

我正在开发三级可扩展列表视图。一切正常,期望可扩展列表视图的第二级和第三级的textview的宽度包装其内容,而不是像我在布局中指定的那样匹配父级。

这是我的父级适配器代码:

public class ParentLevelAdapter extends BaseExpandableListAdapter 
{
    private final Context mContext;
    private final List<String> mListDataHeader;
    private final Map<String, List<String>> mListData_SecondLevel_Map;
    private final Map<String, List<String>> mListData_ThirdLevel_Map;
    private List<String> stringList = new ArrayList<>();
    Activity activity;

    public ParentLevelAdapter(Context mContext, List<String> mListDataHeader) {
        activity = (Activity) mContext;
        this.mContext = mContext;
        this.mListDataHeader = new ArrayList<>();
        this.mListDataHeader.addAll(mListDataHeader);
        // SECOND LEVEL
        String[] mItemHeaders = new String[0];
        mListData_SecondLevel_Map = new HashMap<>();
        int parentCount = mListDataHeader.size();
         mItemHeaders = mContext.getResources().getStringArray(R.array.items_array_expandable_level_two);
         mListData_SecondLevel_Map.put(mListDataHeader.get(i), Arrays.asList(mItemHeaders));

        }
        // THIRD LEVEL
        String[] mItemChildOfChild;
        mListData_ThirdLevel_Map = new HashMap<>();
        for (Object o : mListData_SecondLevel_Map.entrySet()) {
            Map.Entry entry = (Map.Entry) o;
            Object object = entry.getValue();
            if (object instanceof List) {
                Collections.addAll(stringList, (String[]) ((List) object).toArray());
                mItemChildOfChild = mContext.getResources().getStringArray(R.array.items_array);
                }
            }
        }
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition,boolean isLastChild, View convertView, ViewGroup parent) 
    {
        final CustomExpandableListView secondLevelExpListView = new CustomExpandableListView(this.mContext);
        String parentNode = (String) getGroup(groupPosition);
        secondLevelExpListView.setAdapter(new SecondLevelAdapter(this.mContext, mListData_SecondLevel_Map.get(parentNode), mListData_ThirdLevel_Map));
        secondLevelExpListView.setGroupIndicator(null);
        secondLevelExpListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
            int previousGroup = -1;

            @Override
            public void onGroupExpand(int groupPosition) {
                if (groupPosition != previousGroup)
                    secondLevelExpListView.collapseGroup(previousGroup);
                previousGroup = groupPosition;
            }
        });

        secondLevelExpListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {

                return false;
            }
        });
        return secondLevelExpListView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return 1;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return this.mListDataHeader.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return this.mListDataHeader.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent) {
        String headerTitle = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater layoutInflater = (LayoutInflater) this.mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.expandable, parent, false);
        }
        TextView lblListHeader = (TextView) convertView.findViewById(R.id.lblListHeader);
        lblListHeader.setText(headerTitle);

        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}

第二级适配器代码:

public class SecondLevelAdapter extends BaseExpandableListAdapter 
{
    private final Context mContext;
    private final List<String> mListDataHeader;
    private final Map<String, List<String>> mListDataChild;

    public SecondLevelAdapter(Context mContext, List<String> mListDataHeader, Map<String, List<String>> mListDataChild) {
        this.mContext = mContext;
        this.mListDataHeader = mListDataHeader;
        this.mListDataChild = mListDataChild;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return this.mListDataChild.get(this.mListDataHeader.get(groupPosition)).get(childPosition);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {
        final String childText = (String) getChild(groupPosition, childPosition);

        if (convertView == null) {
            LayoutInflater layoutInflater = (LayoutInflater) this.mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.expandable_third, parent, false);
        }
        TextView txtListChild = (TextView) convertView.findViewById(R.id.lblListThird);
        txtListChild.setText(childText);
        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        try {
            return this.mListDataChild.get(this.mListDataHeader.get(groupPosition)).size();
        } catch (Exception e) {
            return 0;
        }
    }

    @Override
    public Object getGroup(int groupPosition) {
        return this.mListDataHeader.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return this.mListDataHeader.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent) {
        String headerTitle = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater layoutInflater = (LayoutInflater) this.mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.expandable_list_second, parent, false);
        }
        TextView lblListHeader = (TextView) convertView
                .findViewById(R.id.lblListItem);
        lblListHeader.setText(headerTitle);
        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}

我的列表行代码只包含一个文本框

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
    android:id="@+id/lblListThird"
    android:layout_width="match_parent"
    android:layout_height="@dimen/_35sdp"
    android:textColor="@color/colorWhite"
    android:paddingLeft="@dimen/_20sdp"
    android:layout_marginTop="@dimen/_10sdp"
    android:textSize="25sp"
    android:layout_gravity="center"
    android:gravity="center|start" />

更准确地说,我正在上传一张图片,该图片可以清晰地显示我想说的内容。请看一下。我从开发人员选项中打开了“show layout bounds”,这表明我的textview只包含文本的内容而不是匹配的父文件。我希望textview在整个过程中都是可点击的,而不是直到文本的长度。 enter image description here

请告诉我我做错了什么以及需要做些什么来解决这个问题。

1 个答案:

答案 0 :(得分:0)

对于现在或将来观看此内容的任何人。我通过删除计算宽度的代码解决了它。我不知道这是否是正确的方法,但它确实完成了工作。以下是代码段:

 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
    heightMeasureSpec = MeasureSpec.makeMeasureSpec(999999, MeasureSpec.AT_MOST);
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}