我坚持将片段滚动到布局中.
这是我的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.G@R_403_6610@TY_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); } }}
这是我的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:tabG@R_403_6610@ty="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 onl oadMore(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放入除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:tabG@R_403_6610@ty="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>
总结 以上是内存溢出为你收集整理的片段中滚动不适用于TabLayout全部内容,希望文章能够帮你解决片段中滚动不适用于TabLayout所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)