Recyclerview项在与父项匹配时并不会占用屏幕的所有宽度 [英] Recyclerview item doesn't take all width of the screen while giving it match parent

查看:58
本文介绍了Recyclerview项在与父项匹配时并不会占用屏幕的所有宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的问题,同时对它显示的评论进行新的recyclerview,如下图所示

here is my problem while make a new recyclerview of reviews it shows as below not take all width

我的活动

public class ReviewsActivity extends AppCompatActivity {

   private ArrayList<Review> reviews;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_reviews);

      reviews = new Gson().fromJson(getIntent().getStringExtra("reviews"), new
            TypeToken<List<Review>>() {
            }.getType());
    setUpReviewsList();
   }


   void setUpReviewsList() {

     RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rvReviews);
     ReviewsRvAdapter mAdapter = new ReviewsRvAdapter(this, reviews, true);
     recyclerView.setLayoutManager(new LinearLayoutManager(this));
     recyclerView.setAdapter(mAdapter);

     }

 } 

我的适配器

 public class ReviewsRvAdapter extends RecyclerView.Adapter<ReviewsRvHolder> 
 {

    private ArrayList<Review> reviews;
    private Context context;
    ReviewsRvHolder holder;
    private DatabaseReference mDatabase;
    boolean isActivity = false;
    // Provide a suitable constructor (depends on the kind of dataset)

    public ReviewsRvAdapter(Context context, ArrayList<Review> reviews) {
      this.reviews = reviews;
      this.context = context;
      mDatabase = FirebaseDatabase.getInstance().getReference();

    }

    public ReviewsRvAdapter(Context context, ArrayList<Review> reviews, boolean isActivity) {
      this.reviews = reviews;
      this.context = context;
      mDatabase = FirebaseDatabase.getInstance().getReference();
      this.isActivity = isActivity;

    }


  /**
   * Create new views (invoked by the layout manager)
   *
   * @param parent
   * @param viewType
   * @return
   */
  @Override
  public ReviewsRvHolder onCreateViewHolder(ViewGroup parent, int viewType) 
   {

     View layoutView = LayoutInflater.from(parent.getContext())
    .inflate(R.layout.list_item_user_review, null);
    //        View layoutView = LayoutInflater.from(parent.getContext())
    .inflate(R.layout.layout_review_summary, null);

    return new ReviewsRvHolder(layoutView);
    }

    /**
    * Replace the contents of a view (invoked by the layout manager)
    *
    * @param holder
    * @param position
    */
   @Override
   public void onBindViewHolder(ReviewsRvHolder holder, int position) {

      final int tempPos = position;
      this.holder = holder;
      Review review = reviews.get(position);
      try {

    holder.txtReviewDate.setReferenceTime(Long.parseLong(review.getDate()));
      } catch (Exception e) {
          e.printStackTrace();
      }
      if (review.getUser() != null) {

         holder.txtUserReviewNum.setText(String.format("%s%s",
                String.valueOf(review.getUser().getNumOfReviews()),
                context.getString(R.string.txt_reviews)));

        if (review.getUser().getUserName() != null)
            holder.txtReviewerName.setText(review.getUser().getUserName());
      }

      holder.txtLikesNum.setText(String.valueOf(review.getLikesNum()));
      holder.rbReviewerRate.setRating((float) review.getRating());
      holder.exTxtUserReview.setText(review.getReview());
      holder.imgBtnMenu.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //ReviewOtherPopUp(tempPos);
        }
      });
    } 

    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {
      if (isActivity) {
         return reviews.size();
      }
      //return the smallest number between 3 and list size
       return (3 > reviews.size() ? reviews.size() : 3);
    }
  }

这是activity_reviews.xml

  <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context="com.better.sftani.ReviewsActivity">

 <android.support.v7.widget.RecyclerView
    android:id="@+id/rvReviews"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

</RelativeLayout>

和list_item_user_review.xml

 <?xml version="1.0" encoding="utf-8"?>
 <android.support.v7.widget.CardView
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_margin="4dp"
 android:elevation="2dp">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="4dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="16dp">

        <ImageView
            android:id="@+id/imgProfile"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_gravity="center_vertical"
            android:background="@color/black"
            android:padding="8dp"/>

        <LinearLayout
            android:id="@+id/reviewer_linearlayout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/imgProfile"
            android:gravity="center_vertical"
            android:orientation="vertical"
            android:paddingLeft="16dp">

            <TextView
                android:id="@+id/txtReviewerName"
                android:layout_width="250dp"
                android:layout_height="wrap_content"
                android:text="Mohamed Abd El-Sattar"
                android:textColor="@color/black"/>

            <TextView
                android:id="@+id/txtUserReviewNum"
                android:layout_width="250dp"
                android:layout_height="wrap_content"
                android:text="@string/txt_reviews"/>
        </LinearLayout>

        <ImageButton
            android:id="@+id/imgBtnMenu"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toEndOf="@id/reviewer_linearlayout"
            android:layout_toRightOf="@id/reviewer_linearlayout"
            android:background="@null"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:src="@drawable/ic_dots"/>
    </RelativeLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingLeft="55dp"
        >

        <RatingBar
            android:id="@+id/rbReviewerRate"
            style="@style/Base.Widget.AppCompat.RatingBar.Small"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:isIndicator="true"
            android:stepSize="1"/>

        <com.github.curioustechizen.ago.RelativeTimeTextView
            android:id="@+id/txtReviewDate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"
            android:layout_marginStart="8dp"
            android:paddingBottom="4dp"
            android:paddingTop="4dp"
            android:text="-"

            app:relative_time_prefix="Completed "/>
    </LinearLayout>

    <com.better.sftani.Helpers.ExpandableTextView
        android:id="@+id/exTxtUserReview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        />

    <LinearLayout
        android:layout_width="121dp"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingLeft="48dp">

        <ImageButton
            android:id="@+id/imgBtnLikeReview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="?selectableItemBackground"
            android:gravity="center_vertical"
            android:src="@drawable/ic_like"
            />

        <TextView
            android:id="@+id/txtLikesNum"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="4dp"
            android:text="0"/>
    </LinearLayout>

</LinearLayout>

我做了很多尝试,以使所有布局都与父项匹配,并且仍然如上图所示,我搜索了我看到的问题 它指定了特定宽度

i tried a lot to make all of the layouts match parent and it still as it appears above and i search about the problem i saw this it gives it specific width

推荐答案

在您的ReviewsRvAdapter类中,在onCreateViewHolder()方法内部,您具有以下代码行:

In your ReviewsRvAdapter class, inside the onCreateViewHolder() method, you have this line of code:

View layoutView = LayoutInflater.from(parent.getContext())
        .inflate(R.layout.list_item_user_review, null);

现在,在放大视图时,您将null作为父级传递.这使得创建的视图无法匹配父级"宽度.

Right now, you're passing null as the parent when inflating the view. This makes it impossible for the created view to "match parent" for the width.

改为以下:

View layoutView = LayoutInflater.from(parent.getContext())
        .inflate(R.layout.list_item_user_review, parent, false);

现在,当您为视图充气时,您将有一个非null的父级,因此宽度将被正确设置.

Now you will have a non-null parent when you inflate the view, and so the width will be set correctly.

这篇关于Recyclerview项在与父项匹配时并不会占用屏幕的所有宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆