2016-04-29 47 views
0

您好我在viewpager中有一個recyclerview。它在應用程序啓動時加載數據。但當我更改查看尋呼機並返回到recyclerview頁面時,數據丟失。 Recylerview看起來很空。在viewpager中更改頁面後,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; 
    } 
+0

添加您的java類也 – Pavya

+0

我更新了我的問題 – hkaraoglu

+0

您在哪裏設置了適配器? – Pavya

回答

1

Android有一個生命週期,所以當你去到另一個活動的狀態變化。 enter image description here

檢查lifecycle android doc

檢查lifecycle fragment doc

所以,你需要做的是落實不onCreate方法的數據綁定。因爲視圖只是一次創建(第一次),所以在這之後,它會調用其他方法,比如onStart()。 在OnStart()或OnResume()方法中實現您的bindView。

+0

是的。我再次在onResume方法中設置適配器。但是,這是一個真正的邏輯? – hkaraoglu

+0

它的片段不在活動中 – Pavya

+0

@復仇對不起,我不明白你在問什麼。 – Adley

相關問題