我有一个带有两个片段的ViewPager,每个片段都有一个RecyclerView。问题是,在我在另一个片段中添加第二个Recycler之前,一个Recycler一直没有问题。
当我滚动第二个Recycler时,第一个回收商中的项目(带有图像和文本的cardview)做了一些奇怪的事情。基本上,cardview的文本随机消失,有些项目有文本,有些则没有。即使是小吃店也不会再出现了。
知道发生了什么事吗?
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.CoordinatorLayout
android:id="@+id/coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Toolbar -->
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<include android:id="@+id/appbar"
layout="@layout/aux_toolbar_main"/>
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/MyCustomTabLayout"
app:tabMode="fixed"
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"
android:layout_below="@+id/tabs"
android:background="@android:color/white"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</android.support.design.widget.CoordinatorLayout>
<include layout="@layout/left_navigation_drawer"/>
</android.support.v4.widget.DrawerLayout>
main.java
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
_initToolbar();
_initViewPager();
_initNavigationDrawer();
_initAnimations();
mBackPressed = 0;
}
private void _initViewPager()
{
mViewPager = (ViewPager) findViewById(R.id.viewPager);
mTabLayout = (TabLayout) findViewById(R.id.tabs);
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
mProductsFragment = new ProductsFragment();
mRecommendedFragment = new RecommendedFragment();
adapter.addFragment(mRecommendedFragment, "Fragment 2");
adapter.addFragment(mProductsFragment, "Fragment 1");
mViewPager.setAdapter(adapter);
mViewPager.setCurrentItem(1);
mTabLayout.setupWithViewPager(mViewPager);
}
class ViewPagerAdapter extends FragmentStatePagerAdapter
{
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);
}
}
Fragment1.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.wallakoala.wallakoala.Fragments.ProductsFragment">
<!-- Recycler Grid -->
<android.support.v7.widget.RecyclerView
android:id="@+id/grid_recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none"
android:clipToPadding="false"/>
...
</FrameLayout>
Fragment2.xml 如果我滚动这个回收站,会发生奇怪的事情。
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/recommended_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.v7.widget.RecyclerView
android:id="@+id/recommended_grid_recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none"
android:clipToPadding="false"/>
...
</FrameLayout>
编辑:添加两个片段代码
Fragment1.java 当奇怪的事情发生时。
@Override
@SuppressWarnings("ConstantConditions")
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
// FrameLayout
mFrameLayout = (FrameLayout)getView().findViewById(R.id.frame);
...
// RecyclerView
mProductsRecyclerView = (RecyclerView)getView().findViewById(R.id.grid_recycler);
_initRecyclerView();
}
@SuppressWarnings("deprecation")
private void _initRecyclerView()
{
mProductsRecyclerView.setVisibility(View.VISIBLE);
mStaggeredGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
mProductAdapter = new ProductsGridAdapter(getActivity()
, mProductsDisplayedList
, mFrameLayout);
mProductsRecyclerView.setHasFixedSize(true);
mProductsRecyclerView.setLayoutManager(mStaggeredGridLayoutManager);
mProductsRecyclerView.setAdapter(mProductAdapter);
mProductsRecyclerView.setItemViewCacheSize(NUM_CACHED_PRODUCTS);
mProductsRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener()
{
boolean scrollingUp;
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
scrollingUp = dy > 0;
if (scrollingUp)
{
...
}
}
});
}
Fragment2.java 如果我滚动此回收器,它会弄乱另一个
@Override
@SuppressWarnings("ConstantConditions")
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
// FrameLayout
mFrameLayout = (FrameLayout)getView().findViewById(R.id.recommended_frame);
// RecyclerView
mProductsRecyclerView = (RecyclerView)getView().findViewById(R.id.recommended_grid_recycler);
_initRecyclerView();
}
private void _initRecyclerView()
{
mGridLayoutManager = new GridLayoutManager(getActivity(), 1);
mProductAdapter = new RecommendedListAdapter(getActivity()
, mProductsDisplayedList
, mFrameLayout);
mProductsRecyclerView.setLayoutManager(mGridLayoutManager);
mProductsRecyclerView.setAdapter(mProductAdapter);
mProductsRecyclerView.setVisibility(View.VISIBLE);
mProductsRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener()
{
boolean scrollingUp;
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
scrollingUp = dy > 0;
if (scrollingUp)
{
...
}
}
});
}
我不想用代码填充整个页面,所以如果缺少某些代码,我会添加它。
提前致谢,