RecyclerView
中的LinearLayout
ScrollView
位于RecyclerView
。
我从网上获取notifyDataSetChanged
的项目,所以我要<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>
。
这有效 - 我在适配器中获取新数据。但视图不会改变。
这里是我上面写的xml:
public void initAdapter(ArrayList<Stream> list) {
// initializing: passed the streams element
this.streams.addAll(list);
// the RecyclerView Adapter
mBitmovinAdapter.notifyDataSetChanged();
mLinearLayout.invalidate();
mRecyclerView.invalidate();
}
当我收到数据时......:
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();
}
和我的适配器..:
div
答案 0 :(得分:0)
将数据设置为适配器,然后调用notifyDataSetChanged。
mPagerAdapter.setData(streams);
mPagerAdapter.notifyDataSetChanged();
答案 1 :(得分:0)
在RecyclerView中尝试此操作 在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);
}
答案 2 :(得分:0)
在添加新元素之前尝试adapter.clear()
之类的内容。然后拨打adapter.addAll()
。