上代码:
布局代码:
RecycleView.xml:
<android.support.v7.widget.RecyclerView
android:id="@+id/list_course"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical">
</android.support.v7.widget.RecyclerView>
listView.xml
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/manage_course_list"
></ListView>
我们需要重写listView的方法,重新计算listView高度,在listView创建适配器下添加如下:
private ListView manage_course_list;
manage_course_list = itemView.findViewById(R.id.manage_course_list);
manage_course_list.setAdapter(adapter);
adapter.notifyDataSetChanged();//监听数据变化
Utility.setListViewHeightBasedOnChildren(manage_course_list);//就是这句
加的就是这句
Utility.setListViewHeightBasedOnChildren(manage_course_list);
(是不是报错啦,因为没这个类啊,👇看下)
所以我们创建一个Utility类对ListView进行重写:
public class Utility {
public static void setListViewHeightBasedOnChildren(ListView listview){
ListAdapter listAdapter=listview.getAdapter();
if(listAdapter==null) {
return;
}
int totalHeight=0;
for(int i=0,len=listAdapter.getCount();i<len;i++){
View listItem=listAdapter.getView(i,null,listview);
listItem.measure(0,0);
totalHeight+=listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params=listview.getLayoutParams();
params.height=totalHeight+(listview.getDividerHeight()*(listAdapter.getCount()-1));
listview.