如何将页脚按钮添加到我的回收站视图
Posted
技术标签:
【中文标题】如何将页脚按钮添加到我的回收站视图【英文标题】:How to add a footer button to my recycler view 【发布时间】:2020-10-31 01:27:39 【问题描述】:我想在我的回收站视图中添加页脚。我在下面附上了自定义适配器代码。我怎样才能做到这一点?我希望按钮用新项目填充回收站视图。我可以自己操作按钮。我只遇到添加页脚的问题。
我想要如下图所示:
package com.example.myapplication.Adapter;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.example.myapplication.Interface.ItemClickListener;
import com.example.myapplication.Model.RSSObject;
import com.example.myapplication.R;
class FeedViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener
public TextView textTitle,txtDate,textContent;
private ItemClickListener itemClickListener;
public FeedViewHolder(View itemView)
super(itemView);
textTitle = (TextView) itemView.findViewById(R.id.textTitle);
txtDate = itemView.findViewById(R.id.textPubDate);
textContent = itemView.findViewById(R.id.textContent);
itemView.setOnClickListener(this);
itemView.setOnLongClickListener(this);
public FeedViewHolder(View itemView, ItemClickListener itemClickListener)
super(itemView);
this.itemClickListener = itemClickListener;
@Override
public void onClick(View v)
itemClickListener.onClick(v, getAdapterPosition(), false);
@Override
public boolean onLongClick(View v)
itemClickListener.onClick(v, getAdapterPosition(), true);
return true;
public class FeedAdapter extends RecyclerView.Adapter<FeedViewHolder>
private RSSObject.RssObject rssObject;
private Context mContext;
private LayoutInflater layoutInflater;
public FeedAdapter(RSSObject.RssObject rssObject, Context mContext)
this.rssObject = rssObject;
this.mContext = mContext;
layoutInflater = layoutInflater.from(mContext);
@Override
public int getItemViewType(int position)
return super.getItemViewType(position);
@Override
public FeedViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
final View itemView = layoutInflater.inflate(R.layout.row,parent,false);
return new FeedViewHolder(itemView);
@Override
public void onBindViewHolder(final FeedViewHolder holder, final int position)
holder.itemView.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
String url = rssObject.getItems().get(holder.getAdapterPosition()).getLink();
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
v.getContext().startActivity(i);
);
String title = rssObject.getItems().get(position).getTitle();
title = title.replace("&","");
holder.textTitle.setText(title);
String description = rssObject.getItems().get(position).getDescription();
description = android.text.html.fromHtml(description).toString();
holder.textContent.setText(description);
holder.txtDate.setText(rssObject.getItems().get(position).getPubDate());
@Override
public int getItemCount()
return rssObject.items.size();
【问题讨论】:
【参考方案1】:有两种方法: 如果你想在你的 xml 文件中的 RecyclerView 下放置一个固定视图,一个相对布局(或 orher),里面有按钮 或者,如果您希望按钮作为列表的一个元素,您应该添加一个带有按钮的视图,并在您的视图的最后位置广告
【讨论】:
是的,我希望将按钮作为回收站视图的最后一个元素...但我不知道该怎么做...请您详细解释一下...【参考方案2】:如果您希望添加页脚,则需要将其指定为另一种视图类型。 在您的适配器代码中,您应该添加为页脚创建另一个支架的逻辑。 请参阅以下 Kotlin 示例:
const val REGULAR_VIEW_TYPE = 0
const val FOOTER_VIEW_TYPE = 1
class FooterExampleAdapter(
private val data: List<String>
) : RecyclerView.Adapter<RecyclerView.ViewHolder>()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
when (viewType) // Use 'when' to create the correct holder for any position
REGULAR_VIEW_TYPE -> RegularViewHolder(parent.inflateView(R.layout.regular_layout))
FOOTER_VIEW_TYPE -> FooterViewHolder(parent.inflateView(R.layout.footer_layout))
// Handle binding by checking holder type
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int)
if (holder is RegularViewHolder) holder.bind(data[position])
else if (holder is FooterViewHolder) holder.bind()
override fun getItemCount() = data.size + 1 // We need the extra 1 for the footer to be counted
// Here is where we specify which view type should be presented at position
override fun getItemViewType(position: Int) =
if (position == data.size) FOOTER_VIEW_TYPE else REGULAR_VIEW_TYPE
class RegularViewHolder(v: View) : RecyclerView.ViewHolder(v)
fun bind(string: String)
class FooterViewHolder(v: View) : RecyclerView.ViewHolder(v)
fun bind()
【讨论】:
以上是关于如何将页脚按钮添加到我的回收站视图的主要内容,如果未能解决你的问题,请参考以下文章
如何将点击监听器添加到我的回收器视图(Android kotlin)[重复]