简体   繁体   English

Cardslib片段撤消滑动

[英]Cardslib fragment undo swipe

I am trying to use the Cardslib library with a list of cards that I can swipe out and undo. 我正在尝试使用Cardslib库以及可以刷卡和撤消的卡列表。 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. 通过调试,我意识到我的问题是CardArrayAdapter需要一个上下文,该上下文将在尝试检索撤消工具栏的LinearLayout时使用。 But when I pass the main Activity as Context it doesn't find the undobar. 但是,当我通过主活动作为上下文时,它没有找到撤消栏。

My code is as follow: 我的代码如下:

MainActivity.java: 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: 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: 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: 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: 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. 感谢您的帮助,我确实整日都在努力寻找解决方案。

Well I guess I should have figured it out by reading again my question. 好吧,我想我应该通过再次阅读我的问题来弄清楚。

I need to pass an activity to the CardAdapter but I do that in onCreateView() . 我需要将一个活动传递给CardAdapter,但要在onCreateView() When by the fragment lifecycle I should assume the activity to exist in onActivityCreated() . 片段生命周期中,我应该假定该活动存在于onActivityCreated()

So for those interested in a solution to this question here is a correction for the file BudgetCardFragment.java: 因此,对于那些对此问题的解决方案感兴趣的人,这里是对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 );
        }
    }
}

In this case you have to create the ArrayAdapter in the onActivityCreated() method. 在这种情况下,您必须在onActivityCreated()方法中创建ArrayAdapter。

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. 当您调用添加,删除...时,适配器将使用其自己的列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM