TabHost Viewpager listview不会滚动

时间:2016-06-02 14:45:25

标签: android listview android-viewpager

Tabhost + viewpager水平滚动。如果片段是空的或包含textview,它可以工作,但如果片段包含列表视图,片段它不会滚动水平!!

如果我尝试从textview滚动它或者片段是空的那就可以了,但是从listview中可以看出,注意textview的宽度和高度是wrap_content而listview中的那些是match_parent

frgament 2(它包含tabhost的片段)

fragment.xml之

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/main_content1"/>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="RESEAU"
    android:layout_marginTop="20dp"
    android:layout_marginLeft="20dp"/>
<View
    android:layout_width="wrap_content"
    android:layout_height="1dp"
    android:layout_marginTop="10dp"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:background="#000000" />
<HorizontalScrollView
    android:id="@+id/hScrollView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:fillViewport="true"
    android:scrollbars="none" >

</HorizontalScrollView>
<android.support.v4.app.FragmentTabHost
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">


        <FrameLayout
            android:id="@+id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">


        <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        </android.support.v4.view.ViewPager>

        </FrameLayout>

    </LinearLayout>

</android.support.v4.app.FragmentTabHost>

fragment2.java

public class Fragment2 extends android.support.v4.app.Fragment implements OnTabChangeListener, OnPageChangeListener {
    private FragmentTabHost mTabHost;
    private ViewPager viewPager;
    private MyFragmentPagerAdapter myViewPagerAdapter;
    View v;

    public Fragment2() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        v = inflater.inflate(R.layout.fragment2, container, false);
        viewPager = (ViewPager) v.findViewById(R.id.pager);

        // init tabhos
        this.initializeTabHost(savedInstanceState);

        // init ViewPager
        this.initializeViewPager();


        return v;
    }

    // fake content for tabhost
    class FakeContent implements TabHost.TabContentFactory {
        private final Context mContext;

        public FakeContent(Context context) {
            mContext = context;
        }

        @Override
        public View createTabContent(String tag) {
            View v = new View(mContext);
            v.setMinimumHeight(0);
            v.setMinimumWidth(0);
            return v;
        }
    }

    private void initializeViewPager() {
        List<android.support.v4.app.Fragment> fragments = new Vector<android.support.v4.app.Fragment>();

        fragments.add(new TramHor());
        fragments.add(new BusHor());
        fragments.add(new Train());


        this.myViewPagerAdapter = new MyFragmentPagerAdapter(
                getChildFragmentManager(), fragments);
        this.viewPager = (ViewPager) v.findViewById(R.id.pager);
        this.viewPager.setAdapter(this.myViewPagerAdapter);
        this.viewPager.setOnPageChangeListener(this);

    }

    private void initializeTabHost(Bundle args) {

        mTabHost = (FragmentTabHost) v.findViewById(android.R.id.tabhost);
        mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.tabcontent);
        mTabHost.addTab(mTabHost.newTabSpec("fragmentb").setIndicator("", getResources().getDrawable(R.drawable.tram)),
                TramHor.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("fragmentc").setIndicator("", getResources().getDrawable(R.drawable.bus)),
                BusHor.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("fragmentd").setIndicator("", getResources().getDrawable(R.drawable.train)),
                Train.class, null);
        mTabHost.setOnTabChangedListener(this);
    }



    public void onTabChanged(String tabId) {
        int pos = this.mTabHost.getCurrentTab();
        this.viewPager.setCurrentItem(pos);

        HorizontalScrollView hScrollView = (HorizontalScrollView)   v.findViewById(R.id.hScrollView);
        View tabView = mTabHost.getCurrentTabView();
        int scrollPos = tabView.getLeft()
                - (hScrollView.getWidth() - tabView.getWidth()) / 2;
        hScrollView.smoothScrollTo(scrollPos, 0);

    }


    public void onPageScrollStateChanged(int arg0) {
    }


    public void onPageScrolled(int arg0, float arg1, int arg2) {
    }


    public void onPageSelected(int position) {
        this.mTabHost.setCurrentTab(position);
    }
}

MyFragmentPageAdapter.java

public class MyFragmentPagerAdapter extends FragmentPagerAdapter {

    List<android.support.v4.app.Fragment> fragments;

    public MyFragmentPagerAdapter(FragmentManager fm, List<android.support.v4.app.Fragment> fragments) {
        super(fm);
        this.fragments = fragments;
    }

    @Override
    public Fragment getItem(int position) {
        return this.fragments.get(position);
    }

    @Override
    public int getCount() {

        return fragments.size();
    }

}

tabhost的片段

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.getgpslocation.fragment.TramHor">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="LIGNE/DIRECTION"
        android:layout_marginTop="70dp"
        android:layout_marginLeft="20dp"/>
    <View
        android:layout_width="wrap_content"
        android:layout_height="1dp"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:background="#000000"/>


    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/lv_sliding_menu2"
        android:background="#FFFFFF"
        android:choiceMode="singleChoice"
        android:layout_gravity="start"/>

</LinearLayout>

1 个答案:

答案 0 :(得分:1)

我找到了解决方案,xml中的viewpager不在正确的位置

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.getgpslocation.fragment.Fragment2"
    android:orientation="vertical">
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/main_content1"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RESEAU"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="20dp"/>
    <View
        android:layout_width="wrap_content"
        android:layout_height="1dp"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:background="#000000" />

    <HorizontalScrollView
        android:id="@+id/hScrollView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fillViewport="true"
        android:scrollbars="none"/>

    <android.support.v4.app.FragmentTabHost
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent">


        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <android.support.v4.view.ViewPager
                android:id="@+id/pager"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
            <FrameLayout
                android:id="@+id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1" >

            </FrameLayout>

        </LinearLayout>

    </android.support.v4.app.FragmentTabHost>

</LinearLayout>