使用GridLayoutManager的RecyclerView左对齐视图

时间:2016-03-04 02:02:06

标签: java android

我正在尝试将RecyclerView GridLayoutManager添加到我的应用中。一切都按预期工作,唯一的问题是列似乎是左对齐而不是中心对齐。有任何想法吗?提前谢谢!

以下是展示其外观的图片:

sdfs

以下是单项布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:background="?android:selectableItemBackgroundBorderless"
    android:clickable="true"
    android:gravity="center_horizontal"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/iconSpot"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:layout_gravity="center_horizontal"
        android:src="@drawable/ic_launcher"
        tools:ignore="MissingPrefix" />

    <TextView
        android:id="@+id/textLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Dummy Text"
        android:textSize="16sp" />

</LinearLayout>

这是完整的活动布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/totalScreen"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignTop="@+id/fab"
    android:layout_gravity="bottom|center"
    android:gravity="bottom|center_vertical">

    <TextView
        android:id="@+id/sheetTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#424242"
        android:padding="12dp"
        android:text="Dummy Title"
        android:textSize="18sp" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignTop="@+id/sheetTitle"
        android:layout_marginEnd="20dp"
        android:layout_marginTop="-28dp"
        android:visibility="gone"
        app:fabSize="normal" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/gridScreen"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/sheetTitle"
        android:background="#424242"
        android:gravity="bottom|center"
        android:layout_gravity = "center"
        android:orientation="vertical"
        android:paddingBottom="24dp" />

</RelativeLayout>

2 个答案:

答案 0 :(得分:3)

确定!我想通了,最后我需要将项目布局宽度设置为match_parent而不是wrap_content。所以这看起来像这样:

Content-Encoding: gzip

答案 1 :(得分:1)

仅使用布局文件很难修复,因为项目布局需要膨胀并附加到主布局才能看到结果。

所以我决定重新创建它。我修改了你的布局文件。

项目布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@color/colorPrimary"
android:layout_margin="2dp"
android:clickable="true"
android:orientation="vertical">

<ImageView
    android:id="@+id/iconSpot"
    android:layout_width="48dp"
    android:layout_height="48dp"
    android:layout_gravity="center_horizontal"
    android:src="@android:drawable/ic_delete"
    tools:ignore="MissingPrefix" />

<TextView
    android:id="@+id/textLabel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:text="Dummy Text"
    android:textSize="16sp" />

</LinearLayout>

主要活动布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/totalScreen"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignTop="@+id/fab"
android:layout_gravity="bottom|center"
android:gravity="bottom|center_vertical">

<TextView
    android:id="@+id/sheetTitle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#424242"
    android:padding="12dp"
    android:text="Dummy Title"
    android:textSize="18sp" />

<android.support.v7.widget.RecyclerView
    android:id="@+id/gridScreen"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/sheetTitle"
    android:background="#424242"
    android:gravity="bottom|center"
    android:layout_gravity = "center"
    android:clipToPadding="false"
    android:orientation="vertical"
    android:paddingBottom="24dp" />

</RelativeLayout>

主要活动类。用于项目边距的此类(GridSpacingItemDecoration)是从here

复制的
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

private GridLayoutManager layout;

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

    layout = new GridLayoutManager(MainActivity.this, 2);

    RecyclerView rView = (RecyclerView)findViewById(R.id.gridScreen);
    rView.setHasFixedSize(true);
    rView.setLayoutManager(layout);

    int spanCount = 2;
    int spacing = 50;
    boolean includeEdge = true;
    rView.addItemDecoration(new GridSpacingItemDecoration(spanCount, spacing, includeEdge));

    RecyclerViewAdapter rcAdapter = new RecyclerViewAdapter(MainActivity.this, getAllItemList());
    rView.setAdapter(rcAdapter);
}

private List<ItemObject> getAllItemList(){

    List<ItemObject> allItems = new ArrayList<ItemObject>();
    allItems.add(new ItemObject(android.R.drawable.ic_delete, "United"));
    allItems.add(new ItemObject(android.R.drawable.ic_delete, "United"));
    allItems.add(new ItemObject(android.R.drawable.ic_delete, "United"));
    allItems.add(new ItemObject(android.R.drawable.ic_delete, "United"));
    allItems.add(new ItemObject(android.R.drawable.ic_delete, "United"));
    allItems.add(new ItemObject(android.R.drawable.ic_delete, "United"));
    return allItems;
}

}

RecycleView适配器类

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewHolders> {

private List<ItemObject> itemList;
private Context context;

public RecyclerViewAdapter(Context context, List<ItemObject> itemList) {
    this.itemList = itemList;
    this.context = context;
}

@Override
public RecyclerViewHolders onCreateViewHolder(ViewGroup parent, int viewType) {

    View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list, null);
    RecyclerViewHolders rcv = new RecyclerViewHolders(layoutView);
    return rcv;
}

@Override
public void onBindViewHolder(RecyclerViewHolders holder, int position) {
    holder.displayedImage.setImageResource(itemList.get(position).getImage());
    holder.textTitle.setText(itemList.get(position).getTitle());

}

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

ViewHolder类

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;


public class RecyclerViewHolders extends RecyclerView.ViewHolder implements View.OnClickListener{

public ImageView displayedImage;
public TextView textTitle;

public RecyclerViewHolders(View itemView) {
    super(itemView);
    itemView.setOnClickListener(this);
    displayedImage = (ImageView)itemView.findViewById(R.id.iconSpot);
    textTitle = (TextView)itemView.findViewById(R.id.textLabel);
}

@Override
public void onClick(View view) {

}

}

ObjectEntity类

public class ItemObject {

private int image;
private String title;

public ItemObject(int image, String title) {
    this.image = image;
    this.title = title;
}

public int getImage() {
    return image;
}

public void setImage(int image) {
    this.image = image;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}
}

结果如下。试试看它是否适合你。

enter image description here