Cardslib片段撤消滑动

时间:2014-09-23 19:09:52

标签: android android-fragments swipe undo android-cards

我正在尝试使用Cardslib库以及可以滑出和撤消的卡列表。 我的问题是撤消栏始终显示并单击撤消不执行任何操作。通过调试我意识到我的问题是CardArrayAdapter需要在尝试检索undobar的LinearLayout时使用的Context。但是当我将主Activity作为Context传递时,它找不到undobar。

我的代码如下:

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;
}
}

我获得的结果是我的卡片列表(这很好),当我没有刷出任何卡片时(这不好),屏幕中间有一个撤消栏。 谢谢你的帮助,我一整天都在努力寻找解决方案。

2 个答案:

答案 0 :(得分:1)

嗯,我想我应该通过再次阅读我的问题来解决这个问题。

我需要将活动传递给CardAdapter,但我会在onCreateView()中执行此操作。在fragment lifecycle之前,我应该假设活动存在于onActivityCreated()

因此,对于那些对这个问题的解决方案感兴趣的人来说,这是对BudgetCardFragment.java文件的更正:

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

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate( R.layout.fragment_budget_card, container, false );
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        CardListView listView = (CardListView)getActivity().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 );
        }
    }
}

答案 1 :(得分:0)

在这种情况下,您必须使用onActivityCreated()方法创建ArrayAdapter。

注意这样做:

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

通过这种方式,您正在使用本地卡。 当你调用add时,删除...适配器可以使用自己的列表。