在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);
}
}
答案 0 :(得分:1)
您可以在addHeaderView()
之间致电:
ListView lv = getListView();
和
setListAdapter(new ListOfFeedsAdapter(mContext, layoutForList, items, mActivity));
现在,我还没有在View
上尝试使用标题ListFragment
,所以虽然它应该有用,但由于某种原因,它有可能没有。