recyclerviews中的项目重叠:
这是recyclerview声明:
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
arrayList = new ArrayList<>();
arrayList.add(new DataModel2x2("Item 1", R.drawable.battle, "#09A9FF"));
arrayList.add(new DataModel4x4("Item 2", R.drawable.beer, "#3E51B1"));
arrayList.add(new DataModel4x2("Item 3", R.drawable.ferrari, "#673BB7"));
arrayList.add(new DataModel4x2("Item 4", R.drawable.jetpack_joyride,
"#4BAA50"));
arrayList.add(new DataModel4x4("Item 5", R.drawable.three_d, "#F94336"));
arrayList.add(new DataModel2x4("Item 6", R.drawable.battle, "#0A9B88"));
arrayList.add(new DataModel2x2("Item 7", R.drawable.beer, "#0A9B88"));
arrayList.add(new DataModel2x2("Item 8", R.drawable.ferrari, "#0A9B88"));
arrayList.add(new DataModel2x2("Item 9", R.drawable.jetpack_joyride,
"#0A9B88"));
arrayList.add(new DataModel2x2("Item 10", R.drawable.three_d, "#0A9B88"));
arrayList.add(new DataModel2x2("Item 12", R.drawable.battle, "#0A9B88"));
arrayList.add(new DataModel2x2("Item 13", R.drawable.beer, "#0A9B88"));
arrayList.add(new DataModel2x2("Item 14", R.drawable.ferrari, "#0A9B88"));
arrayList.add(new DataModel2x2("Item 15", R.drawable.jetpack_joyride,
"#0A9B88"));
arrayList.add(new DataModel2x2("Item 16", R.drawable.three_d, "#0A9B88"));
arrayList.add(new DataModel2x2("Item 17", R.drawable.battle, "#0A9B88"));
arrayList.add(new DataModel2x2("Item 18", R.drawable.ferrari, "#0A9B88"));
RecyclerViewAdapter adapter = new RecyclerViewAdapter(this, arrayList,
this);
recyclerView.setAdapter(adapter);
StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager( 2,
GridLayoutManager.HORIZONTAL);
recyclerView.setLayoutManager(manager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.addItemDecoration(new ItemOffsetDecoration(20));
这是适配器:
package com.journaldev.recyclerviewgridlayoutmanager;
import android.content.Context;
import android.graphics.Color;
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.RelativeLayout;
import android.widget.TextView;
import java.util.ArrayList;
public class RecyclerViewAdapter extends
RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
ArrayList<Tileable> mValues;
Context mContext;
protected ItemListener mListener;
public RecyclerViewAdapter(Context context, ArrayList<Tileable> values,
ItemListener itemListener) {
mValues = values;
mContext = context;
mListener = itemListener;
}
@Override
public int getItemViewType(int position) {
if (Tileable.TILE2x2 == mValues.get(position).getTileType()) {
return Tileable.TILE2x2;
} else if (Tileable.TILE2x4 == mValues.get(position).getTileType()) {
return Tileable.TILE2x4;
} else if (Tileable.TILE4x2 == mValues.get(position).getTileType()) {
return Tileable.TILE4x2;
}
return Tileable.TILE4x4;
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView textView;
public TextView textViewDescription;
public ImageView imageView;
public RelativeLayout relativeLayout;
Tileable item;
public ViewHolder(View v) {
super(v);
v.setOnClickListener(this);
textView = (TextView) v.findViewById(R.id.textView);
imageView = (ImageView) v.findViewById(R.id.imageView);
relativeLayout = (RelativeLayout) v.findViewById(R.id.relativeLayout);
textViewDescription = (TextView) v.findViewById(R.id.textViewDescription);
}
public void setData(Tileable item) {
this.item = item;
textView.setText(item.getText());
imageView.setImageResource(item.getDrawable());
relativeLayout.setBackgroundColor(Color.parseColor(item.getColor()));
switch (item.getTileType()) {
case Tileable.TILE2x2 :{
textViewDescription.setText("2x2 TILE");
break;
}
case Tileable.TILE2x4 :{
textViewDescription.setText("2x4 TILE");
break;
}
case Tileable.TILE4x2 :{
textViewDescription.setText("4x2 TILE");
break;
}
case Tileable.TILE4x4 :{
textViewDescription.setText("4x4 TILE");
break;
}
}
}
@Override
public void onClick(View view) {
if (mListener != null) {
mListener.onItemClick(item);
}
}
}
@Override
public RecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = null;
if (viewType == Tileable.TILE2x2) {
view = LayoutInflater.from(mContext).inflate(R.layout.recycler_view_item, parent, false);
} else if (viewType == Tileable.TILE2x4) {
view = LayoutInflater.from(mContext).inflate(R.layout.recycler_view_item_2x4, parent, false);
} else if (viewType == Tileable.TILE4x2) {
view = LayoutInflater.from(mContext).inflate(R.layout.recycler_view_item_4x2, parent, false);
} else if (viewType == Tileable.TILE4x4) {
view = LayoutInflater.from(mContext).inflate(R.layout.recycler_view_item_4x4, parent, false);
}
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder Vholder, int position) {
Vholder.setData(mValues.get(position));
}
@Override
public int getItemCount() {
return mValues.size();
}
public interface ItemListener {
void onItemClick(Tileable item);
}
}
当我仅添加2x2和2x4磁贴时,布局很好。但是当我添加4x4或4x2项时,交错网格布局中的recyclerView项变得重叠了