Question

I am using PagerSlidingTabs. I have two fragment and each fragment has one listview. I added TabListener, because i want to go first item in listview when user reselect the same tab. This is working only on the second fragment(tab). Problem is second listview's smoothScrollToPosition(0) method working but first listview's smoothScrollToPosition(0) method(all methods) not working. First listview is showing properly on screen but it's id seems to second listview's id.

PagerAdapter:

public class PagerAdapter extends FragmentPagerAdapter {
private Context mContext;
private int mPageCount;

public PagerAdapter(Context context, FragmentManager fm, int pageCount) {
    super(fm);
    this.mContext = context;
    this.mPageCount = pageCount;
}

@Override
public int getCount() {
    return mPageCount;
}

@Override
public Fragment getItem(int position) {
    switch (position) {
        case 0:
            return Fragment1.newInstance(position);
        case 1:
            return Fragment2.newInstance(position);

        default:
            return null;
    }

}}

Fragment1:

public class Fragment1 extends Fragment {

private static final String ARG_POSITION = "position";
private ListView mListView1;
private ListAdapter mAdapter;

public static Fragment1 newInstance(int position) {
    Fragment1 f = new Fragment1();
    Bundle b = new Bundle();
    b.putInt(ARG_POSITION, position);
    f.setArguments(b);
    return f;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    if (mRootView == null) {
        mRootView = inflater.inflate(R.layout.fragment1, container, false);
        mListView1 = (ListView) mRootView.findViewById(R.id.listview1);
        .
        .
        if (mAdapter == null) {
            mAdapter = new ListAdapter(getActivity(), mListView1, R.layout.list_item, mDatas);
            mListView1.setAdapter(mAdapter);
        } else {
            mAdapter.notifyDataSetChanged();
        }
        .
        .
        ((HomeActivity) getActivity()).getPagerSlidingTabStrip().setOnTabListener(new TabListener() {
            @Override
            public void onTabReselected(View tab, int postion) {
                mListView1.smoothScrollToPosition(0);
                //mListView1's id seems to mListView2's id
                //when reselect first tab, application acting like i reselect second tab.
            }
        });
    } else {
        ((ViewGroup) mRootView.getParent()).removeView(mRootView);
    }
    return mRootView;
}}

Fragment2:

public class Fragment2 extends Fragment {

private static final String ARG_POSITION = "position";
private ListView mListView2;
private ListAdapter mAdapter;

public static Fragment2 newInstance(int position) {
    Fragment2 f = new Fragment2();
    Bundle b = new Bundle();
    b.putInt(ARG_POSITION, position);
    f.setArguments(b);
    return f;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    if (mRootView == null) {
        mRootView = inflater.inflate(R.layout.fragment2, container, false);
        mListView2 = (ListView) mRootView.findViewById(R.id.listview2);
        .
        .
        if (mAdapter == null) {
            mAdapter = new ListAdapter(getActivity(), mListView2, R.layout.list_item, mDatas);
            mListView2.setAdapter(mAdapter);
        } else {
            mAdapter.notifyDataSetChanged();
        }
        .
        .
        ((HomeActivity) getActivity()).getPagerSlidingTabStrip().setOnTabListener(new TabListener() {
            @Override
            public void onTabReselected(View tab, int postion) {
                mListView2.smoothScrollToPosition(0); //this line is working fine
            }
        });
    } else {
        ((ViewGroup) mRootView.getParent()).removeView(mRootView);
    }
    return mRootView;
}}

Fragment1 and Fragment2 have only listview(id:listview1, id:listview2). HomeActivity have PagerSlidingTabs view and viewpager.

How can i fix this problem?

Was it helpful?

Solution

When you set the listener from the second fragment, that replaces the listener from the first. That's why all the calls seem to be going to the second tab: its listener was the last one added, so it's receiving all the callbacks. One potential solution is to declare an interface, let's call it OnTabReselctedListener, that has a method onTabReselected(into position). Have your activity maintain a list of those interfaces, have your fragments implement that interface, and register those fragments as listeners in the activity. When the activity receives a reselection event, it can pass that to its children fragments; those fragments check if the position matches their own position and scroll to the top if they do. I hope that made sense; I'm writing this from my phone so I can't easily wrote sample code. If you want me to write some, let me know!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top