Android 选项卡单击滚动到顶部
Android tab click scroll to top
在我的应用中,选项卡是使用 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
的 activity
@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);
}
}
}
只是一个附加组件,我没有像 Jyothish 的代码那样编写方法 mAdapter.getFragment()
,所以我使用 adapter.getItem()
:
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition(), true);
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {//scroll to top
try {
Fragment f = adapter.getItem(tab.getPosition());
if (f != null) {
View fragmentView = f.getView();
RecyclerView mRecyclerView = (RecyclerView) fragmentView.findViewById(R.id.recyclerview);//mine one is RecyclerView
if (mRecyclerView != null)
mRecyclerView.smoothScrollToPosition(0);
}
} catch (NullPointerException npe) {
}
}
});
在我的应用中,选项卡是使用 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
的 activity@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);
}
}
}
只是一个附加组件,我没有像 Jyothish 的代码那样编写方法 mAdapter.getFragment()
,所以我使用 adapter.getItem()
:
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition(), true);
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {//scroll to top
try {
Fragment f = adapter.getItem(tab.getPosition());
if (f != null) {
View fragmentView = f.getView();
RecyclerView mRecyclerView = (RecyclerView) fragmentView.findViewById(R.id.recyclerview);//mine one is RecyclerView
if (mRecyclerView != null)
mRecyclerView.smoothScrollToPosition(0);
}
} catch (NullPointerException npe) {
}
}
});