如何为RecyclerView添加FooterView

时间:2016-08-09 11:35:13

标签: android android-recyclerview

您好我是Android材料设计的新手,在我的应用程序中我使用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);
        }
    }
}

的活动: -

 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));
        }
    }

4 个答案:

答案 0 :(得分:4)

编辑:

方法1:

只需更改

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    if let savedArray = NSUserDefaults.standardUserDefaults().arrayForKey("TEMPLATES") {
        templates = NSMutableArray(array: savedArray)
    }
}

private boolean isPositionFooter(int position) {

    return position == 0;
}

方法2:

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

https://github.com/lopspower/HFRecyclerView

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

private boolean isPositionFooter(int position) {

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

这个图书馆是基于的工作 Is there an addHeaderView equivalent for RecyclerView? @hister

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

如果这有帮助,请将其标记。

答案 1 :(得分:1)

private boolean isPositionFooter(int position) {

    return position == getItemCount() - 1;
}

并且还改变了这个:

    @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);
        VHItem VHitem = (VHItem) holder;
        VHitem.languageName.setText(currentItem.getName());
    }
}

答案 2 :(得分:1)

  private boolean isPositionFooter (int position) {
        return position == languagesBeanList.size ();
    }

答案 3 :(得分:1)

我有同样的问题。我的代码工作得很好,直到我升级我的android工作室,从那时起我的页脚不再工作所以我只是添加了另一个LinearLayout,我设置了我的页脚并为标题做了同样的事情,每当我在Recyclerview上执行操作时我更新我的页脚也是手动的。也许你应该尝试那个选项

我做了类似的事

 <android.support.v7.widget.RecyclerView
                android:id="@+id/recycler"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

            </LinearLayout>