片段中滚动不适用于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]);
}
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: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 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: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>
标签:android-tablayout,listview,android,scroll 来源: https://codeday.me/bug/20191118/2025501.html