簡體   English   中英

在 Fragment 中滾動不適用於 TabLayout

[英]Scrolling in Fragment doesn't work into a TabLayout

我堅持將片段滾動到 tablayout 中。 這是我的MainActivity

    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]); // note: this line can cause crashes
     }
    
     viewPager.setAdapter(viewPagerAdapter);
  }

class ViewPagerAdapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<>(); // note: this line can cause crashes
        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);
        }


    }
}

這是我的activity_main布局。

<?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>

對於每個選項卡,都有一個與 viewPager 關聯的FragmentOne類,這是FragmentOne onCreateView 方法

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;
}

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>

在這里,我將listView.setOnScrollListener添加到 FragmentOne 的 ListView 中,但無法滾動。 但是當我 FragmentOne 放入另一個 Layout 時,比如除了 Tablayout 之外的 LinearLayout,它工作正常。

您已將 viewpager 代碼放在 activity_main.xml 中的 AppBarLayout 中。 在 AppBarLayout 外剪切並粘貼 ViewPager。 將此代碼復制到您的 activity_main.xml 中。 希望它能解決你的問題。

<?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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM