我在TabFragment中有一个Expandablelistview,它运行正常,但我很难在第一个孩子(lblListItem)旁边添加 第二个孩子(lblListValue)。< / p>
第二个子项目也需要通过HashMap填充,我已经在搜索StackOverflow和互联网以收集一些想法,但不知怎的,我找不到有用的东西。我总是陷入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; } }
答案 0 :(得分:0)
这是我的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>