交错的Gridview布局inflater重叠片段

时间:2016-01-24 02:10:28

标签: android android-layout android-fragments gridview layout-inflater

我正试图通过此tutorial实现交错的gridview。我在片段内膨胀交错的gridview时遇到了麻烦。交错的网格视图与我的应用程序中的所有片段重叠。

Screenshot of overlap

我认为问题可能在于此处

  @Override
public MasonryView onCreateViewHolder(ViewGroup parent, int viewType) {
    View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_gallery, parent, false);
    MasonryView masonryView = new MasonryView(layoutView);
    return masonryView;
}

适配器

public class MasonryAdapter extends RecyclerView.Adapter<MasonryAdapter.MasonryView> {

private Context context;

int[] imgList = {R.drawable.two, R.drawable.one, R.drawable.three, R.drawable.four,
        R.drawable.five, R.drawable.six, R.drawable.seven, R.drawable.eight,
        R.drawable.nine, R.drawable.ten};

String[] nameList = {"One", "Two", "Three", "Four", "Five", "Six",
        "Seven", "Eight", "Nine", "Ten"};

public MasonryAdapter(Context context) {
    this.context = context;
}

@Override
public MasonryView onCreateViewHolder(ViewGroup parent, int viewType) {
    View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.grid_item, parent, false);
    MasonryView masonryView = new MasonryView(layoutView);
    return masonryView;
}

@Override
public void onBindViewHolder(MasonryView holder, int position) {
    holder.imageView.setImageResource(imgList[position]);
    holder.textView.setText(nameList[position]);
}

@Override
public int getItemCount() {
    return nameList.length;
}

class MasonryView extends RecyclerView.ViewHolder {
    ImageView imageView;
    TextView textView;

    public MasonryView(View itemView) {
        super(itemView);

        imageView = (ImageView) itemView.findViewById(R.id.img);
        textView = (TextView) itemView.findViewById(R.id.img_name);

    }
}

MainActivity

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

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    mRecyclerView = (RecyclerView) findViewById(R.id.masonry_grid);
    mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));

    MasonryAdapter adapter = new MasonryAdapter(this);
    mRecyclerView.setAdapter(adapter);
    SpacesItemDecoration decoration = new SpacesItemDecoration(16);
    mRecyclerView.addItemDecoration(decoration);

0 个答案:

没有答案