我坚持将 fragment 滚动到 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>
关于android - 在 Fragment 中滚动不适用于 TabLayout,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39916544/