android – ListView与Viewpager作为TabPageIndicator内的标题

所以我创建了一个片段活动,其中包含带有TabPageIndicator的Viewpager从片段滑动到另一个片段,并且在每个片段中我创建了一个ListView,其中包含一个ViewPager作为Header视图,问题是viewPager不刷卡即使我取消主ViewPager swipping

我用ActionBar标签尝试了同样的事情,但它也没有用

 

这是我的片段活动

public class SampleTabsStyled extends FragmentActivity {

private static final String[] CONTENT = new String[] { "Recent", "Artists",
        "Albums", "Songs", "Playlists", "Genres" };

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.simple_tabs);

    FragmentPagerAdapter adapter = new GoogleMusicAdapter(
            getSupportFragmentManager());

    final ViewPager pager = (ViewPager) findViewById(R.id.pager);
    pager.setAdapter(adapter);

    pager.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub

            switch (event.getAction()) {
            case MotionEvent.ACTION_MOVE:
                pager.requestDisallowInterceptTouchEvent(true);
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                pager.requestDisallowInterceptTouchEvent(false);
                break;

            }
            return true;
        }

    });

    TabPageIndicator indicator = (TabPageIndicator) findViewById(R.id.indicator);
    indicator.setViewPager(pager);

}

class GoogleMusicAdapter extends FragmentPagerAdapter {
    public GoogleMusicAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public ListFragment getItem(int position) {
        // return TestFragment.newInstance(CONTENT[position %
        // CONTENT.length]);
        return new SecondTestFragment();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return CONTENT[position % CONTENT.length].toUpperCase();
    }

    @Override
    public int getCount() {
        return CONTENT.length;
    }
}

}

这是我的ListFragment

public class SecondTestFragment extends ListFragment {

String[] countries = new String[] { "India", "Pakistan", "Sri Lanka",
        "China", "Bangladesh", "Nepal", "Afghanistan", "North Korea",
        "South Korea", "Japan" };

View mheaderView;
ViewPager pager;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    /** Creating an array adapter to store the list of countries **/

    View parent = inflater.inflate(R.layout.fragment_list, null);

    // mheaderView = inflater.inflate(R.layout.item_list, null);
    mheaderView = inflater.inflate(R.layout.featured_pager, null);
    pager = (ViewPager) mheaderView.findViewById(R.id.featuredPaper);
    pagerAdapter pa = new pagerAdapter(getActivity());
    pager.setAdapter(pa);
    pager.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            Log.d("Motion Event", event.toString());
            return false;
        }
    });

    CirclePageIndicator indicator = (CirclePageIndicator) mheaderView.findViewById(R.id.featured_indicator);
    indicator.setViewPager(pager);




    return parent;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);

    if (mheaderView != null) {
        this.getListView().addHeaderView(mheaderView);
        Log.d("pager", "not null");
    } else {
        Log.d("pager", "null");
    }

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
            android.R.layout.simple_list_item_1, countries);
    setListAdapter(adapter);

    getListView().setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            getListView().getParent().requestDisallowInterceptTouchEvent(true);
            return false;
        }
    });

    ((PullToRefreshListView) getListView()).setOnRefreshListener(new OnRefreshListener() {
        @Override
        public void onRefresh() {
            // Do work to refresh the list here.

        }
    });


}

}

最佳答案
在父视图寻呼机中覆盖canScroll方法.

@Override
protected boolean canScroll(View v, boolean checkV, int dx, int x, int y) {
   if(v != this && v instanceof ViewPager) {
       return true;
   }
   return super.canScroll(v, checkV, dx, x, y);
}

检查这个答案

ViewPager inside ViewPager

转载注明原文:android – ListView与Viewpager作为TabPageIndicator内的标题 - 代码日志