gpt4 book ai didi

Android标签点击滚动到顶部

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:42:59 25 4
gpt4 key购买 nike

在我的应用中,选项卡是使用 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;
}

我的一个 fragment 看起来像

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);
}
}

}

关于Android标签点击滚动到顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29788575/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com