如何控制recyclerview对象的宽度

时间:2017-05-29 20:08:48

标签: android android-layout android-recyclerview

所以我正在建设和应用程序的学校冲浪系列和电影和我使用recyclerview,但每张卡完全与屏幕匹配,然后左右滚动: enter image description here

我在互联网上搜索,不知道如何让它像以下一样工作:enter image description here

我的recycler_view.xml如下:



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:maxWidth="250dp">

    <ImageView
        android:id="@+id/item_image"
        android:layout_width="250dp"
        android:layout_height="200dp"
        android:layout_gravity="center"
        android:layout_marginTop="8dp"
        android:layout_weight="3"
        android:adjustViewBounds="false"
        android:maxHeight="100dp"
        android:maxWidth="250dp"
        android:minHeight="100dp"
        android:minWidth="250dp" />
    <TextView
        android:id="@+id/item_date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_gravity="top|start"
        android:layout_weight="1"
        android:text="Some date"/>
    <TextView
        android:id="@+id/item_description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center|start"
        android:layout_weight="1"
        android:ellipsize="end"
        android:maxLines="5"/>
</LinearLayout>
&#13;
&#13;
&#13;

和我的主要布局:

&#13;
&#13;
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.juanfri.pruebareclyclerview.MainActivity">


    <TextView
        android:id="@+id/textViewPeliculas"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Peliculas"
        app:layout_constraintStart_toStartOf="parent"
        android:layout_marginStart="16dp"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="32dp"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginEnd="8dp"
        app:layout_constraintHorizontal_bias="0.064" />

    <TextView
        android:id="@+id/textViewSeries"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="32dp"
        android:text="Series"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/recyclerViewPeliculas" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerViewPeliculas"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:scrollbars="horizontal"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textViewPeliculas"
        />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerViewSeries"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:scrollbars="horizontal"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textViewSeries" />


</android.support.constraint.ConstraintLayout>
&#13;
&#13;
&#13;

这个我的回收器适配器我可能需要在这里更改它但不知道:

&#13;
&#13;
package com.example.juanfri.pruebareclyclerview;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;

/**
 * Created by Juanfri on 28/05/2017.
 */

public class RecyclerAdapterPelicula extends RecyclerView.Adapter<RecyclerAdapterPelicula.PeliHolder> {
    private ArrayList<Pelicula> mPeliculas;
    @Override
    public RecyclerAdapterPelicula.PeliHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View inflatedView = LayoutInflater.from(viewGroup.getContext())
                .inflate(R.layout.recyclerview_card, viewGroup, false);
        return new PeliHolder(inflatedView);
    }

    @Override
    public void onBindViewHolder(RecyclerAdapterPelicula.PeliHolder holder, int i) {
        Pelicula itemPhoto = mPeliculas.get(i);
        holder.bindPhoto(itemPhoto);
    }

    @Override
    public int getItemCount() {
        return mPeliculas.size();
    }

    public RecyclerAdapterPelicula(ArrayList<Pelicula> Peliculas) {
        mPeliculas = Peliculas;
    }
    public static class PeliHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        //2
        private ImageView mItemImage;
        private TextView mItemDate;
        private TextView mItemDescription;
        private Pelicula peli;

        //3
        private static final String PHOTO_KEY = "PHOTO";

        //4
        public PeliHolder(View v) {
            super(v);

            mItemImage = (ImageView) v.findViewById(R.id.item_image);
            mItemDate = (TextView) v.findViewById(R.id.item_date);
            mItemDescription = (TextView) v.findViewById(R.id.item_description);
            v.setOnClickListener(this);
        }

        //5
        @Override
        public void onClick(View v) {
            /*Context context = itemView.getContext();
            Intent showPhotoIntent = new Intent(context, Pelicula.class);
            showPhotoIntent.putExtra(PHOTO_KEY, peli);
            context.startActivity(showPhotoIntent);*/
        }
        public void bindPhoto(Pelicula mpeli) {
            peli = mpeli;
            Picasso.with(mItemImage.getContext()).load(mpeli.getPoster()).into(mItemImage);
            mItemDate.setText(peli.getNombrePelicula());
            mItemDescription.setText(Integer.toString(peli.getDuracion()));
        }
    }
}
&#13;
&#13;
&#13;

我真的不需要一个解决方案,只需要知道如何前进-..-&#34;

谢谢你的帮助!

3 个答案:

答案 0 :(得分:1)

你必须为textView设置imageView的重力,我会为你的布局建议这种行为:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:maxWidth="250dp">

    <ImageView
        android:id="@+id/item_image"
        android:layout_width="250dp"
        android:layout_height="200dp"
        android:layout_gravity="center"
        android:layout_marginTop="8dp"
        android:layout_weight="3"
        android:adjustViewBounds="false"
        android:maxHeight="100dp"
        android:maxWidth="250dp"
        android:minHeight="100dp"
        android:minWidth="250dp" />
    <TextView
        android:id="@+id/item_date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:text="Some date"
        android:layout_below="@id/item_image"/>
    <TextView
        android:id="@+id/item_description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:ellipsize="end"
        android:maxLines="5"
        android:layout_below="@id/item_date"/>
</RelativeLayout>

答案 1 :(得分:1)

1。将根LinearLayout宽度用作wrap_content250dp

2。layout_weightImageView移除属性TextView's

3。使用属性android:layout_width="match_parent"android:gravity="center"TextView保持text位置center

4. ImageView删除不必要的max-min高度/宽度。

以下是工作代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:maxWidth="250dp">

    <ImageView
        android:id="@+id/item_image"
        android:layout_width="250dp"
        android:layout_height="200dp"
        android:layout_gravity="center"
        android:layout_marginTop="8dp"
        android:adjustViewBounds="false"
        android:scaleType="centerCrop"
        android:src="@drawable/dummy"/>

    <TextView
        android:id="@+id/item_date"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:gravity="center"
        android:ellipsize="end"
        android:maxLines="1"
        android:textSize="16dp"
        android:textColor="#000000"
        android:text="Big Bang Theory"/>

    <TextView
        android:id="@+id/item_description"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:ellipsize="end"
        android:maxLines="5"
        android:textSize="14dp"
        android:textColor="#727272"
        android:text="2154"/>
</LinearLayout>

<强>输出:

enter image description here

希望这会有所帮助〜

答案 2 :(得分:0)

试试此代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:maxWidth="250dp">

    <ImageView
        android:id="@+id/item_image"
        android:layout_width="250dp"
        android:layout_height="200dp"
        android:layout_gravity="center"
        android:layout_marginTop="8dp"
        android:layout_weight="3"
        android:adjustViewBounds="false"
        android:maxHeight="100dp"
        android:maxWidth="250dp"
        android:minHeight="100dp"
        android:minWidth="250dp" />
    <TextView
        android:id="@+id/item_date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_gravity="top|start"
        android:layout_weight="1"
        android:text="Some date"/>
    <TextView
        android:id="@+id/item_description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center|start"
        android:layout_weight="1"
        android:ellipsize="end"
        android:maxLines="5"/>
</LinearLayout>