gpt4 book ai didi

java - 在 RecyclerView 的底部有一个 ImageView

转载 作者:太空宇宙 更新时间:2023-11-04 11:22:39 25 4
gpt4 key购买 nike

我有一个RecyclerView,我想在底部制作一个ImageView,如果它......只是为了清楚起见,不是屏幕底部,而是RecyclerView的底部,所以你必须滚动RecyclerView才能到达ImageView

ImageView 必须是 RecyclerView 的一部分吗?

最佳答案

1) Create a viewholder for the footer

public class FooterViewHolder extends ViewHolder {
public FooterViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Do whatever you want on clicking the item
}
});
}
}

2) Now on onCreateViewHolder() check for the correct viewholder to inflate the footer or the regular viewholder as appropriate:

    if (viewType == MY_FOOTER_VIEW) {
v = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_list_item_footer, parent, false);

FooterViewHolder footerViewHolder= new FooterViewHolder(v);

return vh;
}

//your regular viewholder inflation

3)Perform the correct binding:

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

try {
if (holder instanceof NormalViewHolder) {
NormalViewHolder vh = (NormalViewHolder) holder;

vh.bindView(position);
} else if (holder instanceof FooterViewHolder) {
FooterViewHolder vh = (FooterViewHolder) holder;
}
} catch (Exception e) {
e.printStackTrace();
}
}

4) on getItemCount() return +1 element

 return data.size() + 1;

5) override getItemViewType() to identify your new type for the footer.

@Override
public int getItemViewType(int position) {
if (position == data.size()) {
// This is where we'll add footer.
return FOOTER_VIEW;
}

return super.getItemViewType(position);
}

关于java - 在 RecyclerView 的底部有一个 ImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44738731/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com