具有多个视图的Android Recycler视图

时间:2019-07-05 06:22:36

标签: java android firebase firebase-storage

我正在制作一个应用程序,希望将文本,图像和视频全部放在单独的ViewHolders中(类似于Instagram和Facebook)。我知道我们必须将Recycler View与多个ViewTypes一起使用。我正在使用Firebase作为存储。如何从Firebase检索项目并将它们放置在适当的ViewHolder中。请帮助我。

4 个答案:

答案 0 :(得分:1)

公共类GovtjobsAdapter扩展了RecyclerView.Adapter {

private List<GovtjobBean> govtjobList;

public class MyViewHolder extends RecyclerView.ViewHolder {
    public TextView tv_govtjob_title,tv_govtjob_lastdatetoapply,tv_govtjob_location;

    public MyViewHolder(View view) {
        super(view);
        tv_govtjob_title = (TextView) view.findViewById(R.id.tv_govtjobs_title);
        tv_govtjob_lastdatetoapply = (TextView) view.findViewById(R.id.tv_govtjob_lastdatetoapply);
        tv_govtjob_location = (TextView) view.findViewById(R.id.tv_govtjob_location);
    }
}


public GovtjobsAdapter(List<GovtjobBean> govtjobList) {
    this.govtjobList = govtjobList;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.govtjob_lists, parent, false);

    return new MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    GovtjobBean govtjoblist = govtjobList.get(position);
    holder.tv_govtjob_title.setText(govtjoblist.getGovt_title());
    holder.tv_govtjob_lastdatetoapply.setText(govtjoblist.getGovt_lastdatetoapply());
    holder.tv_govtjob_location.setText(govtjoblist.getGovt_location());
}

@Override
public int getItemCount() {
    return govtjobList.size();
}

}

答案 1 :(得分:0)

您可以使用适配器的getItemViewType(int position)方法来执行此操作。在此方法中,您可以检查当前位置的项目类型是图像,视频还是文本,并为每个项目返回特定的值。然后,在您的onCreateViewHolder(ViewGroup parent, Int viewType)中,可以使用该viewType参数来扩展视图。

答案 2 :(得分:0)

您将需要创建一个自定义的回收器视图类,在其中重写onCreateViewHolder(ViewGroup parent,Int viewType)。

这是我的例子:

在recyclerview类中的

(1)创建自定义视图持有者类。示例:

class ViewHolderCurrent extends RecyclerView.ViewHolder {

    TextView locationTV;
    TextView conditionTV;
    ...

    public ViewHolderCurrent(View listItemView) {
        super(listItemView);

        locationTV = listItemView.findViewById(R.id.location_main_4);
        conditionTV = listItemView.findViewById(R.id.condition_main_4);
        ...
    }
}

(2)将onCreateViewHolder覆盖到您的自定义视图持有人:

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    switch (viewType) {
        case 1:
            View viewCurrent = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item4, parent, false);
            return new ViewHolderCurrent(viewCurrent);

        case 2:
        ...

        case 3:
        ...
    }

    return null;
}

(3)重写onBindViewHolder()以填充您选择的视图持有者。示例:

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    switch (holder.getItemViewType()) {
        case 1:
            ViewHolderCurrent viewHolderCurrent = (ViewHolderCurrent) holder;

            viewHolderCurrent.locationTV.setText(*text*);
            viewHolderCurrent.conditionTV.setText(*text*);

            break;

        case 2:
        ...
        }
}

我的完整源代码是here

答案 3 :(得分:0)

您必须使用recyclerview ViewTypes ,请检查此链接中的示例:

Android RecyclerView Example – Multiple ViewTypes

或者您可以使用FastAdapter之类的库,它是处理适配器项类型的出色库:

https://github.com/mikepenz/FastAdapter