保存BaseRecyclerViewAdapter的状态

时间:2015-10-28 20:02:50

标签: android android-recyclerview state baseadapter

我在我的应用中使用外部卡片库(gabrielemariotti cardlib)。 我也有两个活动。

  1. 例如,在主要活动中我添加了两张三张卡
  2. 然后我进入了其中一个(去了ProductCard Activity)
  3. 如果我推了回来" - 我看到主要活动中的正常情况(所有两张卡都在那里)。但如果我保存按钮" OK" (从ProductCard Activity调用Intent到Main Activity)我看到只存在一张卡。
  4. 我发现saveInstanceState存在问题,但我不明白如何将CardArrayRecyclerViewAdapter保存在其中。当我从其他活动回来时,你能帮我理解如何在主要活动中保存所有卡吗? 谢谢

    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;
        @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);
            CardRecyclerView 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);
        }
    
    
    }
    
    
    package com.example.dmitry.myfoodbasket;
    
    import android.content.Intent;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.Button;
    
    
    
    /**
     * Created by Dmitry on 27.10.2015.
     */
    public class ProductCard extends AppCompatActivity implements AdapterView.OnItemClickListener {
        Button moneyOk_readylist;
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.product_card);
            moneyOk_readylist=(Button)findViewById(R.id.moneyOk_readylist);
            moneyOk_readylist.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(ProductCard.this,MainActivity.class);
                    startActivity(intent);
                }
            });
    
        }
    
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    
        }
    }
    

0 个答案:

没有答案