在我的应用程序中,选项卡是使用FragmentPageRadapter实现的
public class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(android.support.v4.app.FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new OutboxFragment();
case 1:
return new ComposeFragment();
case 2:
return new NotificationFragment();
case 3:
return new MoreFragment();
}
return null;
}
@Override
public int getCount() {
return 4;
}
public Fragment getFragment(ViewPager container, int position, FragmentManager fm) {
String name = makeFragmentName(container.getId(), position);
return fm.findFragmentByTag(name);
}
private String makeFragmentName(int viewId, int index) {
return "android:switcher:" + viewId + ":" + index;
}
我的一块碎片看起来像
public class OutboxFragment extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor> {
public static OutboxListAdapter mAdapter;
public ListView listItem;
View view;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mAdapter = new OutboxListAdapter(getActivity(),R.layout.outbox_list_item, null, 0);
setListAdapter(mAdapter);
getLoaderManager().initLoader(0, null, this);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
mAdapter.notifyDataSetChanged();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_outbox, container, false);
listItem = (ListView) view.findViewById(android.R.id.list);
return view;
}
static String[] projection = {
"_id",
"messageId",
"messageTopic",
"messageTopicEnc",
"messageType",
"requestor",
"usercode",
"user",
"randomcode",
"hashcode",
"strippedHashcode",
"solicitationId",
"blocked",
"blockedAt",
"closed",
"closedAt",
"createdAt",
"receivedAt",
"toAddresses",
"liked",
"disliked",
"likecount",
"dislikecount",
"poked",
"rkey",
"code",
"url",
"ref",
"icon_idx",
"icon_color_idx",
"action",
"visibility",
"scope",
"polltype",
"option1",
"option2",
"option3",
"option4",
"option5",
"answer",
"reply_at",
"response_count",
"option1_count",
"option2_count",
"option3_count",
"option4_count",
"option5_count",
"options_count",
"answered",
"correct_answer",
"answerEnc",
"askfeedback_email_counter"
};
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
String select = " user LIKE ? AND ( usercode ="+0 +" OR usercode ="+1+" )";
return new CursorLoader(getActivity(), Uri.parse("content://"+DBContentProvider.MESSAGE_THREAD_URI),
projection, select, new String[] {((SwipeTabActivity) getActivity()).userId},
"createdAt DESC");
}
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
mAdapter.swapCursor(data);
mAdapter.notifyDataSetChanged();
}
public void onLoaderReset(Loader<Cursor> loader) {
mAdapter.swapCursor(null);
}
public void refresh(){
// mAdapter = new OutboxListAdapter(getActivity(),R.layout.outbox_list_item, null, 0);
// mAdapter.notifyDataSetChanged();
}
单击选项卡项时,我要将列表项滚动到顶部。我该如何实现?
最佳答案
我找到了问题的解决方案将此代码添加到实现actionbar.tablistener的活动中
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft)
{
FragmentManager fm = getSupportFragmentManager();
Fragment f = mAdapter.getFragment(viewPager, tab.getPosition(), fm);
if(f != null){
if(tab.getPosition() == 0) {
OutboxFragment fragment = (OutboxFragment) f;
fragment.getListView().smoothScrollToPosition(0);
}
}
}