我制作了一个包含3个RecyclerView的片段,1个是水平RV工作,但另外2个是垂直的,不起作用。我无法滚动它。
(图片链接:[http://s15.postimg.org/lc6bxz9ej/so1.png][2])
HomeFragment.java @AfterViews
LinearLayoutManager catLM = new LinearLayoutManager(mContext);
mRVcatList.setLayoutManager(catLM);
mRVcatList.setAdapter(new CategoryAdapter(initCategories()));
LinearLayoutManager hotLM = new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false);
mRVhotList.setLayoutManager(hotLM);
mRVhotList.setAdapter(new HotItemsAdapter(initHotItems()));
LinearLayoutManager brandLM = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
mRVbrandList.setLayoutManager(brandLM);
mRVbrandList.setAdapter(new BrandAdapter(initBrands()));
mRVbrandList.addOnItemTouchListener(new HotItemClickListener(mContext, new HotItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.mFLframe, new ProductListFragment_());
transaction.addToBackStack(null);
transaction.commit();
}
}));
mRVcatList.addOnItemTouchListener(new HotItemClickListener(mContext, new HotItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.mFLframe, new CategoryFragment_());
transaction.addToBackStack(null);
((MainActivity) mContext).hideLogo();
transaction.commit();
}
}));
mRVhotList.addOnItemTouchListener(new HotItemClickListener(mContext, new HotItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.mFLframe, new DetailFragment_());
transaction.addToBackStack(null);
((MainActivity) mContext).hideLogo();
transaction.commit();
}
}));
这是我的XML文件,
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<!--//HOT-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/mRLtopHot">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hot Items"
android:textColor="@color/ColorPrimaryDark"
android:paddingLeft="5dp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/mTVhot"
android:textSize="@dimen/bebas_medium" />
<View
android:layout_width="wrap_content"
android:layout_height="2dip"
android:background="#00B7F6"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/mRVhotList"
android:layout_width="match_parent"
android:layout_height="140dp"
android:padding="5dp"
android:background="#FFFFFF"
android:layout_below="@+id/mRLtopHot"/>
<!--CAT-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#FFFFFF"
android:layout_marginTop="10dp"
android:id="@+id/mRLtopCat"
android:layout_below="@+id/mRVhotList"
android:layout_centerHorizontal="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Categories"
android:paddingLeft="5dp"
android:textColor="@color/ColorPrimaryDark"
android:id="@+id/mTVcat"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:clickable="false"
android:textSize="@dimen/bebas_medium" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/mIVcatCollapse"
android:paddingRight="5dp"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<View
android:id="@+id/view1"
android:layout_width="wrap_content"
android:layout_height="2dip"
android:background="#00B7F6"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/mRVcatList"
android:layout_width="match_parent"
android:layout_height="140dp"
android:background="#FFFFFF"
android:layout_below="@+id/mRLtopCat"/>
<!--BRAND-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="10dp"
android:id="@+id/mRLtopBrand"
android:background="#FFFFFF"
android:layout_below="@+id/mRVcatList"
android:layout_centerHorizontal="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Brands"
android:paddingLeft="5dp"
android:textSize="@dimen/bebas_medium"
android:textColor="@color/ColorPrimaryDark"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/mTVbrand" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="5dp"
android:id="@+id/mIVbrandCollapse"
android:background="@drawable/abc_ic_menu_moreoverflow_mtrl_alpha"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<View
android:layout_width="wrap_content"
android:layout_height="2dip"
android:background="#00B7F6"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/mRVbrandList"
android:layout_width="match_parent"
android:layout_height="140dp"
android:background="#FFFFFF"
android:layout_below="@+id/mRLtopBrand"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
之前,我关闭了“类别”和“品牌”标签,点击后可以展开(这只是setVisibility的一个简单用法),而RecyclerView确实滚动了!但是我不再使用那个UI了。不幸的是,出现了这个问题。
如何使垂直RecyclerView正常工作?谢谢!
答案 0 :(得分:3)
ScrollView中不能有列表样式布局(ScrollView会破坏listview / gridview / recyclerview滚动函数)
您必须获得RecyclerView的高度才能修复它。以下是一个示例:Android list view inside a scroll view
或者,您可以使用此库,但它使用ListView:https://github.com/PaoloRotolo/ExpandableHeightListView(也许可以使用RecyclerView)