在viewpager中更改页面后,RecylerView数据丢失了吗?

时间:2016-04-29 12:04:03

标签: android android-layout

您好我在viewpager中有一个recyclerview。它在应用程序启动时加载数据。但是当我更改查看寻呼机并返回到recyclerview页面时,数据丢失了。 Recylerview看起来很空白。

fragment_home.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
        <android.support.v7.widget.RecyclerView
            android:id="@+id/libraryBookList"
            android:scrollbars="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
</LinearLayout>

acitivity_home.xml

<?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">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <include layout="@layout/toolbar" />

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:layout_weight="1" />

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="fixed"
        app:tabGravity="fill"
        android:layout_gravity="bottom"
        android:background="@drawable/bottom_bar_background" />

</LinearLayout>

</android.support.design.widget.CoordinatorLayout>

LibraryProductListAdapter.class

public class LibraryProductListAdapter extends RecyclerView.Adapter<LibraryProductListAdapter.ViewHolder> {

    private ArrayList<LibraryProductModel> productList;
    private Context context;
    LibraryProductModel productModel;


    public LibraryProductListAdapter(Context context, ArrayList<LibraryProductModel> productList) {
        this.productList = productList;
        this.context = context;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        // each data item is just a string in this case
        public TextView productName;
        public TextView manufacturerName;
        public TextView publisherName;
        public ImageView image;
        public ImageView isShelvedIcon;

        public ViewHolder(View view) {
            super(view);
        }
    }

    @Override
    public LibraryProductListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.component_library_product, parent, false);

        ViewHolder holder = new ViewHolder(v);
        holder.productName = (TextView) v.findViewById(R.id.productName);
        holder.manufacturerName = (TextView)v.findViewById(R.id.manufacturerName);
        holder.publisherName = (TextView)v.findViewById(R.id.publisherName);
        holder.image = (ImageView)v.findViewById(R.id.productImage);
        holder.isShelvedIcon = (ImageView)v.findViewById(R.id.isShelvedIcon);

        holder.productName.setTypeface(App.MUSEO_300);
        holder.manufacturerName.setTypeface(App.MUSEO_300);
        holder.publisherName.setTypeface(App.MUSEO_300);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        productModel = productList.get(position);
        holder.productName.setText(productModel.getName());
        holder.manufacturerName.setText(productModel.getManufacturer());
        holder.publisherName.setText(productModel.getPublisher());
        Picasso.Builder builder = new Picasso.Builder(context);

        if(productModel.getIsUnshelved()){
            holder.isShelvedIcon.setAlpha(0.5F);
        }
        else {
            holder.isShelvedIcon.setAlpha(1.0F);
        }
        Picasso picasso = builder.build();
        picasso.with(context).load(productModel.getImage()).into(holder.image);
    }



    static boolean isAirplaneModeOn(Context context) {
        ContentResolver contentResolver = context.getContentResolver();
        return Settings.System.getInt(contentResolver, AIRPLANE_MODE_ON, 0) != 0;
    }
    @Override
    public int getItemCount() {
        return productList.size();
    }
}

HomeFragment.java

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_home, container, false);
        mRecyclerView = (RecyclerView) v.findViewById(R.id.libraryBookList);
        mRecyclerView.setHasFixedSize(true);
        mLayoutManager = new LinearLayoutManager(getActivity());
        mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.scrollToPosition(0);
        return v;
    }

1 个答案:

答案 0 :(得分:1)

Android有一个生命周期,所以当你去另一个活动时,状态会发生变化。 enter image description here

检查lifecycle android doc

检查lifecycle fragment doc

所以你需要做的是在onCreate方法中实现数据绑定。因为视图刚刚创建了一次(第一次),之后,它会调用其他方法,比如onStart()。 在OnStart()或OnResume()方法中实现bindView。