Настройка горизонтального RecyclerView - PullRequest
0 голосов
/ 16 ноября 2018

Я хочу добавить отступ 10 дп для моей первой плитки в представлении переработчика.

Так же, как в приложении «Моя книга», первое приложение имеет отступ в 10 дп (предположим), а затем между двумя тайлами (предположим, 5 дп).

enter image description here

Аналогично, последний тайл также имеет отступ 10dp (предположим) в конце

enter image description here

Я добавляю свой код представления переработчика. Если кто-то хочет что-то добавить в него RecyclerMetroAdapter.java

package com.example.android.indianmetro;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;

public class RecyclerViewAdapter extends 
RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {

private static final String TAG = "RecyclerViewAdapter";

private ArrayList<String> mPlaceNames = new ArrayList<>();
private ArrayList<String> mPlaceImageUrl = new ArrayList<>();
private ArrayList<String> mPlaceMetroDistance = new ArrayList<>();
private ArrayList<String> mClosestMetro = new ArrayList<>();
private Context mContext;

public RecyclerViewAdapter(Context context, ArrayList<String> placeNames, ArrayList<String> placeImageUrl,
                           ArrayList<String> placeMetroDistance, ArrayList<String> closestMetro    ) {
    mPlaceNames = placeNames;
    mPlaceImageUrl = placeImageUrl;
    mPlaceMetroDistance = placeMetroDistance;
    mClosestMetro = closestMetro;
    mContext = context;
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

    View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.horizontal_item, viewGroup, false);
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
    Log.d(TAG, "onCreateViewHolder: called");

    Glide.with(mContext)
            .asBitmap()
            .load(mPlaceImageUrl.get(i))
            .into(viewHolder.placeImage);

    viewHolder.placeName.setText(mPlaceNames.get(i));
    viewHolder.placeMetroDistance.setText(mPlaceMetroDistance.get(i));
    viewHolder.closestMetro.setText(mClosestMetro.get(i));

}

@Override
public int getItemCount() {
    if (mPlaceNames.size() < 8){
        return mPlaceNames.size();
    } else return 8;
}

public class ViewHolder extends RecyclerView.ViewHolder{

    ImageView placeImage;
    TextView placeName;
    TextView placeMetroDistance;
    TextView closestMetro;

    public ViewHolder(@NonNull View itemView) {
        super(itemView);

        placeImage = (ImageView) itemView.findViewById(R.id.image);
        placeName = itemView.findViewById(R.id.textView);
        placeMetroDistance = itemView.findViewById(R.id.textView2);
        closestMetro = itemView.findViewById(R.id.placeMetroName);
    }
}
}

Код плиток, которые я использую внутри моего RecyclerView

horizontal_item.xml

<?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="wrap_content"
android:layout_height="wrap_content">

<android.support.v7.widget.CardView
    android:id="@+id/cardView2"
    android:layout_width="120dp"
    android:layout_height="140dp"
    android:layout_marginStart="10dp"
    android:layout_marginTop="8dp"
    app:cardCornerRadius="10dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <ImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        android:src="@drawable/delhi1" />

</android.support.v7.widget.CardView>


<TextView
    android:id="@+id/textView"
    android:layout_width="110dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="10dp"
    android:layout_marginTop="2dp"
    android:ellipsize="end"
    android:maxLines="1"
    android:text="India Gate"
    android:textColor="@color/darkHeading"
    android:textSize="15sp"
    android:textStyle="bold"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/cardView2" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="10dp"
    android:layout_marginTop="36dp"
    android:text="2.4 Km Away"
    android:textColor="@color/blueAccent"
    android:textSize="10sp"
    android:textStyle="bold"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/cardView2" />

<ImageView
    android:id="@+id/imageView2"
    android:layout_width="10dp"
    android:layout_height="10dp"
    android:layout_marginStart="10dp"
    android:layout_marginTop="24dp"
    android:src="@drawable/ic_metro"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/cardView2" />

<TextView
    android:id="@+id/placeMetroName"
    android:layout_width="90dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="25dp"
    android:layout_marginStart="25dp"
    android:layout_marginTop="20dp"
    android:ellipsize="end"
    android:maxLines="1"
    android:text="Central Secretariat"
    android:textColor="@color/subHeading"
    android:textSize="12sp"
    android:textStyle="bold"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/cardView2" />

</android.support.constraint.ConstraintLayout>

Также, если кто-то может подсказать, как сделать плитку полностью видимой слева и не допустить, чтобы какая-либо плитка была частично скрыта с левой стороны

enter image description here

1 Ответ

0 голосов
/ 18 ноября 2018

Полагаю, вы можете попытаться установить отступ в onBindViewHolder(),
для первого:

if (i == 0) 
    viewHolder.placeImage.setPadding(10, 5, 5, 5);

и за последнее:

if (i == mPlaceImageUrl.size() - 1) 
    viewHolder.placeImage.setPadding(5, 5, 10, 5);

Измените числа на те, которые вам нравятся. left-> top-> right-> bottom padding.

...