如何使一个视图比RecyclerView中的其他视图更大?

时间:2017-08-20 18:26:24

标签: java android android-layout android-recyclerview recycler-adapter

我在json API中有很少的图像,我设法使用volley Library获取这些图像。我使用了带有图像适配器的recyclerview来在两列中垂直显示图像视图,但我希望将一个图像设置为大图像并将其显示为用户可以单击的第一个图像。此图像也将在每个时间间隔内更改。基本上,API将执行所有后端任务,例如设置时间并告知在Recyclerview顶部显示哪个图像,如果不是所有图像必须具有相同的大小并且垂直显示在2列中。

我有recyclerview的方形布局类。我只想知道如何做到这一点。即使这个概念也没问题。

example

1 个答案:

答案 0 :(得分:0)

您必须为标题项的其他小项创建两个视图,并且onBindViewHolder必须相应地绑定这些视图,以下代码是新闻文章应用程序的示例,应该有所帮助。

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

    private List<NewsArticles> items = new ArrayList<>();

    private Context ctx;
    private NewsArticles header;
    private static final int TYPE_HEADER = 0;
    private static final int TYPE_ITEM = 1;
    private OnItemClickListener mOnItemClickListener;

    public interface OnItemClickListener {
    void onItemClick(View view, NewsArticles obj, int position);
    }

    public void setOnItemClickListener(final OnItemClickListener mItemClickListener) {
    this.mOnItemClickListener = mItemClickListener;
    }

    // Provide a suitable constructor (depends on the kind of dataset)
    public AdapterNewsArticlesListWithHeader(Context context, NewsArticles header, List<NewsArticles> items) {
    this.items = items;
    this.header = header;
    ctx = context;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
    // each data item is just a string in this case
    public TextView title;
    public TextView short_content;
    public TextView date;
    public ImageView image;
    public LinearLayout lyt_parent;

    public ViewHolder(View v) {
        super(v);
        title = (TextView) v.findViewById(R.id.title);
        short_content = (TextView) v.findViewById(R.id.short_content);
        date = (TextView) v.findViewById(R.id.date);
        image = (ImageView) v.findViewById(R.id.image);
        lyt_parent = (LinearLayout) v.findViewById(R.id.lyt_parent);
    }
    }

    class ViewHolderHeader extends RecyclerView.ViewHolder {
    public TextView title;
    public TextView date;
    public ImageView image;
    public LinearLayout lyt_parent;

    public ViewHolderHeader(View v) {
        super(v);
        title = (TextView) v.findViewById(R.id.title);
        date = (TextView) v.findViewById(R.id.date);
        image = (ImageView) v.findViewById(R.id.image);
        lyt_parent = (LinearLayout) v.findViewById(R.id.lyt_parent);
    }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    if (viewType == TYPE_HEADER) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_header, parent, false);
        return new ViewHolderHeader(v);
    } else if (viewType == TYPE_ITEM) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_news, parent, false);
        return new ViewHolder(v);
    }
    return null;
    }

    // Replace the contents of a view (invoked by the layout manager)
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
    if (holder instanceof ViewHolderHeader) {
        ViewHolderHeader vHeader = (ViewHolderHeader) holder;
        vHeader.title.setText(header.getTitle());
        vHeader.date.setText(header.getDate());
        Picasso.with(ctx).load(header.getImage()).into(vHeader.image);
        vHeader.lyt_parent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //TODO: do your thing
            }
        });

    } else if (holder instanceof ViewHolder) {
        final NewsArticles c = items.get(position);
        ViewHolder vItem = (ViewHolder) holder;
        vItem.title.setText(c.getTitle());
        vItem.short_content.setText(c.getShort_content());
        vItem.date.setText(c.getDate());
        Picasso.with(ctx).load(c.getImage()).into(vItem.image);
        vItem.lyt_parent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //TODO: do your thing
            }
        });
    }

    }

    //    need to override this method
    @Override
    public int getItemViewType(int position) {
    if (isPositionHeader(position)) {
        return TYPE_HEADER;
    }
    return TYPE_ITEM;
    }

    private boolean isPositionHeader(int position) {
    return position == 0;
    }

    public NewsArticles getItem(int position) {
    return items.get(position);
    }

    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {
    return items.size();
    }

}