我想设置页脚列表视图。但我得到以下错误:
> : FATAL EXCEPTION: main
> 08-19 07:26:53.225: E/AndroidRuntime(1176): java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams
> cannot be cast to android.widget.AbsListView$LayoutParams
> 08-19 07:26:53.225: E/AndroidRuntime(1176): at android.widget.ListView.clearRecycledState(ListView.java:513)
> 08-19 07:26:53.225: E/AndroidRuntime(1176): at android.widget.ListView.resetList(ListView.java:500)
> 08-19 07:26:53.225: E/AndroidRuntime(1176): at android.widget.ListView.setAdapter(ListView.java:442)
> 08-19 07:26:53.225: E/AndroidRuntime(1176): at .SimpeSearch.onClick(SimpeSearch.java:91)
第91行是setAdapter方法:
FrameLayout footerLayout = (FrameLayout) getLayoutInflater().inflate(R.layout.footer_loadmore,null);
TextView footer = (TextView) footerLayout.findViewById(R.id.footer);
listview.addFooterView(footer);
AdapterSimpleSearch ad = new AdapterSimpleSearch(this,R.layout.row_list_simple_search,10, footer);
listview.setAdapter(ad);
footer_loadmore:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="6dp"
>
<TextView android:id="@+id/footer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textAppearance="@android:style/TextAppearance.Medium"
android:text="Loading..."
/>
</FrameLayout>
构造函数:
public AdapterSimpleSearch(Context context,int layoutResourceId,int pageSize, TextView footer){
super(context,layoutResourceId);
我该如何解决?
更新:
我的适配器:
public AdapterSimpleSearch(Context context,int layoutResourceId,int pageSize, TextView footer){
super(context,layoutResourceId);
helperbooks = new ArrayList<>();
this.context = context ;
//this.helperbooks = hbooks ;
this.mFooter = footer ;
inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.mPageSize = pageSize ;
}
public void notifyNoMoreItems(){
mHasMoreItems = false;
mFooter.setText("No more Items");
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return this.helperbooks.size();
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public static class ViewHolder{
TextView txt_view_title,txt_view_author,txt_view_publisher ;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if(position == getCount() - 1 && mHasMoreItems){
LoaderTaskSimpleSearch t = new LoaderTaskSimpleSearch(position + 1, position + 1 + mPageSize, context,this,"book");
t.execute();
mFooter.setText("Loading . . .");
}
ViewHolder viewholder ;
if(convertView == null){
convertView = this.inflater.inflate(R.layout.row_list_simple_search, null);
viewholder = new ViewHolder();
viewholder.txt_view_title = (TextView)convertView.findViewById(R.id.txt_view_title_list_simple_search);
viewholder.txt_view_author = (TextView)convertView.findViewById(R.id.txt_view_author_list_simple_search);
viewholder.txt_view_publisher = (TextView)convertView.findViewById(R.id.txt_view_publisher_list_simple_search);
convertView.setTag(viewholder);
}else
viewholder = (ViewHolder)convertView.getTag();
viewholder.txt_view_title.setText(helperbooks.get(position).getTitle());
viewholder.txt_view_author.setText(helperbooks.get(position).getAuthor());
viewholder.txt_view_publisher.setText(helperbooks.get(position).getPublisher());
return convertView;
}
}
答案 0 :(得分:6)
从页脚XML中删除FrameLayout
父级。您所需要的只是TextView
。您可以使用边距填充。
(或者,只需将整个FrameLayout
设置为页脚视图。)
发生错误是因为footer
父级是FrameLayout
而ListView
需要控制父级本身。
所以,XML:
<?xml version="1.0" encoding="utf-8"?>
<TextView android:id="@+id/footer"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_margin="6dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textAppearance="@android:style/TextAppearance.Medium"
android:text="Loading..."
/>
和代码:
View footer = getLayoutInflater().inflate(R.layout.footer_loadmore, null);
listview.addFooterView(footer);
答案 1 :(得分:1)
这对我有帮助,可以在android文档中找到。 (以JetPack为例)
尝试先设置适配器,然后添加页脚/页眉。
val binding = ProductDetailFragmentBinding.inflate(inflater, container, false)
val headerView = ProductListHeaderBinding.inflate(inflater, container, false)
val footerView = ProductListFooterBinding.inflate(inflater, container, false)
binding.productListView.adapter = adapter
binding.productListView.addHeaderView(headerView.root)
binding.productListView.addFooterView(footerView.root)
答案 2 :(得分:0)
使用这个。
View footerLayout = ((LayoutInflater) this.getSystemService(
Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer_loadmore,
null, false);
listview.addFooterView(footerLayout);
同时将height
的{{1}}和width
更改为wrap_content。