2015-10-29 27 views
1

我在我的應用程序中使用外部卡庫(gabrielemariotti cardlib)。 特別是我正在使用CardRecyclerView擴展RecyclerView和CardArrayRecyclerViewAdapter擴展BaseRecyclerViewAdapter。 我有兩個活動主要活動(帶CardRecyclerView)和ProductCard活動如何在onSaveInstanceState中保存

我的目標是當我從其他活動(ProductCard)返回時或在打開MainActivity的方向時,將所有卡片保存在主活動中。 不幸的是我不明白如何在onSaveInstanceState中保存我的'mRecyclerView'。誰能幫忙?

package com.example.dmitry.myfoodbasket; 

import android.content.Intent; 
import android.os.Bundle; 
import android.support.design.widget.FloatingActionButton; 
import android.support.design.widget.Snackbar; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.LinearLayoutManager; 
import android.support.v7.widget.Toolbar; 
import android.util.Log; 
import android.view.View; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.Toast; 

import java.io.Serializable; 
import java.util.ArrayList; 

import it.gmariotti.cardslib.library.cards.actions.BaseSupplementalAction; 
import it.gmariotti.cardslib.library.cards.actions.IconSupplementalAction; 
import it.gmariotti.cardslib.library.cards.material.MaterialLargeImageCard; 
import it.gmariotti.cardslib.library.internal.Card; 
import it.gmariotti.cardslib.library.internal.CardHeader; 
import it.gmariotti.cardslib.library.recyclerview.internal.CardArrayRecyclerViewAdapter; 
import it.gmariotti.cardslib.library.recyclerview.view.CardRecyclerView; 

public class MainActivity extends AppCompatActivity { 
    final String LOG_TAG = "myLogs"; 
    ArrayList<BaseSupplementalAction> actions; 
    ArrayList<Card> cards; 
    MaterialLargeImageCard card; 
    CardArrayRecyclerViewAdapter mCardArrayAdapter; 
    CardRecyclerView mRecyclerView; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Log.d(LOG_TAG, "onCreate"); 


     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       /*Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
         .setAction("Action", null).show();*/ 
       cards.add(card); 
       mCardArrayAdapter.notifyDataSetChanged(); 
      } 
     }); 



     cards = new ArrayList<Card>(); 
     card = 
       MaterialLargeImageCard.with(this) 
         .setTextOverImage("Italian Beaches") 
         .useDrawableId(R.drawable.im_beach) 
         .setupSupplementalActions(R.layout.carddemo_native_material_supplemental_actions_large_icon,actions) 
         .build(); 
     cards.add(card); 
     card.setOnClickListener(new Card.OnCardClickListener() { 
      @Override 
      public void onClick(Card card, View view) { 
       Intent intent=new Intent(MainActivity.this,ProductCard.class); 
       startActivity(intent); 




      }}); 

     actions = new ArrayList<BaseSupplementalAction>(); 

     IconSupplementalAction t1 = new IconSupplementalAction(this, R.id.ic1); 
     t1.setOnActionClickListener(new BaseSupplementalAction.OnActionClickListener() { 
      @Override 
      public void onClick(Card card, View view) { 
       Toast.makeText(MainActivity.this, " Click on Text SHARE ", Toast.LENGTH_SHORT).show(); 
      } 
     }); 
     actions.add(t1); 

     IconSupplementalAction t2 = new IconSupplementalAction(this, R.id.ic2); 
     t2.setOnActionClickListener(new BaseSupplementalAction.OnActionClickListener() { 
      @Override 
      public void onClick(Card card, View view) { 
       Toast.makeText(MainActivity.this," Click on Text LEARN ", Toast.LENGTH_SHORT).show(); 
      } 
     }); 
     actions.add(t2); 

     IconSupplementalAction t3 = new IconSupplementalAction(this, R.id.ic3); 
     t3.setOnActionClickListener(new BaseSupplementalAction.OnActionClickListener() { 
      @Override 
      public void onClick(Card card, View view) { 
       Toast.makeText(MainActivity.this, " Карточка удалена ", Toast.LENGTH_SHORT).show(); 
       cards.remove(card); 
       mCardArrayAdapter.notifyDataSetChanged(); 
      } 
     }); 
     actions.add(t3); 


     mCardArrayAdapter = new CardArrayRecyclerViewAdapter(this, cards); 
     mRecyclerView = (CardRecyclerView) this.findViewById(R.id.carddemo_recyclerview); 
     mRecyclerView.setHasFixedSize(false); 
     mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); 
     if (mRecyclerView != null) { 
      mRecyclerView.setAdapter(mCardArrayAdapter); 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 


} 
+1

您不能保存視圖,所有你能做的就是保存您在查看有數據。 –

+0

@SharpEdge OK。在這種情況下,你推薦我什麼?什麼是最適合我的問題的變種? –

+0

查看我的回答 –

回答

1

每當用戶旋轉屏幕時,您的活動將被破壞並重新創建。當屏幕改變方向時,系統破壞並重新創建前景活動。

爲了節省額外的數據有關的活動狀態,你必須重寫 的onSaveInstanceState() 回調方法

@Override 
public void onSaveInstanceState(Bundle savedInstanceState) { 
// Save the user's current game state 
savedInstanceState.putInt(STATE_SCORE, mCurrentScore); 
savedInstanceState.putInt(STATE_LEVEL, mCurrentLevel); 
// Always call the superclass so it can save the view hierarchy state 
super.onSaveInstanceState(savedInstanceState); } 

現在您可以保存在上面Bundle實例特定字符串,整數或事件定製Parelable對象。 當你的活動重新創建只是檢查是否捆綁onSavedInstance中的onCreate()爲空或不是

@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); // Always call the superclass first 

// Check whether we're recreating a previously destroyed instance 
if (savedInstanceState != null) { 
// Restore value of members from saved state and populare your RecyclerView again 
mCurrentScore = savedInstanceState.getInt(STATE_SCORE); 
mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL); 
} else { 
// Probably initialize members with default values for a new instance 
} 

你可以用你想保存,然後重新填充你的視圖像RecyclerView等與上述數據替換您保存在savedInstance中的數據作爲鍵值對。

請記住,Bundle並不是要存儲大量數據。

對不起,如果一些代碼的idents不是很清楚,它很難發佈答覆從移動。

爲了更清楚地說明,請參閱本official guide

+0

謝謝你的回答。但是你說過要使用像mCurrentScore或mCurrentLevel這樣的變量。他們是什麼類型的?我認爲它可以是字符串或int或類似的東西。但是我想將所有卡都保存在回收站中。我是否需要將ArrayList 卡作爲變量保存?將ArrayList保存爲變量是真的嗎? –

+0

卡是什麼?它是一些自定義類嗎?如果那樣的話,只要你的Card類正確地實現了Parcelable接口,你就可以將它保存在Bundle中。 –

+0

是的,絕對。卡是一個自定義類 –

相關問題