0

我在片段中有这个 cardlibs 列表视图。

我有一些通过主要活动传入的动态数据。

当我第一次使用列表视图调用片段来填充卡片时,一切都很酷。表明。我设置了检查片段是否已激活的功能,以及它是否只是调用片段内的一个方法,该方法应该将另一张卡添加到列表中。

主持人活动

    public void pushToFeedFromActivity(JSONObject msg){
    FragmentManager fragmentManager = getFragmentManager();
    if (!myFeedActive) {
        myFeedActive = true;
        Bundle argsScene = new Bundle();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.feedInnerParent, FragFeed.newInstance(msg), "myfeed");
        fragmentTransaction.commit();

    }else {
        FragFeed fragFeed = (FragFeed) getFragmentManager().findFragmentByTag("myfeed");
        fragFeed.pushToFeed(msg);
    }
};

   @OnClick(R.id.PushToFeedButton)
    public void presstoPushToFeed(View v) {
    pushToFeedFromActivity(json)
    ......

在 onActivity 中,我从包中获取 arg/解析 json 并将其传递给将卡片添加到列表中的方法,就像我说这些指令在第一次添加片段时有效,但在随后的时间中无效,当只是该方法直接从活动中调用。

分段

    public void onActivityCreated(Bundle savedInstanceState) {
    ....
    pushToFeed(jsonFromString);
    .....
    }

    public void pushToFeed(JSONObject json) {
    Log.v(TAG + "pushtofeed", json.toString());
    if (json != null) {
        try {
            body = jsonFromString.get("stdin").toString();
            lo = jsonFromString.get("lo").toString();
            channel = jsonFromString.get("channel").toString();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    {
    ArrayList<Card> cards = new ArrayList<Card>();
    {
        SceneCard card = new SceneCard(getActivity());
        //CardHeader header = new CardHeader(getActivity());
        card.setClickable(true);
        //card.addCardHeader(header);
        card.setSceneCardloqoo(loqoo);
        card.setSceneCardmsgbody(body);
        card.setSceneCardchannel(channel);
        card.getSceneCardtime();
        cards.add(card);

    }


    CardArrayAdapter mCardArrayAdapter = new CardArrayAdapter(getActivity(), cards);
    CardListView listView = (CardListView) getActivity().findViewById(R.id.fragFeedList);
    if (listView != null) {
        listView.setVerticalScrollBarEnabled(false);
        listView.setAdapter(mCardArrayAdapter);
        Log.v(TAG + "pushtofeed - listview", json.toString());

    }
    mCardArrayAdapter.notifyDataSetChanged();

}

我已经在片段方法中添加了 Log 语句,所以我确定调用正在通过,列表实现中只是有些东西混淆了。是什么赋予了?

4

1 回答 1

1

来自lib的创建者本人,通过github问题来解决......

https://github.com/gabrielemariotti/cardslib/issues/313

 @gabrielemariotti commented:

  "In your code, you are using listView.setAdapter(mCardArrayAdapter); each time.
   This code will remove all views from the list view.

   In your case if you want to add new items to an existing adapter,
   you have to retrieve your adapter, add the new items,
   calls the notifyDataSetChanged, without using listView.setAdapter again."
于 2014-10-01T13:58:23.313 回答