为了账号安全,请及时绑定邮箱和手机立即绑定

具有多个视图的 Android Recycler View

具有多个视图的 Android Recycler View

江户川乱折腾 2023-03-09 17:04:13
我正在制作一个应用程序,我想在其中将文本、图像和视频全部放在单独的 ViewHolders 中(类似于 Instagram 和 Facebook)。我知道我们必须使用具有多个 ViewTypes 的 Recycler View。我正在使用 firebase 作为我的存储。我如何从 firebase 检索项目并将它们放在适当的 ViewHolder 中。请帮我。
查看完整描述

4 回答

?
小唯快跑啊

TA贡献1863条经验 获得超2个赞

公共类 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();

}

}


查看完整回答
反对 回复 2023-03-09
?
人到中年有点甜

TA贡献1895条经验 获得超7个赞

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

Android RecyclerView 示例——多个 ViewType

或者你可以使用像 FastAdapter 这样的库,它是处理适配器项类型的很棒的库:

https://github.com/mikepenz/FastAdapter


查看完整回答
反对 回复 2023-03-09
?
HUH函数

TA贡献1836条经验 获得超4个赞

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


这是我的例子:


(1) 在您的 recyclerview 类中创建自定义视图持有者类。例子:


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:

        ...

        }

}

我的完整源代码在这里


查看完整回答
反对 回复 2023-03-09
?
红糖糍粑

TA贡献1815条经验 获得超6个赞

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



查看完整回答
反对 回复 2023-03-09
  • 4 回答
  • 0 关注
  • 199 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号