gabrielemariotti / cardslib

Android Library to build a UI Card
4.66k stars 1.19k forks source link

Activity containing CardListView opening after a long delay #496

Open safakadir opened 9 years ago

safakadir commented 9 years ago

Hi Gabriel!

First thanks to you for this awesome library. Unfortunately, I'm having some difficulties about using the cards efficiently.

I basically have an activity which loads a model and puts the data into a CardListView of _CardWithList_s. User clicks a view and a new intent started in OnClickListener; my new activity is created, data is loaded, cards are created.The problem is as follows: Whenever user clicks for new activity, display is frozen for a few seconds in previous activity (it's a long delay). I put some logs in my code, measured running time, so the process is completed in a very short time (15-20 ms) as soon as user clicks. But user have to wait a few seconds until he sees the new activity.

I thought the problem may be caused something I'm doing wrong about cards.

Here is my code:

public class ExampleActivity extends MyActivity {

    MyModel myModel;
    long startTime;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        startTime = System.currentTimeMillis();
        setContentView(R.layout.activity_ders_programi);
        setToolbar();

        setParentActivity();
    }

    @Override
    protected void onResume() {
        super.onResume();
        myModel = Controller.getMyModel();
        if(myModel == null) { //If we don't have the object previously
            //this fills the object, takes some time
            myModel = new MyModel(this);
            //myModel is observable and this activity is observer.
            //Whenever it's done, it notifies this activity
        }
        else //If we got the object previously
            createCardViews();
        Controller.setMyModel(myModel);
        long stopTime = System.currentTimeMillis();
        Log.d("modelflow", "dersprog: onResume ends in: " + (stopTime - startTime) + "ms");
    }

    //If Observable object notifies this activity, update() is called.
    @Override
    public void update(Observable observable, Object o) {
        createCardViews();
    }

   //Creating a list of cardViews, this also takes some time
   private void createCardViews() {
       ArrayList<Card> cards = new ArrayList<Card>();
       for(int i = 0; i < 5; i++) {
           MyModelCardModel card = new MyModelCardModel(this, i);
           card.init();
           cards.add(card);
       }

       CardArrayAdapter mCardArrayAdapter = new CardArrayAdapter(this,cards);

       CardListView listView = (CardListView) findViewById(R.id.my_card_list_view);
       if (listView!=null)
           listView.setAdapter(mCardArrayAdapter);

       //I think here is the last point.
       //After this point there will be no significant process.
       long stopTime = System.currentTimeMillis();
       Log.d("modelflow", "card create takes: " + (stopTime - startTime) + "ms");

}

And here is the output in logcat: * .... D/modelflow: card create takes: 15ms * .... D/modelflow: onResume ends in: 15ms

New activity is shown 1-2 seconds after seeing these logs.