私は自分のアイテムビューを最小限に抑えましたが、まだたくさんのパディングがあるレイアウトがあり、それは私が期待したものではありません。RecyclerView:奇妙なレイアウト
写真を参照してください:レンダラーに
一つのセル:
実際のビュー:誰かが私はこれらの空白を削除する方法
を説明することができます画像の上部と下部にありますか?
一つのセル
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/categoryPhoto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/bg_alles_fur_kinder" />
<TextView
android:id="@+id/categoryName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Children"
android:textColor="#fff"
android:textAllCaps="true"
android:layout_alignLeft="@id/categoryPhoto"
android:layout_alignBottom="@id/categoryPhoto"
android:textSize="22sp" />
</RelativeLayout>
カスタムRecyclerView
/**
* Created by laurentmeyer on 21/08/16.
*/
public class CategoryRecyclerView extends RecyclerView {
public CategoryRecyclerView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
setHasFixedSize(true);
ArrayList<Category> categories = new ArrayList<>();
categories.add(new Category(Category.CategoryType.CHILDREN));
categories.add(new Category(Category.CategoryType.DECORATION));
categories.add(new Category(Category.CategoryType.FASHION));
categories.add(new Category(Category.CategoryType.FURNITURE));
categories.add(new Category(Category.CategoryType.HOUSE));
categories.add(new Category(Category.CategoryType.LITTERATURE));
categories.add(new Category(Category.CategoryType.MULTIMEDIA));
categories.add(new Category(Category.CategoryType.OTHER));
CategoryRecyclerViewAdapter adapter = new CategoryRecyclerViewAdapter(categories);
setAdapter(adapter);
GridLayoutManager glm = new GridLayoutManager(getContext(), 2);
glm.setAutoMeasureEnabled(true);
setLayoutManager(glm);
}
public class CategoryRecyclerViewAdapter extends RecyclerView.Adapter<CategoryRecyclerViewAdapter.CategoryViewHolder> {
List<Category> categories;
CategoryRecyclerViewAdapter(List<Category> categories) {
this.categories = categories;
}
@Override
public CategoryViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.category_card, parent, false);
CategoryViewHolder cvh = new CategoryViewHolder(v);
return cvh;
}
@Override
public void onBindViewHolder(CategoryViewHolder holder, int position) {
holder.categoryName.setText(categories.get(position).getType().getName());
holder.photo.setImageResource(categories.get(position).getType().imageId);
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
@Override
public int getItemCount() {
return categories.size();
}
public class CategoryViewHolder extends RecyclerView.ViewHolder {
TextView categoryName;
ImageView photo;
public CategoryViewHolder(View itemView) {
super(itemView);
categoryName = (TextView) itemView.findViewById(R.id.categoryName);
photo = (ImageView) itemView.findViewById(R.id.categoryPhoto);
}
}
}
}
EDIT:完璧LinearLayoutManager
作品を使用してここで
は、コード(非常に簡単)です。
あなたが本当に巣へのLinearLayoutでRelativeLayoutが必要ですか?ネストされたレイアウトはパフォーマンスに悪い –
ええ、私はそれを取り除くことができます、前にcardviewがあったが、私はデバッグのためにそれを削除しました。 Thx –
変更されました、thx –