我正在嘗試使用Cardlib庫以及可以刷出並撤消的卡片列表。 我的問題是撤消欄總是顯示並點擊撤消不會執行任何操作。通過調試,我意識到我的問題是CardArrayAdapter需要一個在嘗試檢索undobar的LinearLayout時將使用的上下文。但是當我將主Activity作爲上下文傳遞時,它找不到unsobar。Cardslib片段撤消刷卡
我的代碼如下:
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;
}
}
我得到的結果是我的卡列表(這是很好)與撤消欄右鍵在屏幕中間的時候我沒有刷出什麼卡尚未(這不好)。 感謝您的幫助,我一整天都在努力尋找解決方案。
謝謝加布裏埃爾。你的意思是將代碼片段添加到適配器中嗎?我已經在BudgetCardAdapter中使用它了。還是你的意思是別的? – Gregriggins36 2014-09-25 13:31:46
我的意思是它可能是危險的 – 2014-09-25 13:36:51