2016-06-02 2 views
0

Tabhost + viewpager horizontal scroll. если фрагмент пуст или содержит текстовое изображение, он работает, но если фрагмент содержит просмотр списка, фрагмент его не прокручивает по горизонтали !!TabHost Viewpager listview не прокручивает

при попытке прокрутить из 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

Я нашел решение, ViewPager в XML не был в нужном месте

<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> 
Смежные вопросы