How to add a button at the end of RecyclerView?
One way to do it would be to make your footer view a "ViewType" of your adapter.
In order to do that, overrides getItemViewType to return a different value for your last item.
@Override
public int getItemViewType(int position) {
return (position == mData.size()) ? VIEW_TYPE_FOOTER : VIEW_TYPE_CELL;
}
Then in the onCreateViewHolder, handle the different viewType.
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == VIEW_TYPE_CELL) {
//Create viewholder for your default cell
}
else {
//Create viewholder for your footer view
}
}
Don't forget to update the value return by getCount() by adding 1, and to distinguish the 2 types of ViewHolder in OnBindViewHolder (with instanceof for example).
How to add a button at the end of a recycler view (kotlin)?
You will achieve this by adding the RecyclerView
in a parent NestedScrollView
and then place the Button
below the RecyclerView
.
So your code should look like this
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
NOTE
Add
recyclerView.nestedScrollingEnabled = false
for scroll to work for sdk 21
Add a button (or any element) after the last recyclerview element Android
You will have to override getItemViewType(int position)
in your adapter.
public final int CONTENT_TYPE = 1;
public final int BUTTON_TYPE = 2;
Lets say your cursor count is 'x'. For positions 0 to (x-1), you will inflate the cardview with the cursor content. Hence, getItemViewType
will return CONTENT_TYPE for positions 0 to (x-1). Next, for position 'x', return BUTTON_TYPE.
You will get hold of the view type in the onCreateXXX
method of your adapter. Based on the view type, inflate the right xml (button or cursor content).
Be sure to return cursor count + 1 in the getItemCount() method. (+1 being for the button layout).
Is there a way to add a button or anything under a RecyclerView
Android recyclerview is used to display large amount of items on
screen, but with improved performance and other benefits. RecyclerView
in Android uses an adapter to represent the information we want to
show as list. The data to be displayed is sent to adapter, which handles the view.
Coming to your custom approach of adding a button. You can control the amount of data being sent to adapter at beginning.
Considering you have a server that will send data when requested with a tweak to send data in set of row.
Add a floating button which is displayed when user reaches end of recyclerview, this button requests further data from server and send a signal to adapter
that your variable containing the data has been updated. So, this will add data to your view and you can scroll further until that data ends.- So you require to design your server for custom data range request.
- Variable to keep track of current data range present.
- Code to check end of RecyclerView data feed.
- function to request more data on button click and signal adapter for update.
You can search endless scrolling using RecyclerView to get tutorial
related to this.
How to add a plus image as a button at the end of my RecyclerView in Android?
You need to switch on viewType in onBindViewHolder
.
override fun onBindViewHolder(holder: CustomAdapterExercise.ViewHolder, position: Int) {
if (holder.itemViewType == typeAdd) {
// Bind the add button
else {
holder.bindItems(exerciseList[position])
}
}
Sidenote:
You're using a ViewHolder the wrong way, one of the benefits of ViewHolders is not being forced to do findByView
every time you update a View.
Related Topics
What Are The Default Color Values for The Holo Theme on Android 4.0
Retrofit 2 with Only Form-Data
Using Videoview for Streaming or Progressive-Download Video
How to Create a Looper Thread, Then Send It a Message Immediately
How to Create a Service Which Continuously Monitors App Usage Information
Android/iOS - Custom Uri/Protocol Handling
How to Secure My App Against Piracy
What's "Requestcode" Used for on Pendingintent
Change The Background Color of a Pop-Up Dialog
How to Make Facebook's App New Menu on Android
Receive Audio via Bluetooth in Android
Pass 2D Array to Another Activity
Flutter: Upgrade The Version Code for Play Store
How to Get Android Avd Name from Adb Device Name