I'm writing an app, where i need header in RecyclerView. I want to add it dynamically like in listview(addHeaderView) and change data in header(eg. on refresh).
Thx in advance!
I'm writing an app, where i need header in RecyclerView. I want to add it dynamically like in listview(addHeaderView) and change data in header(eg. on refresh).
Thx in advance!
If I clearly understood the problem I guess it is not so hard. Using the recycler view adapter it is possible to use different view holder classes to describe different view for different data. To implement correctly the adapter just override the methods for managing the creation and the binding of the view holders.
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private String[] mDataset;
// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public static class ViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView mTextView;
public ViewHolder(TextView v) {
super(v);
mTextView = v;
}
}
// Provide a suitable constructor (depends on the kind of dataset)
public MyAdapter(String[] myDataset) {
mDataset = myDataset;
}
// Create new views (invoked by the layout manager)
@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.my_text_view, parent, false);
// set the view's size, margins, paddings and layout parameters
...
ViewHolder vh = new ViewHolder(v);
return vh;
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
holder.mTextView.setText(mDataset[position]);
}
// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return mDataset.length;
}
}
Then to add dynamically an header that is described by a particular view holder you just have to add the an element to the structure that represents the data and use some logic to distinguish it from a normal view displayed for example using the position of the element, after doing so, reload the data of the adapter, it will bind again the view holders with the positions. If you need more details, tell me more on the concrete situation and I will give you an example.
I found error in my adapter, it was in getItemCount(), i made a mistake with two data's(for header and for items) so it was always returning 0 and nothing happened. I fixed it and now its working! Btw, thx to everybody who tried to help me! Bye :)
The simplest way to add a header to your RecyclerView is by using a CoordinatorLayout
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- Replace this ImageView with your header view -->
<ImageView
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="300dp"
app:layout_scrollFlags="scroll" />
</android.support.design.widget.AppBarLayout>
<!-- This can be any scrolling view -->
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>