具有多个视图的 Android Recycler View

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



江户川乱折腾
浏览 200回答 4
4回答

小唯快跑啊

公共类 GovtjobsAdapter 扩展 RecyclerView.Adapter {private List<GovtjobBean> govtjobList;public class MyViewHolder extends RecyclerView.ViewHolder {&nbsp; &nbsp; public TextView tv_govtjob_title,tv_govtjob_lastdatetoapply,tv_govtjob_location;&nbsp; &nbsp; public MyViewHolder(View view) {&nbsp; &nbsp; &nbsp; &nbsp; super(view);&nbsp; &nbsp; &nbsp; &nbsp; tv_govtjob_title = (TextView) view.findViewById(R.id.tv_govtjobs_title);&nbsp; &nbsp; &nbsp; &nbsp; tv_govtjob_lastdatetoapply = (TextView) view.findViewById(R.id.tv_govtjob_lastdatetoapply);&nbsp; &nbsp; &nbsp; &nbsp; tv_govtjob_location = (TextView) view.findViewById(R.id.tv_govtjob_location);&nbsp; &nbsp; }}public GovtjobsAdapter(List<GovtjobBean> govtjobList) {&nbsp; &nbsp; this.govtjobList = govtjobList;}@Overridepublic MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {&nbsp; &nbsp; View itemView = LayoutInflater.from(parent.getContext())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .inflate(R.layout.govtjob_lists, parent, false);&nbsp; &nbsp; return new MyViewHolder(itemView);}@Overridepublic void onBindViewHolder(MyViewHolder holder, int position) {&nbsp; &nbsp; GovtjobBean govtjoblist = govtjobList.get(position);&nbsp; &nbsp; holder.tv_govtjob_title.setText(govtjoblist.getGovt_title());&nbsp; &nbsp; holder.tv_govtjob_lastdatetoapply.setText(govtjoblist.getGovt_lastdatetoapply());&nbsp; &nbsp; holder.tv_govtjob_location.setText(govtjoblist.getGovt_location());}@Overridepublic int getItemCount() {&nbsp; &nbsp; return govtjobList.size();}}

人到中年有点甜

您必须使用 recyclerview ViewTypes,检查此链接中的示例:Android RecyclerView 示例——多个 ViewType或者你可以使用像 FastAdapter 这样的库,它是处理适配器项类型的很棒的库:https://github.com/mikepenz/FastAdapter

HUH函数

您将需要创建一个自定义回收器视图类,您可以在其中重写 onCreateViewHolder(ViewGroup parent, Int viewType)。这是我的例子:(1) 在您的 recyclerview 类中创建自定义视图持有者类。例子:class ViewHolderCurrent extends RecyclerView.ViewHolder {&nbsp; &nbsp; TextView locationTV;&nbsp; &nbsp; TextView conditionTV;&nbsp; &nbsp; ...&nbsp; &nbsp; public ViewHolderCurrent(View listItemView) {&nbsp; &nbsp; &nbsp; &nbsp; super(listItemView);&nbsp; &nbsp; &nbsp; &nbsp; locationTV = listItemView.findViewById(R.id.location_main_4);&nbsp; &nbsp; &nbsp; &nbsp; conditionTV = listItemView.findViewById(R.id.condition_main_4);&nbsp; &nbsp; &nbsp; &nbsp; ...&nbsp; &nbsp; }}(2) 将 onCreateViewHolder 覆盖到您的自定义视图持有者:@Overridepublic RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {&nbsp; &nbsp; switch (viewType) {&nbsp; &nbsp; &nbsp; &nbsp; case 1:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; View viewCurrent = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item4, parent, false);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return new ViewHolderCurrent(viewCurrent);&nbsp; &nbsp; &nbsp; &nbsp; case 2:&nbsp; &nbsp; &nbsp; &nbsp; ...&nbsp; &nbsp; &nbsp; &nbsp; case 3:&nbsp; &nbsp; &nbsp; &nbsp; ...&nbsp; &nbsp; }&nbsp; &nbsp; return null;}(3)重写 onBindViewHolder() 以填充您选择的查看器。例子:@Overridepublic void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {&nbsp; &nbsp; switch (holder.getItemViewType()) {&nbsp; &nbsp; &nbsp; &nbsp; case 1:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ViewHolderCurrent viewHolderCurrent = (ViewHolderCurrent) holder;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; viewHolderCurrent.locationTV.setText(*text*);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; viewHolderCurrent.conditionTV.setText(*text*);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case 2:&nbsp; &nbsp; &nbsp; &nbsp; ...&nbsp; &nbsp; &nbsp; &nbsp; }}我的完整源代码在这里

红糖糍粑

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

相关分类

Java