match_parent width does not work in RecyclerView

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

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

           


        
10条回答
  •  眼角桃花
    2020-11-27 13:45

    I was using a FrameLayout with MATCH_PARENT for width and was seeing the same behavior with a RecyclerView + LinearLayoutManager. None of the above changes worked for me until I did the following in the onCreateViewHolder callback:

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // create a new view
        View v = LayoutInflater.from(parent.getContext())
                               .inflate(R.layout.note_layout, parent, false);
        v.setLayoutParams(new RecyclerView.LayoutParams(
              ((RecyclerView) parent).getLayoutManager().getWidth(),
              context.getResources()
                     .getDimensionPixelSize(R.dimen.note_item_height)));
    
        return new ViewHolder(v);
    }
    

    Clearly looks like a bug in (I'm guessing) the RecyclerView implementation.

提交回复
热议问题