【问题标题】:TabHost Viewpager listview doesn't scrollTabHost Viewpager listview 不滚动
【发布时间】:2016-06-02 14:45:25
【问题描述】:

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

如果我尝试从 textview 滚动它可以工作,或者如果片段为空它可以工作,但是从 listview 没有,请注意 textview 它的宽度和高度是 wrap_content 而列表视图中的那些是 match_parent

frgament 2(它包含 tabhost 的片段)

片段.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>

【问题讨论】:

    标签: android listview android-viewpager


    【解决方案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>
    

    【讨论】:

      猜你喜欢
      • 2013-08-13
      • 1970-01-01
      • 2015-08-05
      • 1970-01-01
      • 2013-05-05
      • 2011-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多