简体   繁体   中英

Layout not refreshing from RecyclerView in LinearLayout

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() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM