2015-10-29 30 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

是的,绝对。卡是一个自定义类 –

相关问题