2016-10-07 10 views
2

Ich bin fest mit dem Scrollen in einem Fragment in ein Tablayout. Das ist meine MainActivity KlasseScrollen in Fragment funktioniert nicht in einem TabLayout

public class Mainctivity extends AppCompatActivity { 
    private TabLayout tabLayout; 
    private ViewPager viewPager; 
    ViewPagerAdapter viewPagerAdapter; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     viewPager = (ViewPager) findViewById(R.id.viewpager); 
     setupViewPager(); 
     tabLayout = (TabLayout) findViewById(R.id.tabs); 
     tabLayout.setViewPager(TabLayout.GRAVITY_FILL); 
     tabLayout.setupWithViewPager(viewPager); 
     } 
    private void setupViewPager() { 
     viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager()); 
     String [] tabNames = new String[{"Technology","World","Life"} ; 
     int [] tabTopicIds = new int[]{1,2,3} ; 
     for(int i=0; i< 7; i++) 
     { 
     OneFragment homeFeed = new OneFragment(); 
     Bundle homeArgs = new Bundle(); 
     homeArgs.putString("PAGE_NUMBER",""+tabTopicIds[i]); 
     homeFeed.setArguments(homeArgs); 
     viewPagerAdapter.addFragment(homeFeed, tabNames[i]); 
    } 

    viewPager.setAdapter(viewPagerAdapter); 
    } 

class ViewPagerAdapter extends FragmentPagerAdapter { 
     private final List<Fragment> mFragmentList = new ArrayList<>(); 
     private final List<String> mFragmentTitleList = new ArrayList<>(); 

     public ViewPagerAdapter(FragmentManager manager) { 
      super(manager); 
     } 

     @Override 
     public Fragment getItem(int position) { 

      return mFragmentList.get(position); 
     } 

     @Override 
     public int getCount() { 
      return mFragmentList.size(); 
     } 

     public void addFragment(Fragment fragment, String title) { 
      mFragmentList.add(fragment); 
      mFragmentTitleList.add(title); 
     } 

     @Override 
     public CharSequence getPageTitle(int position) { 
      return mFragmentTitleList.get(position); 
     } 


    } 
} 

Und hier ist mein activity_main Layout.

<?xml version="1.0" encoding="utf-8"?> 

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="@color/colorPrimary" 
     app:layout_scrollFlags="scroll|enterAlways" 
     app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> 

     <android.support.design.widget.TabLayout 
      android:id="@+id/tabs" 
      android:layout_width="match_parent" 
      android:layout_height="48dip" 
      android:background="@color/white" 
      app:tabIndicatorColor="@color/colorPrimary" 
      app:pstsIndicatorHeight="3dip" 
      app:pstsTextAllCaps="false" 
      app:tabPaddingStart="2dp" 
      app:tabPaddingEnd="2dp" 
      app:tabGravity="fill"/> 
     <android.support.v4.view.ViewPager 
      android:id="@+id/viewpager" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 

    </android.support.design.widget.AppBarLayout> 

</android.support.design.widget.CoordinatorLayout> 

Für jedes Register gibt es eine FragmentOne Klasse mit viewPager zugeordnet ist, und das ist onCreateView Methode der FragmentOne

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View mainView = inflater.inflate(R.layout.activity_main, container, false); 
     listView = (ListView) mainView.findViewById(R.id.list); 
     feedItems = new ArrayList<FeedItem>(); 
     listAdapter = new FeedListAdapter(getActivity(), feedItems); 
     listView.setAdapter(listAdapter); 

     listView.setOnScrollListener(new EndlessScrollListener() { 
      @Override 
      public boolean onLoadMore(int page, int totalItemsCount) { 
       if(totalItemsCount > 150) return false; 
       return true; 
      } 

      @Override 
      public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { 
       if(!isLoading && firstVisibleItem + visibleItemCount >= totalItemCount && totalItemCount<MAX_ITEM_IN_LIST){ 
        requestToServer(); // request for next items 
       } 
      } 
     }); 

return mainView; 
} 

Layout FragmentOne

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/swipe_refresh_layout" 
android:layout_width="match_parent" 
android:layout_height="wrap_content"> 

     <ListView 
      android:id="@+id/list" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:divider="@android:color/darker_gray" 
      android:background="@color/white" 
      android:dividerHeight="1.0sp"/> 

</LinearLayout> 

Here I listView.setOnScrollListener in Listview von FragmentOne hinzufügen aber funktioniert nicht scrollen. Aber wenn ich FragmentOne ein anderes Layout wie LinearLayout außer Tablayout einfügen, funktioniert es gut.

+0

in Layout FragmentOne machen Eltern Linearlayout Höhe Match – Pavya

+0

Sie den Code in viewpager AppBarLayout in activity_main.xml gesetzt worden. Schneiden Sie den ViewPager aus und fügen Sie ihn außerhalb von AppBarLayout ein. Hoffe es löst es. –

Antwort

2

Sie haben den viewpager-Code innerhalb von AppBarLayout in activity_main.xml abgelegt. Schneiden Sie den ViewPager aus und fügen Sie ihn außerhalb von AppBarLayout ein. Kopieren Sie diesen Code in Ihre activity_main.xml. Hoffe es löst dein Problem.

<?xml version="1.0" encoding="utf-8"?> 

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<android.support.design.widget.AppBarLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 

<android.support.v7.widget.Toolbar 
    android:id="@+id/toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="?attr/actionBarSize" 
    android:background="@color/colorPrimary" 
    app:layout_scrollFlags="scroll|enterAlways" 
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> 

    <android.support.design.widget.TabLayout 
     android:id="@+id/tabs" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@color/white" 
     app:tabIndicatorColor="@color/colorPrimary" 
     app:pstsIndicatorHeight="3dip" 
     app:pstsTextAllCaps="false" 
     app:tabPaddingStart="2dp" 
     app:tabPaddingEnd="2dp" 
     app:tabGravity="fill"/> 


</android.support.design.widget.AppBarLayout> 

<android.support.v4.view.ViewPager 
     android:id="@+id/viewpager" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 

</android.support.design.widget.CoordinatorLayout> 
+0

Vielen Dank, was für eine große Erleichterung. – Moshii

Verwandte Themen