2016-03-04 3 views
0

Я пытаюсь добавить RecyclerView с GridLayoutManager в свое приложение. Все работает так, как ожидалось, только проблема заключается в том, что столбцы остаются оправданными, а не оправданными по центру. Есть идеи? Заранее спасибо!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> 
+0

Вы также можете показать свой файл макета – Inducesmile

+0

Хорошо, только что обновил OP. Надеюсь, это поможет –

ответ

2

Ok! Я понял, что это оказалось фактом, что мне нужно было установить ширину макета элемента как match_parent вместо wrap_content. Таким образом, это выглядит так:

<?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="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_horizontal" 
    android:layout_margin="10dp" 
    android:background="?android:selectableItemBackgroundBorderless" 
    android:clickable="true" 
    android:orientation="vertical"> 

    <ImageView 
     android:id="@+id/iconSpot" 
     android:layout_width="44dp" 
     android:layout_height="44dp" 
     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> 
1

Трудно установить только с файлом макета, поскольку макет элемента ne чтобы надуть и прикрепить к основному макету, чтобы увидеть результат.

Поэтому я решил воссоздать его. Я внес изменения в файл макета.

макет элемент

<?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

+0

Wow! Во-первых, я хочу поблагодарить за это! Надеюсь, мы сможем понять это, так что там есть некоторый интервал, но, к сожалению, элементы по-прежнему остаются оправданными (хотя и с левым полем), но он по-прежнему не работает, как ваш скриншот. –

+0

Хорошо, я решил. Проверьте мой ответ :) –

Смежные вопросы