RecyclerView水平wrap_content最大尺寸

时间:2016-10-24 15:19:18

标签: android android-recyclerview

我有一个RecyclerView(下面的图片),我从API加载了一些内容(图片和一些文本)。我希望所有行的高度完全相同,但文本的长度可能会有很大差异。因此黄色框应该更大,以匹配具有更多文本的绿色框。如果这有意义吗?

picture A

目前,我使用的LinearLayoutManager设置为Horizo​​ntal,但我也使用GridLayoutManager测试了1行产生相同的效果。

我也使用setAutoMeasureEnabled为true。

  1. 使用RecyclerView有一种简单的方法吗?

  2. 如果没有,最好如何计算高度并将其发送到每个ViewHolder?

  3. 谢谢,

    根据要求:

    我在MainActivity onCreate中的初始化:

    mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        mRecyclerView.setHasFixedSize(true);
    
        // use a linear layout manager
        mLayoutManager = new GridLayoutManager(MainActivity.this, 1, GridLayoutManager.HORIZONTAL, false);
        mLayoutManager.setAutoMeasureEnabled(true);
    
        mRecyclerView.setLayoutManager(mLayoutManager);
    
        setUpSomeData();
    
        TopFilmsCard card1 = (TopFilmsCard)cards.get(0);
        mAdapter = new FilmsAdapter(card1.getFilms());
        mRecyclerView.setAdapter(mAdapter);
    

    FilmsAdapter

     private class FilmsAdapter
            extends RecyclerView.Adapter<FilmsCardHolder> {
    
        private List<Film> films;
    
        public FilmsAdapter(List<Film> films) {
            this.films = films;
        }
    
        @Override
        public int getItemViewType(int position) {
            return R.layout.holder_top_films;
        }
    
        @Override
        public FilmsCardHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    
            return new FilmsCardHolder(LayoutInflater.from(MainActivity.this).inflate(R.layout.holder_film, parent, false));
        }
    
        @Override
        public void onBindViewHolder(FilmsCardHolder holder, int position) {
            holder.bindCard(films.get(position));
        }
    
        @Override
        public int getItemCount() {
            return films.size();
        }
    }
    

    FilmsCardHolder

    公共类FilmsCardHolder扩展了RecyclerView.ViewHolder {

    public FilmsCardHolder(View itemView) {
        super(itemView);
    }
    
    
    public void bindCard(Film film) {
    
        ViewGroup layout = (ViewGroup)itemView.findViewById(R.id.layoutFilm);
        TextView lblFilm = (TextView)itemView.findViewById(R.id.lblFilmName);
        ImageView imgPicture = (ImageView)itemView.findViewById(R.id.imgPicture);
    
        layout.setBackgroundResource(film.getColor());
        lblFilm.setText(film.getName());
        imgPicture.setImageResource(film.getPicture());
    
    
    }
    

    }

    activity_main.xml中

     <RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/activity_main"
      android:layout_width="match_parent"
      android:layout_height="match_parent">
    
      <android.support.v7.widget.RecyclerView
          android:id="@+id/recyclerView"
          android:scrollbars="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:background="#CCD1D9"/>
    

    holder_film

      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/layoutFilm"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="vertical"
      android:background="#5D9CEC">
    
      <TextView
          android:id="@+id/lblFilmName"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:gravity="center_horizontal"
          android:padding="10dp"
          android:textSize="22sp"/>
    
      <ImageView
          android:id="@+id/imgPicture"
          android:layout_width="100dp"
          android:layout_height="100dp"
          android:layout_gravity="center_horizontal"
          />
    

0 个答案:

没有答案