我正在使用PagerSlidingTabs。我有两个片段,每个片段都有一个listview。我添加了TabListener,因为当用户重新选择相同的选项卡时,我想在listview中找到第一项。这仅适用于第二个片段(选项卡)。问题是第二个listview的smoothScrollToPosition(0)
方法有效,但第一个listview的smoothScrollToPosition(0)
方法(所有方法)都不起作用。第一个listview在屏幕上正确显示,但它的id似乎是第二个listview的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;
}
}}
片段1:
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和Fragment2只有listview(id:listview1,id:listview2)。 HomeActivity有PagerSlidingTabs视图和viewpager。
我该如何解决这个问题?
答案 0 :(得分:0)
从第二个片段设置侦听器时,将从第一个片段替换侦听器。这就是为什么所有的调用似乎都要进入第二个标签:它的监听器是最后一个添加的,所以它接收所有的回调。一个可能的解决方案是声明一个接口,让我们称之为OnTabReselctedListener,它有一个方法onTabReselected(into position)
。让您的活动维护这些接口的列表,让您的片段实现该接口,并将这些片段注册为活动中的侦听器。当活动收到重选事件时,它可以将其传递给其子片段;这些碎片检查位置是否与他们自己的位置匹配,如果他们这样做则滚动到顶部。我希望这是有道理的;我是用手机写的,所以我不能轻易编写示例代码。如果你想让我写一些,请告诉我!