RecyclerView显示不全
我的recyclerView定义了八个元素,实际效果只显示了六个,按照Adapter中getItemCount返回值为8,按照网上说的也尝试在RecyclerView外层套嵌相对布局都不好使,请老师帮忙解惑
public class HomeFragmentDishAdapter extends RecyclerView.Adapter<HomeFragmentDishViewHolder> { Context context; List<HomeFragmentDIsh> ItemList; public HomeFragmentDishAdapter(Context context, List<HomeFragmentDIsh> itemList) { this.context = context; ItemList = itemList; } @NonNull @Override public HomeFragmentDishViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { View view = LayoutInflater.from(context).inflate(R.layout.home_fragment_dish, viewGroup,false); HomeFragmentDIsh hfd = ItemList.get(i); return new HomeFragmentDishViewHolder(view); } @Override public void onBindViewHolder(@NonNull HomeFragmentDishViewHolder homeFragmentDishViewHolder, int i) { homeFragmentDishViewHolder.Icon.setImageResource(ItemList.get(i).Icon); homeFragmentDishViewHolder.DishName.setText(ItemList.get(i).DishName); homeFragmentDishViewHolder.DishDiscripe.setText(ItemList.get(i).DishPrice); homeFragmentDishViewHolder.DishPrice.setText(ItemList.get(i).DishPrice); homeFragmentDishViewHolder.DishNumsofSells.setText(ItemList.get(i).DishNumsOfSell ); } @Override public int getItemCount() { return ItemList!=null ? ItemList.size() : 0; } } class HomeFragmentDishViewHolder extends RecyclerView.ViewHolder{ ImageView Icon; TextView DishName; TextView DishDiscripe; TextView DishPrice; TextView DishNumsofSells; public HomeFragmentDishViewHolder(@NonNull View itemView){ super(itemView); Icon = (ImageView)itemView.findViewById(R.id.imageView); DishName = (TextView) itemView.findViewById(R.id.textView_DishName); DishDiscripe = (TextView) itemView.findViewById(R.id.textView_DishDiscripe); DishPrice = (TextView) itemView.findViewById(R.id.textView_DishPrice); DishNumsofSells = (TextView) itemView.findViewById(R.id.textView_NumofSell); } }
public class Fragment_main extends Fragment { int[] Icons; String[] DishNames; String[] DishDescripes; String[] DishPrices; String[] DishNumsofSells; RecyclerView RecyclerViewMainFragmentDish; public Fragment_main() { // Required empty public constructor } /** * Use this factory method to create a new instance of * this fragment using the provided parameters. * * @return A new instance of fragment fragment_main. */ // TODO: Rename and change types and number of parameters public static Fragment_main newInstance() { Fragment_main fragment = new Fragment_main(); return fragment; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_main, container, false); } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); initView(); RecyclerViewMainFragmentDish.setAdapter(new HomeFragmentDishAdapter(getActivity(), DataUtil.getHomeFragmentDishList(Icons, DishNames, DishDescripes, DishPrices, DishNumsofSells))); LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity()); linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL); RecyclerViewMainFragmentDish.setLayoutManager(linearLayoutManager); } private void initView() { Icons =new int[]{R.drawable.pro1,R.drawable.pro2,R.drawable.product3,R.drawable.product4,R.drawable.pro1,R.drawable.pro2,R.drawable.product3,R.drawable.product4}; DishNames = new String[]{"牛排","地三鲜","松仁大虾","冷饮","羊排","海三鲜","松仁玉米","可乐"}; DishDescripes =new String[]{"超值单人套餐,三十分钟极速配送","营养搭配,科学饮食更健康!","海的味道,我知道!","冰霜冷饮吃吃吃!","超值单人套餐,三十分钟极速配送","营养搭配,科学饮食更健康!","海的味道,我知道!","冰霜冷饮吃吃吃!"}; DishPrices = new String[]{"39.3新用户一元抢~!","9.9新用户一元抢~!","19.9新用户一元抢~!","48.5新用户一元抢~!","39.3新用户一元抢~!","9.9新用户一元抢~!","19.9新用户一元抢~!","48.5新用户一元抢~!"}; DishNumsofSells = new String[]{"已售:20份","已售:30份","已售:20份","已售:40份","已售:20份","已售:30份","已售:20份","已售:40份"}; RecyclerViewMainFragmentDish = getView().findViewById(R.id.recyclerView_dish); } }
0
收起
正在回答
1回答
是你xml的问题吧,应该是下面的tab遮挡了recyclerView。你给recyclerView一个固定的高度,或者要是用线性布局的话,用比重设置一下。祝:学习愉快
1.Android 零基础入门
- 参与学习 人
- 提交作业 1789 份
- 解答问题 2907 个
Android大楼Java起,本阶段是Android攻城狮培养计划的第一部分语法与界面基础篇,将带大家从0开始入门Android开发。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星