在Fragment中将headerView设置为ListView是正确的方法吗?
我有一些片段(请参见下面的代码)。在此片段中,我填充ListView。
但是我不知道在哪里可以调用addHeaderView。
有谁能够帮助我?
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);
}
}
最佳答案
您可以在以下之间调用addHeaderView()
:
ListView lv = getListView();
和:
setListAdapter(new ListOfFeedsAdapter(mContext, layoutForList, items, mActivity));
现在,我还没有尝试在
View
上使用标头ListFragment
,因此虽然应该可以使用,但由于某种原因,它有可能无法使用。关于android - 如何在ListFragment中调用addHeaderView?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6791823/