match_parent width does not work in RecyclerView

后端 未结 10 1411
深忆病人
深忆病人 2020-11-27 13:25

My RecyclerView and item has match_parent width but the result is : \"enter

           


        
10条回答
  •  爱一瞬间的悲伤
    2020-11-27 13:53

    In your adapter where you are inflating the item in onCreateViewHolder, is the second parameter of the inflate call null?.

    If so change it to parent which is the first parameter in the onCreateViewHolder function signature.

    View rootView = LayoutInflater.from(context).inflate(R.layout.itemLayout, parent, false);
    

    If you need the second parameter to be null then when you get the view reference on inflating, do the following

    View rootView = LayoutInflater.from(context).inflate(R.layout.itemLayout, null, false);
    RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    rootView.setLayoutParams(lp);
    return new RecyclerViewHolder(rootView);
    

提交回复
热议问题