本文主要是介绍ScrollView嵌套RecyclerView再嵌套RecyclerView导致的布局展示不完整问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
背景:页面布局,最外层有个ScrollView,然后里面有个RecyclerView,然后每个RecyclerView的item都是一个RecyclerView
异常:页面展示不完整,最底下的Item 展示一半,在往上滑就滑不动了
解决:
// 每一个item渲染完后重新计算外层recyclerview高度 // 因为外层的recyclerview是先渲染的,渲染时 内部recyclerview无数据此时高度是0 // 当内部recyclerview 渲染完后要重新计算高度 外部recyclerview才可以撑开//第一步:测量内部recyclerView高度 recommendedRvViewHolder.recyclerView.measure(0, 0);//第二步:重新设置外层的recyclerview高度 已经有的高度 + 新渲染Item的高度 ViewGroup.LayoutParams params = recyclerView.getLayoutParams(); //这里的30 是布局文件中"为你推荐"、"热门榜单"所在布局的高度 18 + 两个item之间的间距 9 27 取整 30 params.height += recommendedRvViewHolder.recyclerView.getMeasuredHeight() + DensityUtil.dip2px(mContext, 30); recyclerView.setLayoutParams(params);
看下图:最底下的item展示出来了
部分核心的代码:
<?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="match_parent"android:background="@color/common_white"android:orientation="vertical"><include layout="@layout/search_layout"></include><ScrollViewandroid:layout_width="match_parent"android:layout_height="match_parent"android:scrollbars="none"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><com.youth.banner.Bannerandroid:id="@+id/star_film_home_banner"android:layout_width="match_parent"android:layout_height="158dp"android:visibility="invisible" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="5dp"android:background="@color/film_common_bg_gray" /><android.support.v7.widget.RecyclerViewandroid:id="@+id/star_film_recommend_rv"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"android:layout_marginTop="10dp"android:overScrollMode="never"android:scrollbars="none" /></LinearLayout></ScrollView>
</LinearLayout>
public class RecommendedListRvAdapter extends RecyclerView.Adapter<RecommendedRvViewHolder> {/*** 查询的每页数量*/private final int PAGE_SIZE = 6;private Context mContext;private List<CategoryObjectV1> cats;private RecyclerView recyclerView;public RecommendedListRvAdapter(Context context, List<CategoryObjectV1> cats, RecyclerView rv) {this.mContext = context;this.cats = cats;this.recyclerView = rv;}@NonNull@Overridepublic RecommendedRvViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {View view = LayoutInflater.from(mContext).inflate(R.layout.recommended_recy_item, null);return new RecommendedRvViewHolder(view);}@Overridepublic void onBindViewHolder(@NonNull final RecommendedRvViewHolder recommendedRvViewHolder, final int i) {try {recommendedRvViewHolder.recommend_item_tv.setText(cats.get(i).getLanguages().get(0).getName());recommendedRvViewHolder.recommend_item_ll.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//跳转到推荐二级页面Intent intent = new Intent(mContext, StarFilmSecondActivity.class);intent.putExtra(StarConstants.INTENT_CAT_ID, cats.get(i).getId());mContext.startActivity(intent);}});// 设置频道列表数据recommendedRvViewHolder.recyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));recommendedRvViewHolder.recyclerView.addItemDecoration(new DividerGridItemDecoration(mContext, mContext.getResources().getDrawable(R.drawable.recy_custom_divider)));//请求目录下的推荐内容并渲染UIStarCoreVariable.threadPoolUtil.poolExecute(new Runnable() {@Overridepublic void run() {final OndemandContentSimplePageCacheDTO ondemandContentSimplePageCacheDTO = VodService.getInstance().requestVodContentFromServer(ServerConfigLocalConstants.SERVER_UP_URL, cats.get(i).getId(), 1, PAGE_SIZE);MainUIHandler.handler().post(new Runnable() {@Overridepublic void run() {FilmChannelRecyAdapter filmChannelRecyAdapter = new FilmChannelRecyAdapter(mContext, ondemandContentSimplePageCacheDTO);recommendedRvViewHolder.recyclerView.setAdapter(filmChannelRecyAdapter);// 每一个item渲染完后重新计算外层recyclerview高度// 因为外层的recyclerview是先渲染的,渲染时 内部recyclerview无数据此时高度是0// 当内部recyclerview 渲染完后要重新计算高度 外部recyclerview才可以撑开recommendedRvViewHolder.recyclerView.measure(0, 0);ViewGroup.LayoutParams params = recyclerView.getLayoutParams();//这里的30 是布局文件中"为你推荐"、"热门榜单"所在布局的高度 18 + 两个item之间的间距 9 27 取整 30params.height += recommendedRvViewHolder.recyclerView.getMeasuredHeight() + DensityUtil.dip2px(mContext, 30);recyclerView.setLayoutParams(params);}});}});} catch (Exception e) {e.printStackTrace();}}@Overridepublic int getItemCount() {return cats.size();}
}
public class RecommendedRvViewHolder extends RecyclerView.ViewHolder {public LinearLayout recommend_item_ll;public TextView recommend_item_tv;public RecyclerView recyclerView;public RecommendedRvViewHolder(@NonNull View itemView) {super(itemView);recommend_item_ll = itemView.findViewById(R.id.rec_item_all_ll);recommend_item_tv = itemView.findViewById(R.id.rec_item_name_tv);recyclerView = itemView.findViewById(R.id.rec_item_rv);}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="18dp"android:orientation="vertical"><ImageViewandroid:id="@+id/rec_item_iv"android:layout_width="5dp"android:layout_height="14dp"android:layout_centerVertical="true"android:background="@color/film_common_light_red" /><TextViewandroid:id="@+id/rec_item_name_tv"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerVertical="true"android:layout_marginLeft="10dp"android:layout_toRightOf="@id/rec_item_iv"android:textColor="@color/film_common_text_black"android:textSize="@dimen/film_common_text_size_big"tools:text="为你推荐" /><LinearLayoutandroid:id="@+id/rec_item_all_ll"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:layout_centerVertical="true"android:layout_marginRight="9dp"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_vertical"android:layout_marginRight="4dp"android:text="热门榜单"android:textColor="@color/film_common_text_gray"android:textSize="@dimen/film_common_text_size_small" /><ImageViewandroid:layout_width="8dp"android:layout_height="7dp"android:layout_gravity="center_vertical"android:background="@drawable/common_arrow_right" /></LinearLayout></RelativeLayout><android.support.v7.widget.RecyclerViewandroid:id="@+id/rec_item_rv"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginLeft="9dp"android:layout_marginTop="9dp"android:layout_marginRight="9dp"android:scrollbars="none" /></LinearLayout>
这篇关于ScrollView嵌套RecyclerView再嵌套RecyclerView导致的布局展示不完整问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!