gpt4 book ai didi

android - 如何为 RecyclerView 添加 FooterView

转载 作者:行者123 更新时间:2023-11-29 14:36:06 27 4
gpt4 key购买 nike

您好,我是 Android Material 设计的新手,在我的应用程序中,我正在使用 RecyclerView,我需要为此 RecyclerView 添加 footerView,我在下面编写了用于添加 footerView 的代码

但是根据我的代码,它在 Header 上添加了为什么会发生这种情况,有人可以帮助我如何将其添加为 footerView

我的代码:-

public class PersonalInformationRecyclerLanguagesAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

private static final int TYPE_FOOTER = 0;
private static final int TYPE_ITEM = 1;
RecyclerViewFooter recyclerViewFooter;
private List<LanguagesBean> languagesBeanList;
private Context context;

public PersonalInformationRecyclerLanguagesAdapter(Context context, RecyclerViewFooter recyclerViewFooter, List<LanguagesBean> languagesBeanList) {

this.context = context;
this.recyclerViewFooter = recyclerViewFooter;
this.languagesBeanList = languagesBeanList;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

if (viewType == TYPE_FOOTER) {

View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.lagnguages_recyclerview_footer_layout, parent, false);
return new VHFooter(v);

} else if (viewType == TYPE_ITEM) {

View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_languages_list_items, parent, false);
return new VHItem(v);
}
return null;
}

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

if (holder instanceof VHFooter) {

VHFooter VHfooter = (VHFooter) holder;
VHfooter.addMoreButton.setText(recyclerViewFooter.getAddMore());
VHfooter.nextButton.setText(recyclerViewFooter.getNext());

} else if (holder instanceof VHItem) {

LanguagesBean currentItem = getItem(position - 1);
VHItem VHitem = (VHItem) holder;
VHitem.languageName.setText(currentItem.getName());
}
}

private LanguagesBean getItem(int position) {

return languagesBeanList.get(position);
}

@Override
public int getItemCount() {

return languagesBeanList.size() + 1;
}

@Override
public int getItemViewType(int position) {

if (isPositionFooter(position))
return TYPE_FOOTER;
return TYPE_ITEM;
}

private boolean isPositionFooter(int position) {

return position == 0;
}

class VHFooter extends RecyclerView.ViewHolder {

private Button addMoreButton, nextButton;

public VHFooter(View itemView) {

super(itemView);
addMoreButton = (Button) itemView.findViewById(R.id.addMore_button);
nextButton = (Button) itemView.findViewById(R.id.next_button);
}
}

class VHItem extends RecyclerView.ViewHolder {

private TextView languageName;

public VHItem(View view) {
super(view);
languageName = (TextView) view.findViewById(R.id.language_textview);
}
}
}

Activity :-

 private void intializingView(View view) {
//Implement Recycler View
recyclerView = (RecyclerView) view.findViewById(R.id.languages_recyclerView);
recyclerView.setHasFixedSize(true);

LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);

languagesBeanArrayList = new ArrayList<>();
setRecyclerViewData();
personalInformationRecyclerLanguagesAdapter = new PersonalInformationRecyclerLanguagesAdapter(getActivity(), getFooter(), languagesBeanArrayList);

//RecyclerView.ItemDecoration itemDecoration = new RecyclerViewDividerItemDecoration(getActivity());
//recyclerView.addItemDecoration(itemDecoration);

recyclerView.setAdapter(personalInformationRecyclerLanguagesAdapter);

} catch (Throwable throwable) {
throwable.printStackTrace();
}
}

/**
* setRecyclerViewData
*/
private void setRecyclerViewData() {

for (int i = 0; i < 5; i++) {
languagesBeanArrayList.add(new LanguagesBean("English", 1));
}
}

最佳答案

编辑:

方法一:

只是改变

private boolean isPositionFooter(int position) {

return position == 0;
}

private boolean isPositionFooter(int position) {

return position == languagesBeanList.size() + 1;
}

方法二:

您可以使用此 GitHub 库以最简单的方式将页眉或页脚添加到您的 RecyclerView。

https://github.com/lopspower/HFRecyclerView

您需要在您的项目中添加 HFRecyclerView 库,或者您也可以从 Gradle 中获取它:

compile 'com.mikhaellopez:hfrecyclerview:1.0.0'

这个库是基于在 Is there an addHeaderView equivalent for RecyclerView? @海斯特

所以我相信这是最好的方法。如果仍然不清楚,请按照该库中提供的示例进行操作。

如果有帮助,请标记。

关于android - 如何为 RecyclerView 添加 FooterView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38849619/

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