我想在listView中添加标题我尝试这样我在这里缺少的ids很好。 我在片段中使用它。
ListView lv;
LinearLayout header = (LinearLayout) rootView.findViewById(R.id.header_layout);
lv.addHeaderView(header);
// APP is crashing here?
logcat的
java.lang.ClassCastException: android.widget.AbsListView$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams
at android.widget.RelativeLayout$DependencyGraph.findRoots(RelativeLayout.java:1732)
at android.widget.RelativeLayout$DependencyGraph.getSortedViews(RelativeLayout.java:1677)
at android.widget.RelativeLayout.sortChildren(RelativeLayout.java:381)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:389)
at android.view.View.measure(View.java:17547)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17547)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at android.view.View.measure(View.java:17547)
答案 0 :(得分:7)
我们可以将标题添加到ListView,如下所示:
LayoutInflater myinflater = getLayoutInflater();
ViewGroup myHeader = (ViewGroup)myinflater.inflate(R.layout.headerlayout, myListView, false);
myListView.addHeaderView(myHeader, null, false);
但是,根据您的错误,您可能已经在xml和java文件中使用了相对布局,您正在使用LinearLayout。
您还必须将ListView初始化为:
lv=(ListView)findViewById(R.id.yourlistview);`
答案 1 :(得分:0)
您需要在设置标题视图之前初始化列表视图。
类似这样的事情
lv = (ListView) findViewById(R.id.list);
然后你可以在膨胀标题视图后添加标题视图。
View head = (View) getActivity().getLayoutInflater().inflate(R.layout. header_layout, null);
lv.addHeaderView(header);
答案 2 :(得分:0)
将此添加到您的适配器:
@Override
public boolean isEmpty() {
return false;
}
如果您有SectionedBaseAdapter:
,请将其添加到您的适配器
@Override
public int getCountForSection(int section) {
switch (section) {
case 0:
return listPOJOInAdapter== null ? 1 : listPOJOInAdapter.size();
}
return 1;
}
将HeaderView添加到ListView后,尝试设置ListView的适配器。
希望它会对你有所帮助!如果你获得成功,请告诉我!