2014-09-23 108 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); 
     } 
    } 
}