簡體   English   中英

Cardslib片段撤消滑動

[英]Cardslib fragment undo swipe

我正在嘗試使用Cardslib庫以及可以刷卡和撤消的卡列表。 我的問題是,撤消欄總是顯示,單擊撤消不會執行任何操作。 通過調試,我意識到我的問題是CardArrayAdapter需要一個上下文,該上下文將在嘗試檢索撤消工具欄的LinearLayout時使用。 但是,當我通過主活動作為上下文時,它沒有找到撤消欄。

我的代碼如下:

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

我得到的結果是我的卡片列表(很好),當我還沒有刷出任何卡片時(這是不好的),撤消欄位於屏幕中間。 感謝您的幫助,我確實整日都在努力尋找解決方案。

好吧,我想我應該通過再次閱讀我的問題來弄清楚。

我需要將一個活動傳遞給CardAdapter,但要在onCreateView() 片段生命周期中,我應該假定該活動存在於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 );
        }
    }
}

在這種情況下,您必須在onActivityCreated()方法中創建ArrayAdapter。

請注意執行以下操作:

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

這樣,您就可以使用本地卡。 當您調用添加,刪除...時,適配器將使用其自己的列表。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM