Search code examples
androidandroid-fragmentsswipeundoandroid-cards

Cardslib fragment undo swipe


I am trying to use the Cardslib library with a list of cards that I can swipe out and undo. My issue is that the undo bar always shows and clicking on undo doesn't do anything. By debugging I realized that my problem is that the CardArrayAdapter requires a Context that will be used when trying to retrieve the LinearLayout of the undobar. But when I pass the main Activity as Context it doesn't find the undobar.

My code is as follow:

MainActivity.java:

    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate( savedInstanceState );
      setContentView( R.layout.main_layout );

      BudgetCardFragment budgetCardFragment = (BudgetCardFragment)getSupportFragmentManager().
            findFragmentById( R.id.fragment_budget_cards );
    }

main_layout.xml:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment_budget_cards"
android:name="com.test.view.fragment.BudgetCardFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.test.Main"
tools:layout="@android:layout/list_content" />

BudgetCardFragment.java:

    public class BudgetCardFragment extends Fragment {
        public BudgetCardFragment(){}

        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View rootView = inflater.inflate( R.layout.fragment_budget_card, container, true );

            CardListView listView = (CardListView)rootView.findViewById(R.id.myList);
            List<Card> cards= new ArrayList<Card>();
            for (int i = 0; i < 3; i++) {
                BudgetCard budgetCard = new BudgetCard( getActivity(), mListCategory.get(i).getName() );
                cards.add( budgetCard );
            }
            BudgetCardAdapter budgetCardAdapter = new BudgetCardAdapter( getActivity(), cards );
            if ( listView != null ) {
                listView.setAdapter( budgetCardAdapter );
            }

            return rootView;
        }
    }

fragment_budget_card.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 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"
         tools:context="com.test.Main">

    <it.gmariotti.cardslib.library.view.CardListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/myList"/>

    <!-- Include undo message layout -->
    <include layout="@layout/list_card_undo_message"/>

</RelativeLayout>

BudgetCardAdapter.java:

public class BudgetCardAdapter extends CardArrayAdapter {
    private Context mContext;
    private List<Card> cards;
    public BudgetCardAdapter( Context context, List<Card> cards ) {
        super( context, cards );

        this.mContext = context;
        this.cards = cards;

        //Enable undo controller
        setEnableUndo(true);
    }
    @Override
public int getCount() {
    return cards.size();
}

@Override
public Card getItem(int position) {
    return cards.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}
}

The result I obtain is my list of cards (that's good) with the undo bar right in the middle of the screen when I didn't swipe out any card yet (that's not good). Thank you for your help, I've really been struggling all day to try to find a solution to this.


Solution

  • In this case you have to create the ArrayAdapter in the onActivityCreated() method.

    Pay attention to do this:

    @Override
    public Card getItem(int position) {
        return cards.get(position);
    }
    

    In this way you are working with your local cards. When you call add, remove... the adapter works with its own lists.