I want to add items to RecyclerView when I click the "Add" button
This currently works but only once , meaning if I click the Add button the first time, the item is added and visible, but after that, nothing is added.
Here is my code for RecyclerView Adapter
public class Adapter extends RecyclerView.Adapter<Adapter.myViewHolder> {
List<Integer> listItem;
public Adapter(List<Integer> passedListItem){
this.listItem = passedListItem;
}
#Override
public myViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recyclerview_layout, parent, false);
myViewHolder holder = new myViewHolder(itemView);
return holder;
}
#Override
public void onBindViewHolder(myViewHolder holder, int position) {
int itemNumber = position+1;
holder.itemTextView.setText("Item Number " + itemNumber + ": " + listItem.get(position));
}
#Override
public int getItemCount() {
return listItem.size();
}
public class myViewHolder extends RecyclerView.ViewHolder {
TextView itemTextView;
public myViewHolder(View view){
super(view);
itemTextView = view.findViewById(R.id.tv_itemTextView);
}
}
}
Here's my MainActivity
public class MainActivity extends AppCompatActivity {
List<Integer> itemList = new ArrayList<>();
EditText itemEditText;
RecyclerView recyclerView;
Adapter rvAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView)findViewById(R.id.rv_itemsRecyclerView);
itemEditText = (EditText)findViewById(R.id.et_editText);
//Setting the layout and Adapter for RecyclerView
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
rvAdapter = new Adapter(itemList);
recyclerView.setAdapter(rvAdapter);
}
//Click listener for "Add" Button
public void onAddButtonClicked(View view) {
try {
int IntegerFormat = Integer.valueOf(itemEditText.getText().toString());
itemList.add(IntegerFormat);
rvAdapter.notifyItemInserted(itemList.size() - 1);
itemEditText.setText("");
} catch(NumberFormatException e) {
Toast.makeText(getApplicationContext(), "The field is empty",
Toast.LENGTH_SHORT).show();
}
}
}
When i click the Add button, the first item is added and is visible, but when I click the Add button second time, nothing happens.
Solved
EDIT: Apparently, my Recycler view layout had its width and height set to match_parent instead of wrap_content, so the second item was getting added after clicking the button but it was added way way below. And I was stupid enough to not even try to scroll down. Everything was just working fine but I was ignorent.
I just checked your code and it is working fine. The mistake is in the recyclerview_layout.xml file which you havent posted. What you have is this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/tv_itemTextView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="TextView" />
</LinearLayout>
Please change this to:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/tv_itemTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
Your first list item is filling the entire recycler view because your linear layout has android:layout_height="match_parent" instead of android:layout_height="wrap_content" so the first item is hiding the other items but they are there in the adapter. You can confirm this by logging in logcat.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
My goal is to display a list where each element (iteration) is displayed with two buttons:
The buttons would change a variable in the specific element of the list.
My problem: How do I create this gui in Android Studio?
Here I implement a simple example of RecyclerView in android (java). You can change it as you need:
Add item.xml in your layout folder, and write these lines in it:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="wrap_content">
<TextView
android:id="#+id/titleTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/option2Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/option1Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toLeftOf="#+id/option2Button"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
this is based on your sample image. this layout contains all views in a single row of list. if you want more buttons or texts in each row, you must add them in this file.
You always need an Adapter class for RecyclerViews. So, add MyAdapter.java class to your project as below:
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
{
private Context context;
private List<String> titles;
MyAdapter(Context context, List<String> titles)
{
this.context = context;
this.titles = titles;
}
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType)
{
RecyclerView.ViewHolder viewHolder;
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
viewHolder = getViewHolder(parent, inflater);
return viewHolder;
}
private RecyclerView.ViewHolder getViewHolder(ViewGroup parent, LayoutInflater inflater)
{
RecyclerView.ViewHolder viewHolder;
View v1 = inflater.inflate(R.layout.item, parent, false);
viewHolder = new ItemVH(v1);
return viewHolder;
}
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder holder, int position)
{
ItemVH itemVH = (ItemVH) holder;
// set title for each item:
itemVH.titleTextView.setText(titles.get(position));
itemVH.option1Button.setText("option 1");
itemVH.option2Button.setText("option 2");
// click listener for first button:
itemVH.option1Button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
// option 1 button clicked
}
});
// click listener for second button:
itemVH.option2Button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
// option 2 button clicked
}
});
}
#Override
public int getItemCount()
{
return titles.size();
}
// add item to list:
public void addItem(String title)
{
titles.add(title);
notifyItemInserted(titles.size() - 1);
}
// remove item from list:
public void remove(String title)
{
int position = titles.indexOf(title);
if (position > -1)
{
titles.remove(position);
notifyItemRemoved(position);
}
}
protected class ItemVH extends RecyclerView.ViewHolder
{
private TextView titleTextView;
private Button option1Button;
private Button option2Button;
public ItemVH(View itemView)
{
super(itemView);
titleTextView = itemView.findViewById(R.id.titleTextView);
option1Button = itemView.findViewById(R.id.option1Button);
option2Button = itemView.findViewById(R.id.option2Button);
}
}
}
The titles list is list of Strings shown on items. You create this list and send it to the MyAdapter class from MainActivity. all views in single item are find in ItemVH class, and you can work with these views in onBindViewHolder function.
In activity_main.xml add a RecyclerView:
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/recyclerView"/>
In MainActivity.java add these lines in onCreate:
// create list:
List<String> titles = new ArrayList<>();
titles.add("first item");
titles.add("second item");
titles.add("third item");
// define the adapter:
MyAdapter adapter = new MyAdapter(this, titles);
// set the RecyclerView:
RecyclerView recyclerView = findViewById(R.id.recyclerView);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
Result:
I've investigated several SO answers on this question (here, here, and here) and none of the proposed solutions have worked. My problem is that my RecyclerView list items aren't being displayed. I've set breakpoints in MessengerRecyclerAdapter, onCreateViewHolder, onBindViewHolder, and getItemCount and only the first one is ever called. While in a breakpoint I've entered the expression evaluator and executed
MessengerRecyclerAdapter.getItemCount();
And received the expected answer of 20. The RecyclerView itself takes up the intended content area as demonstrated by the screenshot below (I turned the RecyclerView magenta to highlight the space it occupies).
My RecyclerView XML code is below:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/thread_list"
android:background="#color/colorAccent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:name="com.jypsee.jypseeconnect.orgPicker.MessengerListFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="LinearLayoutManager"
tools:context="com.jypsee.jypseeconnect.orgPicker.MessengerListFragment"
tools:listitem="#layout/fragment_messenger_cell"/>
</LinearLayout>
My RecyclerView Cell XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/text_margin"
android:textAppearance="?attr/textAppearanceListItem"
android:textColor="#color/blueText"/>
<TextView
android:id="#+id/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/text_margin"
android:textAppearance="?attr/textAppearanceListItem"
android:textColor="#color/darkText"/>
</LinearLayout>
My ListFragment class:
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
List<DummyContent.DummyItem> items = new ArrayList<>();
for (Integer i = 0; i<20; i++){
DummyContent.DummyItem item = new DummyContent.DummyItem(i.toString(),"Content","Details");
items.add(item);
}
View view = inflater.inflate(R.layout.fragment_messenger_list, container, false);
mRecyclerView = (RecyclerView) view.findViewById(R.id.thread_list);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
mRecyclerAdapter = new MessengerThreadRecyclerAdapter(items, mListener);
mRecyclerView.setAdapter(mRecyclerAdapter);
mRecyclerAdapter.notifyDataSetChanged();
return view;
}
My Adapter class:
public class MessengerRecyclerAdapter
extends RecyclerView.Adapter<MessengerRecyclerAdapter.MessageThreadHolder>{
private final List<DummyItem> mValues;
private final RecyclerViewClickListener mListener;
public MessengerRecyclerAdapter(List<DummyItem> items, RecyclerViewClickListener listener) {
mValues = items;
mListener = listener;
}
#Override
public MessageThreadHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.fragment_messenger_cell, parent, false);
return new MessageThreadHolder(view);
}
#Override
public void onBindViewHolder(final MessageThreadHolder holder, final int position) {
holder.mItem = mValues.get(position);
holder.mIdView.setText(mValues.get(position).id);
holder.mContentView.setText(mValues.get(position).content);
holder.mView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mListener != null) {
mListener.recyclerViewListClicked(v, position);
}
}
});
}
#Override
public int getItemCount() {
return mValues.size();
}
public class MessageThreadHolder extends RecyclerView.ViewHolder {
public final View mView;
public final TextView mIdView;
public final TextView mContentView;
public DummyItem mItem;
public MessageThreadHolder(View view) {
super(view);
mView = view;
mIdView = (TextView) view.findViewById(R.id.id);
mContentView = (TextView) view.findViewById(R.id.content);
}
}
}
As you can see I've set the linearLayout orientation to vertical and set the layout manager, which were the 2 most common solutions. I'm really at a loss as to what to try next, so any help is appreciated.
As I said in previous Answer edit. The issues was in your xml. The main reason it was not showing was because, You were trying to add the fragment using include tag instead of fragment tag thus respective fragment class was never getting called on the fragment layout being added to your activity.
Below is the code you needed to add the Fragment correctly.
<fragment
android:id="#+id/message_thread"
android:name="com.jypsee.jypseeconnect.orgPicker.MessengerThreadListFragment"
layout="#layout/fragment_messengerthread_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="#layout/fragment_messengerthread_list" />
And your fragment layout should be like this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".orgPicker.MessengerThreadListFragment">
<android.support.v7.widget.RecyclerView
android:id="#+id/thread_list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Here is the screenshot of working
In my case , I was missing calling notifyDataSetChanged() after setting list in adapter.
I want to add card style in my app like this
i use in my app mysql database so i need to make like this cards and put my data from database in it now i use ListView with this code
public void listAllItme() {
ListAdapter lA = new listAdapter(listitems);
listView.setAdapter(lA);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent open = new Intent(R_arabic.this, rewaya_show.class);
open.putExtra("name", listitems.get(position).name);
open.putExtra("url", listitems.get(position).url);
open.putExtra("img", listitems.get(position).img);
open.putExtra("num", listitems.get(position).num);
startActivity(open);
}
}
});
}
class listAdapter extends BaseAdapter {
ArrayList<listitem_gib> lista = new ArrayList<listitem_gib>();
public listAdapter(ArrayList<listitem_gib> lista) {
this.lista = lista;
}
#Override
public int getCount() {
return lista.size();
}
#Override
public Object getItem(int position) {
return lista.get(position).name;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater layoutInflater = getLayoutInflater();
View view = layoutInflater.inflate(R.layout.row_item_gib, null);
TextView name = (TextView) view.findViewById(R.id.textView_gib);
ImageView img = (ImageView) view.findViewById(R.id.imageView_gib);
TextView num = (TextView) view.findViewById(R.id.textView_gib2);
TextView size = (TextView) view.findViewById(R.id.textView_gib3);
name.setText(lista.get(position).name);
num.setText(lista.get(position).num);
size.setText(lista.get(position).size);
Picasso.with(R_arabic.this).load("http://grassyhat.com/android/image/" + lista.get(position).img).into(img);
return view;
}
}
first i want to know how i can make like this card style
second how i can use this code with card menu not listview
sorry im new in android and sorry for my bad english
What do you mean by card menu? because the example in the image is a recyclerview with a cardview item, you can achieve this by doing something like this
This will be your activity
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView)findViewById(R.id.recyclerView);
//Just your list of objects, in your case the list that comes from the db
List<Items> itemsList = new ArrayList<>();
CardAdapter adapter = new CardAdapter(this, itemsList);
//RecyclerView needs a layout manager in order to display data so here we create one
StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
//Here we set the layout manager and the adapter to the listview
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
}
Inside the layout file you just have to place the recyclerview like this
<RelativeLayout
android:id="#+id/activity_main"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="jsondh.myapplication.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
Then your adapter will be something like this
public class CardAdapter extends RecyclerView.Adapter<CardAdapter.CardViewHolder> {
private List<Items> itemsList;
private Activity activity;
public CardAdapter(Activity activity, List<Items> items){
this.activity = activity;
this.itemsList = items;
}
#Override
public CardViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = activity.getLayoutInflater().inflate(R.layout.cardview_layout, parent, false);
return new CardViewHolder(itemView);
}
#Override
public void onBindViewHolder(CardViewHolder holder, int position) {
//Here you bind your views with the data from each object from the list
}
#Override
public int getItemCount() {
return itemsList.size();
}
public class CardViewHolder extends RecyclerView.ViewHolder {
public ImageView bookImage;
public TextView bookLabel01, bookLabel02;
public CardViewHolder(View itemView) {
super(itemView);
bookImage = (ImageView)itemView.findViewById(R.id.image);
bookLabel01 = (TextView)itemView.findViewById(R.id.label01);
bookLabel02 = (TextView)itemView.findViewById(R.id.label02);
}
}
And the last one will be the layout from each item on the list, like this
<RelativeLayout
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:layout_margin="10dp">
<android.support.v7.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardElevation="15dp"
app:cardBackgroundColor="#3369Ed">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/image"
android:layout_width="150dp"
android:layout_height="130dp"/>
<TextView
android:id="#+id/label01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Label"
android:layout_gravity="right"
android:padding="5dp"
android:textColor="#ffffff"/>
<TextView
android:id="#+id/label02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LongerLabel"
android:layout_gravity="right"
android:padding="5dp"
android:textColor="#ffffff"/>
</LinearLayout>
</android.support.v7.widget.CardView>
You also have to add this to your gradle file:
compile 'com.android.support:recyclerview-v7:25.0.0'
compile 'com.android.support:cardview-v7:25.0.0'
Hope it helps!
It's pretty simple. You will have to use a RecyclerView with GridLayoutManager and add a cardView to it.
Then, use an Adapter and ViewHolder to feed the data.
I suggest you to check this out:
https://developer.android.com/training/material/lists-cards.html
I have two recyclerViews (say A and B) in the same layout that have an adapter which for both the recyclerViews look like this:
public class ChapterListAdapter extends RecyclerView.Adapter<ChapterListAdapter.ChapterListViewHolder>{
private final ArrayList<ChapterObj> mChapterListObj;
private final Context mContext;
public ChapterListAdapter(ArrayList<ChapterObj> chapterObj, Context c) {
mChapterListObj = chapterObj;
mContext = c;
}
#Override
public ChapterListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.chapter_list_item, parent, false);
ChapterListViewHolder vh = new ChapterListViewHolder(v);
return vh;
}
#Override
public void onBindViewHolder(ChapterListViewHolder holder, int position) {
holder.chapterNumber.setText(mChapterListObj.get(position).getChapterNumber());
}
#Override
public int getItemCount() {
return mChapterListObj.size();
}
public class ChapterListViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnTouchListener {
private TextView chapterNumber;
public ChapterListViewHolder(View itemView) {
super(itemView);
chapterNumber = (TextView) itemView.findViewById(R.id.chapter_number);
itemView.setOnClickListener(this);
itemView.setOnTouchListener(this);
}
#Override
public void onClick(View v) {
Log.d("hello", "hello");
}
}
}
}
Both A and B have click listeners. Independently, both work fine. But when both are in the same layout, whenever I click on an item of 'A', the click listener of 'B' gets triggered.
If you need to see more code, tell me which file you want to see, I'll add their code too.
Edit: The xml layout file in which I've used them together looks like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".VerseActivity">
<android.support.v7.widget.RecyclerView
android:id="#+id/chapter_list_menu"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
xmlns:android="http://schemas.android.com/apk/res/android"
>
</android.support.v7.widget.RecyclerView>
<android.support.v7.widget.RecyclerView
android:id="#+id/verse_list_menu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentTop="true"
xmlns:android="http://schemas.android.com/apk/res/android"
>
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
You can do same thing with Sectioned recycler view. which will even load faster and let you display as many sectioned as you want. (removes hurdle of 2-3 recyclerview in same page or view)
If you want to create your own you can check this - https://gist.github.com/gabrielemariotti/4c189fb1124df4556058
or you can find many libraries for that also.
I've a RecyclerView in a LinearLayout which is in a ScrollView.
I get the items for the RecyclerView from the net so I've to notifyDataSetChanged.
This is working - I get the new data in the Adapter. But the view doesn't change.
Here the xml like I wrote above:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="#+id/viewPager_featured"
android:layout_width="match_parent"
android:layout_height="#dimen/featured_object_height"
android:layout_gravity="center" />
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" />
</LinearLayout>
when I get the data...:
public void initAdapter(ArrayList<Stream> list) {
// initializing: passed the streams element
this.streams.addAll(list);
// the RecyclerView Adapter
mBitmovinAdapter.notifyDataSetChanged();
mLinearLayout.invalidate();
mRecyclerView.invalidate();
}
and my adapter..:
BitmovinAdapter(ArrayList<Stream> streams) {
this.mStreams = streams;
}
ArrayList<Stream> mStreams;
class BitmovinHolder extends RecyclerView.ViewHolder {
TextView title;
TextView format;
ImageView poster;
public BitmovinHolder(final View view) {
super(view);
title = (TextView) view.findViewById(R.id.title);
format = (TextView) view.findViewById(R.id.format);
poster = (ImageView) view.findViewById(R.id.poster);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
BasePlayerActivity.launchPlayerActivity(view.getTag().toString(), title.getText().toString(), 0, MainActivity.debug, view.getContext());
}
});
}
}
#Override
public void onBindViewHolder(BitmovinHolder holder, int position) {
Stream stream = mStreams.get(position);
holder.title.setText(stream.title);
holder.format.setText(stream.format);
Glide.with(holder.poster.getContext()).load(stream.poster).into(holder.poster);
holder.itemView.setTag(stream.stream);
}
#Override
public BitmovinHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new BitmovinHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false));
}
#Override
public int getItemCount() {
return mStreams.size();
}
Set the data to adapter and then call notifyDataSetChanged.
mPagerAdapter.setData(streams);
mPagerAdapter.notifyDataSetChanged();
Try this In RecyclerView
Add this Method in your RecyclerView.Adapter
ArrayList<Stream> list
/*
* Inserting a new item at the head of the list. This uses a specialized
* RecyclerView method, notifyItemInserted(), to trigger any enabled item
* animations in addition to updating the view.
*/
public void addItem(int position) {
if (position > list.size()) return;
list.add(position, generateDummyItem());
notifyItemInserted(position);
}
Try something like adapter.clear() before adding new elements. Then call adapter.addAll().