CardGridStaggeredView卡消失

时间:2015-07-16 14:21:03

标签: android android-layout android-recyclerview cardslib staggered-gridview

我正在使用CardGridStaggeredView在我的Activity中显示卡片,问题是当我使用2列或更多列时,卡片会在滚动时消失。

这是我的xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/account_grey_300"
    tools:context=".FavouritesActivity">

    <include android:id="@+id/tool_bar"
        layout="@layout/tool_bar">
    </include>

    <RelativeLayout xmlns:card="http://schemas.android.com/apk/res-auto"
        android:id="@+id/activity_favourites"
        android:layout_width="match_parent" android:layout_height="match_parent"
        android:radius="10dp">
        <!--android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin"-->
        <!--android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin"-->
        <ProgressBar android:id="@+id/fav_progress" style="?android:attr/progressBarStyleLarge"
            android:layout_width="match_parent" android:layout_height="wrap_content"
            android:visibility="gone"
            android:layout_marginBottom="8dp" android:layout_marginTop="16dp"/>

        <it.gmariotti.cardslib.library.extra.staggeredgrid.view.CardGridStaggeredView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            card:item_margin="2dp"
            card:column_count_portrait="1"
            card:column_count_landscape="2"
            android:id="@+id/carddemo_extras_grid_stag"
            android:radius="10dp"
            card:cardCornerRadius="10dp"/>

    </RelativeLayout>

</LinearLayout>

Favourites活动:

Toolbar toolbar; Runnable viewTours; ProgressDialog m_ProgressDialog = null;
ArrayList<Tour> m_tours = null;
ArrayList<Card> cards = new ArrayList<>();
CardGridStaggeredArrayAdapter mCardArrayAdapter;
CardGridStaggeredView mGridView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_favourites);
    toolbar = (Toolbar) findViewById(R.id.tool_bar); // Attaching the layout to the toolbar object

    mProgressView = findViewById(R.id.fav_progress);

    setSupportActionBar(toolbar);
    // Show the Up button in the action bar.
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);  //pulsante drawer
    getSupportActionBar().setHomeButtonEnabled(true);       //pulsante dietro
    getSupportActionBar().setTitle("I tuoi preferiti");

    SharedPreferences usrData = getSharedPreferences(usr_loggedin, MODE_PRIVATE);
    SharedPreferences.Editor editor = usrData.edit();
    boolean noTours = true;
    if(usrData.contains("favourites")){
        noTours = false;
    }
    Intent intent = getIntent();
    bundle = intent.getExtras();
    if(noTours){
        showAlert("no_tours");
    }else{
        String jsn = usrData.getString("favourites","");
        Tours tours = (new Gson().fromJson(jsn, Tours.class));
        m_ProgressDialog = ProgressDialog.show(this,"Attendere...","Reperimento dati in corso...", true);
        callBackMethod(tours);
    }
}
public void callBackMethod(final Tours tours){
    if(tours!=null){
        viewTours = new Runnable(){//System.out.println("I've been called back");
            @Override
            public void run() { getOrders(tours); }
        };
        Thread thread =  new Thread(null, viewTours, "MagentoBackground"); thread.start();
    }else{ // missing connection
        m_ProgressDialog.dismiss();
        NavUtils.navigateUpTo(this, new Intent(this, MainActivity.class));
    }
}

public void getOrders(Tours tours){
    try{
        m_tours = new ArrayList<>();
        m_tours.addAll(tours.getTours());

        if(m_tours != null && m_tours.size() > 0){
            for (int i = 0; i < m_tours.size(); i++) {
                DeviceCardList card = new DeviceCardList(FavouritesActivity.this,
                        R.layout.cardslib_inner_layout, m_tours.get(i), mBitmap);
                cards.add(card);
            }
        }
    }catch (Exception e) {  Log.e("BACKGROUND_PROC", e.getMessage().toString());}
    //add tours to the adapter
    runOnUiThread(returnRes);
}
private Runnable returnRes = new Runnable() {
    @Override
    public void run() {
        mCardArrayAdapter = new CardGridStaggeredArrayAdapter(FavouritesActivity.this, cards);
        mGridView = (CardGridStaggeredView)
                FavouritesActivity.this.findViewById(R.id.carddemo_extras_grid_stag);
        if (mGridView != null) {
            mGridView.setAdapter(mCardArrayAdapter);
        } // mCardArrayAdapter.notifyDataSetChanged();
        //TODO: java.lang.IllegalArgumentException --- handle forced cancellation of tours download
        //TODO: android.view.WindowManager$BadTokenException
        try{
            m_ProgressDialog.dismiss();
        }catch(IllegalArgumentException e){
            String s = e.getMessage();
            Toast.makeText(FavouritesActivity.this,s,Toast.LENGTH_LONG).show();
        }catch(WindowManager.BadTokenException e){
            String s = e.getMessage();Toast.makeText(FavouritesActivity.this,s,Toast.LENGTH_LONG).show();
        } //m_adapter.notifyDataSetChanged();
    }
};

0 个答案:

没有答案