RecylerView中的多重布局

时间:2015-12-30 07:10:28

标签: android

我正在尝试使用RecyclerView网格项目,以便在位置1处并排显示2个项目,在位置2处它应显示1个项目。
这是我的适配器类代码;它在2列上显示网格项,但我需要如下图所示 Image
MainFragment.class

public class MoviesFragment extends Fragment {

private MoviesAdapter mAdapter;
private RecyclerView mRecyclerView;

RecyclerView.LayoutManager mLayoutManager;

public MoviesFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View rootview= inflater.inflate(R.layout.fragment_movies, container, false);

    mRecyclerView = (RecyclerView)rootview.findViewById(R.id.list);
    mRecyclerView.setHasFixedSize(true);

    // The number of Columns
    mLayoutManager = new GridLayoutManager(getActivity(), 2);
    mRecyclerView.setLayoutManager(mLayoutManager);


    mAdapter = new MoviesAdapter(Arrays.asList(
            new CardViewData("hdh", "Direction pp", R.drawable.arya_movie),
            new CardViewData("jdjd", "Description of Item 1", R.drawable.julaie),
            new CardViewData("hdhd", "Description of Item 2", R.drawable.badrinadg),
            new CardViewData("hdhd", "Description of Item 3", R.drawable.happy),
            new CardViewData("hdhd", "Description of Item 4", R.drawable.julai),
            new CardViewData("hdhd", "Direddctor : sahs", R.drawable.vedam_movie)));

    mRecyclerView.setAdapter(mAdapter);
    return rootview;

}

}

这是我的适配器类:
MovieAdapter.class

public class MoviesAdapter extends RecyclerView.Adapter<MoviesAdapter.ViewHolder> {



private List<CardViewData> mDataset;

public MoviesAdapter(List<CardViewData> dataset) {
    mDataset = dataset;
}

@Override
public ViewHolder onCreateViewHolder(final ViewGroup viewGroup, int i) {
    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.moview_card_layout, viewGroup, false);
    return new ViewHolder(v);
}

@Override
public void onBindViewHolder(final ViewHolder viewHolder, int i) {
    final CardViewData cardViewData = mDataset.get(i);

    viewHolder.mTitle.setText(cardViewData.getTitle());
    viewHolder.mDescription.setText(cardViewData.getDescription());
    viewHolder.mImage.setImageResource(cardViewData.getImage());

    viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(v.getContext(), "Title: " + cardViewData.getTitle(), Toast.LENGTH_SHORT).show();
        }
    });
}

@Override
public int getItemCount() {
    return mDataset == null ? 0 : mDataset.size();

}

public static class ViewHolder extends RecyclerView.ViewHolder {

    public TextView mTitle;
    public TextView mDescription;
    public ImageView mImage;

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

        mTitle = (TextView) itemView.findViewById(R.id.row_title);
        mDescription = (TextView) itemView.findViewById(R.id.row_description);
        mImage = (ImageView) itemView.findViewById(R.id.row_image);
    }
}

}

3 个答案:

答案 0 :(得分:0)

@Override
public int getViewType(int position) {
    if (position == 0) {
        return TYPE_1;
    }else if(position == 1){
        return TYPE_2; 
    }
}

@Override
public RecyclerView.ViewHolder getView(ViewGroup parent, int viewType) {
    if (viewType == TYPE_1) {
        View view = inflater.inflate(R.layout.type_one_layout, parent, false);
        return new Type1Holder(view);
    } else {
        View view = inflater.inflate(R.layout.type_two_layout, parent, false);
        return new Type2Holder(view);
    }
 }

@Override
public void onBindView(RecyclerView.ViewHolder viewHolder, int i) {
    if (viewHolder instanceof Type1Holder) {
        /// Insert the data into the UI of Type1
    }else if(viewHolder instanceof Type1Holder){
        /// Insert the data into the UI of Type2
    }

答案 1 :(得分:0)

 static final int TYPE_HEADER = 0;
 static final int TYPE_CELL = 1;

 @Override
public int getViewType(int position) {
if (position == 0) {
    return TYPE_HEADER;
}else if(position == 1){
    return TYPE_CELL; 
}

 @Override
public RecyclerView.ViewHolder getView(ViewGroup parent, int viewType) {
if (viewType == TYPE_HEADER) {
    View view = inflater.inflate(R.layout.header_layout, parent, false);
    return new Type1Holder(view);
} else {
    View view = inflater.inflate(R.layout.cell_layout, parent, false);
    return new Type2Holder(view);
}
@Override
public void onBindView(RecyclerView.ViewHolder viewHolder, int i) {
if (viewHolder instanceof Type1Holder) {
    /// Insert the data into the UI of Type1
}else if(viewHolder instanceof Type1Holder){
    /// Insert the data into the UI of Type2
}

答案 2 :(得分:0)

你需要覆盖这个方法并返回视图类型取决于项目位置,在你的情况下,它应该是这样的:

@Override
public int getItemViewType(int position) {
    switch (position) {
        case 0:
            return layout_1_with_int_value;
        case 1:
            return layout_2_with_int_value;
        default:
            return default_layout_with_int_value;
    }
}

您需要为每种类型的布局创建不同的ViewHolder子类,然后在方法@Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {}中创建 你ininflater不同的布局取决于viewType。但是在完成所有操作之前还有一个步骤,在方法public void onBindViewHolder(final RecyclerView.ViewHolder viewHolder, int position) {}中,您调用int viewType = getItemViewType(position);来获取当前位置的视图类型并创建正确的ViewHolder。

完成!

希望它会有所帮助:)