开发者

How to call addHeaderView in ListFragment?

开发者 https://www.devze.com 2023-03-22 19:17 出处:网络
Is it a correct way to set headerView to ListView in Fragment? I have some fragment(see below code). In this fragment i populate ListView.

Is it a correct way to set headerView to ListView in Fragment? I have some fragment(see below code). In this fragment i populate ListView. But I don't know where I can call addHeaderView. Can anybody help me?

public class FeedsListForViewFragment extends ListFragment {

    private Context mContext;
    List<FeedItem> items;
    private int layoutForList = R.layout.list_feeds_simple;
    private ActiveRecordBase _db;
    private Activity mActivity;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity = getActivity();
        _db = ((JtjApplication)getActivity().getApplication()).getDatabase();
        try {
            _db.open();
        } catch (ActiveRecordException e) {
            e.printStackTrace();
        }          

    }

    public void setItemsToList(List<FeedItem> items, int curPosition) {
        mContext = getActivity().getApplicationContext();
        this.items = items;
        ListView lv = getListView();
        setListAdapter(new ListOfFeedsAdapter(mContext, layoutForList, items, mActivity));
        lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        lv.setCacheColorHint(Color.WHITE);

        setPosition(curPosition);
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        if (!(((FeedsActivity) getActivity()).getCurrentPosition() == position)) {
            updateUnreadItemsInDb(position);

            updateLinksList(position, items.get(position));
            ((FeedsActivity) getActivity()).setCurrentPosition(position);
        }
    }

    @SuppressWarnings("unchecked")
    private void updateUnreadItemsInDb(int position) {
        FeedItem feed = items.get(position);
        if (feed.isRead != true) {
            List<FeedItem> feedsForUpdate = null;
            List<FeedType> lft = null;
            try {
                feedsForUpdate = _db.find(FeedItem.class, false, "GUID=?", new String[] { String.valueOf(feed.guid) }, null, null, "PUBLICATIONDATE DESC", null);
                lft = _db.findAll(FeedType.class);
            } catch (ActiveRecordException e1) {
                e1.printStackTrace();
            }
            for (FeedItem curFeed:feedsForUpdate) {
                curFeed.isRead = true;
                try {
                    curFeed.save();
                    FeedType ft = lft.get(Integer.parseInt(curFeed.channel_id) - 1);
                    ft.unread_count = ft.unread_count - 1;
                    ft.update();
                } catch (ActiveRecordException e) {
                    e.printStackTrace();
                }
            }
            items.get(position).isRead = true;
            ((ArrayAdapter<FeedItem>) getListView().getAdapter()).notifyDataSetChanged();
        }
    }

    private void setPosition(int position) {
  开发者_运维技巧      ListView lv = getListView();
        lv.setSelection(position);
        lv.setItemChecked(position, true);       
        try{
            updateLinksList(position, items.get(position));
        } catch (IndexOutOfBoundsException e) {
            e.printStackTrace();
        }
    }

    private void updateLinksList(int position, FeedItem feedItem) {
        FeedsViewFragment viewFragment = (FeedsViewFragment) getFragmentManager().findFragmentById(R.id.feed_view_fragment);
        viewFragment.setViewItem(feedItem);
    }
}


You would call addHeaderView() between:

ListView lv = getListView();

and:

setListAdapter(new ListOfFeedsAdapter(mContext, layoutForList, items, mActivity));

Now, I have not tried a header View on a ListFragment, so while it should work, there is a chance that for some reason it does not.

0

精彩评论

暂无评论...
验证码 换一张
取 消