match_parent width does not work in RecyclerView

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

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

           


        
相关标签:
10条回答
  • 2020-11-27 13:47

    I've been stuck with this problem for a while, and the solution that worked for me was placing 2 views with match_parent, one inside the other.

    If I did this on my list item's layout:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:background="#F00"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_height="match_parent">
    
    </RelativeLayout>
    

    although it is a relative layout as others have mentioned, and I am passing the parent view but false in the inflater, it would simply not show up at all (red background to check).

    But, when I did this:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_height="match_parent">
    
        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:background="#0F0"
            android:layout_height="match_parent"/>
    
    </RelativeLayout>
    

    The green layout showed up and took up the whole space.

    So just having a child view inside the main one with match_parent solves the problem, no idea why.

    0 讨论(0)
  • 2020-11-27 13:49

    Inside onCreateViewHolder(...) method of adapter where you are inflating the view.. you have to define the ViewGroup as the parent.This you will get from the 1st parameter of onCreateViewHolder(...) method.

    see the below line in the second parameter i'm passing the ViewGroup. This will automatically match the view to its parent:

    rowView=inflater.inflate(R.layout.home_custom_list, parent,false);
    

    ///the complete code is below

    public View onCreateViewHolder(ViewGroup parent, int position) {
        // TODO Auto-generated method stub
        View rowView;
            LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            rowView=inflater.inflate(R.layout.home_custom_list, parent,false);
    
    0 讨论(0)
  • 2020-11-27 13:50

    This worked for me.

    replace this

       View view = View.inflate(parent.getContext(), R.layout.row_timeline, null);
       return new TimeLineViewHolder(view, viewType);
    

    by this

     View rootView = LayoutInflater.from(context).inflate(R.layout.row_timeline, null, false);
            RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            rootView.setLayoutParams(lp);
            return new TimeLineViewHolder(rootView, viewType);
    
    0 讨论(0)
  • 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);
    
    0 讨论(0)
提交回复
热议问题