Gabriele Zanelli
Gabriele Zanelli

Reputation: 31

Android endless RecyclerView with one last row for adding new items

I want to create something like this from Google Keep.

Screenshots of Keep:

https://s31.postimg.org/o4wpmxj0b/photo_2016_06_28_22_05_18.jpg https://s31.postimg.org/6tngsmtrf/photo_2016_06_28_22_05_25.jpg

I have already created a RecyclerView and its Adapter/ViewHolder containing a list of items which have a TextView and a Checkbox.

I would really like to get that last one empty item, in order to add endless new items to the recycler view.

I tought about that and the only idea that I ended up with, was to manually add a row OUT of the RecyclerView, just after the end, and make it look exactly like its items.

But of course it doesn't seem a well done thing. Are there any other methods to do that?

Here is my code, I'm using a Firebase Adapter 'cause I need to sync data online, but I don't think there are differences.

 private void initTaskList() {
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    recyclerView.setHasFixedSize(true);

    subjectAdapter = new FirebaseRecyclerAdapter<String,TaskHolder>
            (String.class, R.layout.item_checkbox_text, TaskHolder.class, FirebaseUser.getTasksRef(eventID)) {
        @Override
        protected void populateViewHolder(TaskHolder viewHolder, String taskInfo, int position) {
            viewHolder.taskText.setText(taskInfo);
        }
    };
    recyclerView.setAdapter(subjectAdapter);
}

static class TaskHolder extends RecyclerView.ViewHolder {
    private TextView taskText;
    private CheckBox taskCheckbox;

    public TaskHolder(View itemView) {
        super(itemView);
        taskText = (TextView)itemView.findViewById(R.id.task_text);
        taskCheckbox = (CheckBox)itemView.findViewById(R.id.task_checkbox);
    }

}

Upvotes: 0

Views: 875

Answers (2)

mik dass
mik dass

Reputation: 381

I believe all you need is a recyclerView with a footer.Here is a library to help you add the footer to the endlessRecyclerView https://github.com/willblaschko/header-footer-recycler-view

This library will allow you to add many headers and footers to the recyclerView.

Now create a footer layout (it can be any kind of view like edittext or progress bar)

<ListView android:id="@id/android:list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"/>

 <-----Your Footer view defined here---->

Now find the footerView by it's id and set a onclickListener() to it and add it to the recyclerView as a footer by

 mAdapter.addFooter(FooterView)

You can remove it too at later stage by

mAdapter.removeFooter(FooterView);

Hope it helps.

Upvotes: 0

gabe3vino
gabe3vino

Reputation: 333

I don't remember the exact location off the top of my head, but I think in the xml where you define the greater R.layout.item_checkbox_text, you can just add the desired 'last' row after or within the recyclerview with its own xml tag. You can either make it a seperate element outside of the recyclerview, or as a row. Since you defined in your ViewHolder that the rows of the list will map to R.id.task_text and R.id.task_checkbox, you should be able to place it at the end without having it displayed multiple times.

EDIT: As an example, here's the same concept but with a header for a listview:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include layout="@layout/msgqueue_header"
        android:id="@+id/include" />

    <ListView android:id="@id/android:list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:drawSelectorOnTop="false"

        android:fadeScrollbars="false"/>

</LinearLayout>

If I placed that include after the ListView but within the same LinearLayout, it would instead function as a footer.

The trickier part will be then defining the on click behaviour for that last row so as to properly add the new data to your adapter and repopulate your view. It may be relatively simple though if you call notifyDatasetchanged() properly, assuming you have access to the underlying dataset attached to your adapter that stores your tasks info.

Upvotes: 0

Related Questions