gpt4 book ai didi

android - ListView OnScrollListener 不会在 ViewPager 中被调用

转载 作者:太空宇宙 更新时间:2023-11-03 12:58:33 26 4
gpt4 key购买 nike

我有一个 FragmentPagerAdapter 创建它的子页面(它们是 ListFragments),如下所示:

@Override
public Fragment getItem(int position) {
return FragmentChildPage.newInstance();
}

我给 ViewPager 的 OffscreenPageLimit 为 1。子 ListFragments 有一个自定义的 ScrollListener

我遇到的问题是自定义滚动监听器仅适用于当 ViewPager 滑过其 OffscreenPageLimit 时创建的子 ListFragments,即子页面 > 2.

更令人困惑的是,如果我滚动几页然后返回到第一页,滚动监听器现在可以工作了。

这是怎么回事? ViewPager 似乎干扰了一些子页面 OnScrollListener

谢谢

最佳答案

试试这个。适合我!

public class ActivityTest extends SherlockFragmentActivity {

private ViewPager mViewPager;

private TestAdapter mAdapter;

@Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);

mViewPager = new ViewPager(this);
mViewPager.setId(android.R.id.button1);
setContentView(mViewPager);

mAdapter = new TestAdapter(getSupportFragmentManager());
mViewPager.setAdapter(mAdapter);
}

private class TestAdapter extends FragmentPagerAdapter {

public TestAdapter(FragmentManager fm) {
super(fm);
}

@Override
public Fragment getItem(int position) {
return TestListFragment.newInstance(position);
}

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

}

private static class TestListFragment extends SherlockListFragment {

private static final String TAG = TestListFragment.class.getSimpleName();

private String[] mItems = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9","0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };

private int mPos;

@SuppressLint("ValidFragment")
public static TestListFragment newInstance(int pos) {
TestListFragment frag = new TestListFragment();
Bundle b = new Bundle();
b.putInt("pos", pos);
frag.setArguments(b);
return frag;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

mPos = getArguments().getInt("pos");
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

setListAdapter(new ArrayAdapter<String>(getSherlockActivity(), android.R.layout.simple_list_item_1, mItems));

getListView().setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {

}

@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
Log.d(TAG, "onScroll() - Fragment: " + mPos);
}
});
}
}

}

关于android - ListView OnScrollListener 不会在 ViewPager 中被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15255604/

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