gpt4 book ai didi

android - Cardslib fragment 撤消滑动

转载 作者:行者123 更新时间:2023-11-30 02:39:19 26 4
gpt4 key购买 nike

我正在尝试将 Cardslib 库与我可以刷出和撤消的卡片列表一起使用。我的问题是撤消栏总是显示并且单击撤消不会执行任何操作。通过调试我意识到我的问题是 CardArrayAdapter 需要一个上下文,在尝试检索 undobar 的 LinearLayout 时将使用该上下文。但是当我将主 Activity 作为 Context 传递时,它找不到撤消栏。

我的代码如下:

主要 Activity .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;
}
}

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

最佳答案

好吧,我想我应该通过再次阅读我的问题来弄明白。

我需要将一个 Activity 传递给 CardAdapter,但我是在 onCreateView() 中完成的。当fragment lifecycle我应该假定该 Activity 存在于 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 );
}
}
}

关于android - Cardslib fragment 撤消滑动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26002868/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com