RecyclerView显示不全

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);
    }


}

http://img1.sycdn.imooc.com//climg/5d00bae1000102f404640776.jpg

正在回答

登陆购买课程后可参与讨论,去登陆

1回答

是你xml的问题吧,应该是下面的tab遮挡了recyclerView。你给recyclerView一个固定的高度,或者要是用线性布局的话,用比重设置一下。祝:学习愉快

  • 爱笑的syt 提问者 #1
    是这个问题,谢谢老师
    2019-06-12 17:37:42
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师