我在我的主tablayout
中使用了材料设计activity
,其中我有scrollable
个标签,在我的第一个fragment
我使用的是可展开的listview
。
这是我的片段代码
public class House_Hold_List_Fragment extends Fragment {
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<HouseHoldList_Model> listDataHeader;
HashMap<HouseHoldList_Model, List<String>> listDataChild;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.house_hold_list_fragment, container, false);
setHasOptionsMenu(true);
expListView = (ExpandableListView) view.findViewById(R.id.lvExp);
listDataHeader = new ArrayList<HouseHoldList_Model>();
listDataChild = new HashMap<HouseHoldList_Model, List<String>>();
loadModel();
// preparing list data
prepareListData();
listAdapter = new ExpandableListAdapter(getActivity(), listDataHeader, listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
return view;
}
public void prepareListData(){
List<String> top250 = new ArrayList<String>();
top250.add("The Shawshank Redemption");
top250.add("The Godfather");
top250.add("The Godfather: Part II");
top250.add("Pulp Fiction");
top250.add("The Good, the Bad and the Ugly");
top250.add("The Dark Knight");
top250.add("12 Angry Men");
List<String> nowShowing = new ArrayList<String>();
nowShowing.add("The Conjuring");
nowShowing.add("Despicable Me 2");
nowShowing.add("Turbo");
nowShowing.add("Grown Ups 2");
nowShowing.add("Red 2");
nowShowing.add("The Wolverine");
List<String> comingSoon = new ArrayList<String>();
comingSoon.add("2 Guns");
comingSoon.add("The Smurfs 2");
comingSoon.add("The Spectacular Now");
comingSoon.add("The Canyons");
comingSoon.add("Europa Report");
listDataChild.put((HouseHoldList_Model)listDataHeader.get(0), top250); // Header, Child data
listDataChild.put(listDataHeader.get(1), nowShowing);
listDataChild.put(listDataHeader.get(2), comingSoon);
}
public void loadModel(){
listDataHeader.add(new HouseHoldList_Model("A"));
listDataHeader.add(new HouseHoldList_Model("B"));
listDataHeader.add(new HouseHoldList_Model("C"));
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// TODO Auto-generated method stub
//super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.search:
//do something
Log.e("search clicked","sd");
return true;
case R.id.action_settings:
//do something
Log.e("search clicked","sd");
return true;
}
return super.onOptionsItemSelected(item);
}
}
这是我的模型类代码,我只是添加名称
public class HouseHoldList_Model {
String name;
public HouseHoldList_Model(){
}
public HouseHoldList_Model(String name){
this.name=name;
}
}
这是我的 ExpandableListAdapter 类代码
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context _context;
private List<HouseHoldList_Model> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<HouseHoldList_Model
, List<String>> _listDataChild;
public ExpandableListAdapter(Context context, List<HouseHoldList_Model> listDataHeader,
HashMap<HouseHoldList_Model, List<String>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}
@Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@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.house_holld_list_item, null);
}
TextView txtListChild = (TextView) convertView
.findViewById(R.id.house_hold_list_item);
txtListChild.setText(childText);
return convertView;
}
@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 int getGroupCount() {
return this._listDataHeader.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
HouseHoldList_Model model = (HouseHoldList_Model) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.house_hold_list_row, null);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.house_hold_name);
lblListHeader.setText(model.name);
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
我的 house_hold_list_row 是行的自定义布局,其中我有不同的字段,如姓名,地址等,但当时我只显示名称。 这里是我的logcat。
11-13 18:03:05.565 6756-6756/com.zaigham.lhw_diary E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.zaigham.lhw_diary, PID: 6756
android.view.InflateException: Binary XML file line #0: Error inflating class TextView
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:714)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:756)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:759)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:759)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:759)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:759)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at com.zaigham.lhw_diary.HouseHoldList.ExpandableListAdapter.getGroupView(ExpandableListAdapter.java:86)
这是我的 house_hold_list_row
<?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="match_parent"
android:background="@color/gray">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:layout_centerInParent="true"
android:layout_marginRight="5dp"
android:layout_marginLeft="5dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3">
<TextView
android:id="@+id/house_hold_number"
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="1"
android:layout_gravity="center_vertical"
android:layout_marginLeft="5dp"
android:padding="8dp"
android:gravity="center"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:textColor="@color/white"
android:background="@drawable/hh_count_round"
android:layout_weight=".5"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_weight="2.2"
android:orientation="vertical">
<TextView
android:id="@+id/house_hold_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="18dp"
android:textColor="@color/blacktext"
android:text="Muhammad Ramza"/>
<TextView
android:id="@+id/house_hold_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Main double road g-11"
android:textColor="@color/blacktext"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Family Members"
android:textColor="@color/blacktext" />
<TextView
android:id="@+id/house_hold_fm_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="10"
android:layout_marginLeft="10dp"
android:background="@drawable/hh_fm_count_round"
android:textColor="@color/white"/>
</LinearLayout>
</LinearLayout>
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:src="@drawable/down"
android:layout_marginRight="5dp"
android:layout_gravity="center"
android:layout_weight=".3"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
答案 0 :(得分:0)
IMO你的textview无法引用convertView所以把它带到你的if()条件下试试下面的代码
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.house_hold_list_row, null)
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.house_hold_name);
lblListHeader.setText(model.name);
}
答案 1 :(得分:0)
This will work
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
HouseHoldList_Model model = (HouseHoldList_Model) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.house_hold_list_row, null);
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.house_hold_name);
}
lblListHeader.setText(model.name);
return convertView;
}