0
我正在研究三級可擴展列表視圖。一切工作完美地期望,可擴展列表視圖的第二和第三級的textview的寬度包裝其內容,並不符合我在佈局中指定的父級。如何在可擴展列表視圖中製作textview 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" />
更準確地說,我上傳了一張照片,可以清楚地看到我想說的內容。請看看它。我已經打開了來自開發者選項的「顯示佈局界限」,它顯示我的文本視圖只是包裝文本的內容而不是匹配父項。我希望textview可以在整個文檔中進行點擊,而不是直到文本的長度。
請告訴我什麼,我做錯了,需要做的事情來解決這個什麼。