Go to the "Android" tab.

in my app tabs are implemented with 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;
}

      

and one of my fragments looks like

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

      

when clicking on a tab item, I want to scroll the list items to the top. How to implement this?

+3


source to share


2 answers


I found a solution to the problem by adding this code to an activity that implements 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);
        }
    }

}

      

+8


source


Just an add-on, I didn't write a method mAdapter.getFragment()

like in Jyothish code, so I used instead 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) {
                            }
                        }
                    });

      

+1


source







All Articles