2014-09-23 340 views
1

我正在嘗試使用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; 
} 
} 

我得到的結果是我的卡列表(這是很好)與撤消欄右鍵在屏幕中間的時候我沒有刷出什麼卡尚未(這不好)。 感謝您的幫助,我一整天都在努力尋找解決方案。

回答

0

在這種情況下,你必須創建在onActivityCreated()方法一個ArrayAdapter。

注意做到這一點:

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

這樣你與你的本地卡的工作。 當您調用add時,請刪除...適配器使用其自己的列表。

+0

謝謝加布裏埃爾。你的意思是將代碼片段添加到適配器中嗎?我已經在BudgetCardAdapter中使用它了。還是你的意思是別的? – Gregriggins36 2014-09-25 13:31:46

+0

我的意思是它可能是危險的 – 2014-09-25 13:36:51

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