Как да направите текстов изглед в разширяем изглед на списък MATCH_PARENT

Работя върху тристепенен изглед с разширяем списък. Всичко работи перфектно, очаквайте, че ширината на текстовия изглед на второто и третото ниво на разширяемия изглед на списък обгръща съдържанието му и не съответства на родителския, както съм посочил в оформлението.

Ето моят код за адаптер на родителско ниво:

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" />

To be more precise i am uploading a picture which will give a clear view of what i am trying to say. Please have a look at it. I've turned on the "show layout bounds" from developer option which shows that my textview is only wrapping the content of the text and not matching parent. I want the textview to be clickable throughout and not till just the length of the text. enter image description here

Моля, кажете ми какво правя грешно и какво трябва да се направи, за да се поправи това.


person rogue_shadow    schedule 07.04.2017    source източник


Отговори (1)


За всеки, който гледа това сега или в бъдеще. Реших го, като премахнах кода, който изчислява ширината. Не знам дали това е правилният начин, но върши работата. Ето кодовия фрагмент:

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