自定义 Expandablelistview 添加第二个 child 显示在 child1 旁边
Custom Expandablelistview adding 2nd child to be shown next to child1
我在 TabFragment 中有一个 Expandablelistview,它工作正常,但我正在努力 add a 2nd child (lblListValue) 在第一个 child (lblListItem) 旁边。
第二个childitem也需要通过HashMap填充,我已经搜索了Whosebug和互联网来收集一些想法但不知何故我找不到有用的东西.我总是卡在 ExpandableListAdapter 代码中,所以我再次从头开始尝试。
非常欢迎对此提供帮助。
编辑:实际上 Frank N. Stein 的回答是正确的,应该删除 LinearLayout。
list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/lblListItem"
android:paddingTop="5sp"
android:paddingBottom="5dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="man1"
android:textSize="14sp"
android:textStyle="bold" >
</TextView>
<TextView
android:id="@+id/lblListValue" <--- 2nd child to be displayed
android:paddingTop="5sp"
android:paddingBottom="5dp"
android:paddingRight="20sp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:text="test1"
android:textSize="14sp">
</TextView>
</RelativeLayout>
</LinearLayout>
TabFragment:
public class TabFragment1 extends Fragment {
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
View rootView;
public TabFragment1() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.tab_fragment_1, container, false);
return rootView;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// get the listview
expListView = (ExpandableListView) rootView.findViewById(R.id.expListView);
// preparing list data
prepareListData();
listAdapter = new ExpandableListAdapter(this.getActivity(), listDataHeader, listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
// Listview on child click listener
expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Toast.makeText(
getActivity(),
listDataHeader.get(groupPosition)
+ " : "
+ listDataChild.get(
listDataHeader.get(groupPosition)).get(
childPosition), Toast.LENGTH_SHORT)
.show();
return false;
}
});
}
/* * Preparing the list data */
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
// Adding child data
listDataHeader.add("Factuurgegevens");
listDataHeader.add("Contactgegevens");
listDataHeader.add("Betalingsgegevens");
listDataHeader.add("Mijn account");
listDataHeader.add("Wachtwoord");
// Adding child data
List<String> Factuurgegevens = new ArrayList<String>();
Factuurgegevens.add("Naam");
Factuurgegevens.add("Straat en huisnummer");
Factuurgegevens.add("Postcode en woonplaats");
List<String> Contactgegevens = new ArrayList<String>();
Contactgegevens.add("Telefoonummer");
Contactgegevens.add("E-mail");
List<String> Betalingsgegevens = new ArrayList<String>();
Betalingsgegevens.add("Rekeningnummer");
Betalingsgegevens.add("Naam rekeninghouder");
List<String> Mijn_account = new ArrayList<String>();
Mijn_account.add("Gebruikersnaam");
List<String> Wachtwoord = new ArrayList<String>();
Wachtwoord.add("Wachtwoord");
listDataChild.put(listDataHeader.get(0), Factuurgegevens); // Header, Child data
listDataChild.put(listDataHeader.get(1), Contactgegevens);
listDataChild.put(listDataHeader.get(2), Betalingsgegevens);
listDataChild.put(listDataHeader.get(3), Mijn_account);
listDataChild.put(listDataHeader.get(4), Wachtwoord);
}
}
ExpandableListAdapter:
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;
//private final LayoutInflater inf;
public ExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
//inf = LayoutInflater.from(MainActivity.mContext);
}
@Override
public int getGroupCount() {
return this._listDataHeader.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
}
@Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
}
TextView txtListChild = (TextView) convertView
.findViewById(R.id.lblListItem);
txtListChild.setText(childText);
return convertView;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_group, null);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.lblListHeader);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
这是我的 list_item.xml 版本:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/lblListItem"
android:paddingTop="5sp"
android:paddingBottom="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="man1"
android:textSize="14sp"
android:textStyle="bold"
/>
<TextView
android:id="@+id/lblListValue"
android:paddingTop="5sp"
android:paddingBottom="5dp"
android:paddingRight="20sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/lblListItem"
android:text="test1"
android:textSize="14sp"
/>
</RelativeLayout>
我在 TabFragment 中有一个 Expandablelistview,它工作正常,但我正在努力 add a 2nd child (lblListValue) 在第一个 child (lblListItem) 旁边。
第二个childitem也需要通过HashMap填充,我已经搜索了Whosebug和互联网来收集一些想法但不知何故我找不到有用的东西.我总是卡在 ExpandableListAdapter 代码中,所以我再次从头开始尝试。
非常欢迎对此提供帮助。
编辑:实际上 Frank N. Stein 的回答是正确的,应该删除 LinearLayout。
list_item.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/lblListItem" android:paddingTop="5sp" android:paddingBottom="5dp" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="man1" android:textSize="14sp" android:textStyle="bold" > </TextView> <TextView android:id="@+id/lblListValue" <--- 2nd child to be displayed android:paddingTop="5sp" android:paddingBottom="5dp" android:paddingRight="20sp" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="right" android:text="test1" android:textSize="14sp"> </TextView> </RelativeLayout> </LinearLayout>
TabFragment:
public class TabFragment1 extends Fragment { ExpandableListAdapter listAdapter; ExpandableListView expListView; List<String> listDataHeader; HashMap<String, List<String>> listDataChild; View rootView; public TabFragment1() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { rootView = inflater.inflate(R.layout.tab_fragment_1, container, false); return rootView; } @Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); // get the listview expListView = (ExpandableListView) rootView.findViewById(R.id.expListView); // preparing list data prepareListData(); listAdapter = new ExpandableListAdapter(this.getActivity(), listDataHeader, listDataChild); // setting list adapter expListView.setAdapter(listAdapter); // Listview on child click listener expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { Toast.makeText( getActivity(), listDataHeader.get(groupPosition) + " : " + listDataChild.get( listDataHeader.get(groupPosition)).get( childPosition), Toast.LENGTH_SHORT) .show(); return false; } }); } /* * Preparing the list data */ private void prepareListData() { listDataHeader = new ArrayList<String>(); listDataChild = new HashMap<String, List<String>>(); // Adding child data listDataHeader.add("Factuurgegevens"); listDataHeader.add("Contactgegevens"); listDataHeader.add("Betalingsgegevens"); listDataHeader.add("Mijn account"); listDataHeader.add("Wachtwoord"); // Adding child data List<String> Factuurgegevens = new ArrayList<String>(); Factuurgegevens.add("Naam"); Factuurgegevens.add("Straat en huisnummer"); Factuurgegevens.add("Postcode en woonplaats"); List<String> Contactgegevens = new ArrayList<String>(); Contactgegevens.add("Telefoonummer"); Contactgegevens.add("E-mail"); List<String> Betalingsgegevens = new ArrayList<String>(); Betalingsgegevens.add("Rekeningnummer"); Betalingsgegevens.add("Naam rekeninghouder"); List<String> Mijn_account = new ArrayList<String>(); Mijn_account.add("Gebruikersnaam"); List<String> Wachtwoord = new ArrayList<String>(); Wachtwoord.add("Wachtwoord"); listDataChild.put(listDataHeader.get(0), Factuurgegevens); // Header, Child data listDataChild.put(listDataHeader.get(1), Contactgegevens); listDataChild.put(listDataHeader.get(2), Betalingsgegevens); listDataChild.put(listDataHeader.get(3), Mijn_account); listDataChild.put(listDataHeader.get(4), Wachtwoord); } }
ExpandableListAdapter:
public class ExpandableListAdapter extends BaseExpandableListAdapter { private Context _context; private List<String> _listDataHeader; // header titles // child data in format of header title, child title private HashMap<String, List<String>> _listDataChild; //private final LayoutInflater inf; public ExpandableListAdapter(Context context, List<String> listDataHeader, HashMap<String, List<String>> listChildData) { this._context = context; this._listDataHeader = listDataHeader; this._listDataChild = listChildData; //inf = LayoutInflater.from(MainActivity.mContext); } @Override public int getGroupCount() { return this._listDataHeader.size(); } @Override public int getChildrenCount(int groupPosition) { return this._listDataChild.get(this._listDataHeader.get(groupPosition)) .size(); } @Override public Object getGroup(int groupPosition) { return this._listDataHeader.get(groupPosition); } @Override public Object getChild(int groupPosition, int childPosititon) { return this._listDataChild.get(this._listDataHeader.get(groupPosition)) .get(childPosititon); } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public boolean hasStableIds() { return false; } @Override public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { final String childText = (String) getChild(groupPosition, childPosition); if (convertView == null) { LayoutInflater infalInflater = (LayoutInflater) this._context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = infalInflater.inflate(R.layout.list_item, null); } TextView txtListChild = (TextView) convertView .findViewById(R.id.lblListItem); txtListChild.setText(childText); return convertView; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { String headerTitle = (String) getGroup(groupPosition); if (convertView == null) { LayoutInflater infalInflater = (LayoutInflater) this._context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = infalInflater.inflate(R.layout.list_group, null); } TextView lblListHeader = (TextView) convertView .findViewById(R.id.lblListHeader); lblListHeader.setTypeface(null, Typeface.BOLD); lblListHeader.setText(headerTitle); return convertView; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } }
这是我的 list_item.xml 版本:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/lblListItem"
android:paddingTop="5sp"
android:paddingBottom="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="man1"
android:textSize="14sp"
android:textStyle="bold"
/>
<TextView
android:id="@+id/lblListValue"
android:paddingTop="5sp"
android:paddingBottom="5dp"
android:paddingRight="20sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/lblListItem"
android:text="test1"
android:textSize="14sp"
/>
</RelativeLayout>