问题
i have a list view inside a recyclerview like this:
and scrolling of listview doesnt work on this implementation.
my goal is when clicking on button the listview toggle to show and hide. the main Ui is on viewpager for tab
public class tabOne extends Fragment {
RecyclerView myTabRecycler;
adapterMainList adapter;
public tabOne() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.tab_one, container, false);
myTabRecycler = (RecyclerView) v.findViewById(R.id.tabRC);
myTabRecycler.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(G.context);
llm.setOrientation(LinearLayoutManager.VERTICAL);
myTabRecycler.setLayoutManager(llm);
adapter = new adapterMainList(Utiles.getSampleCat());
myTabRecycler.setAdapter(adapter);
Utiles.Log(Utiles.getSampleCat().size());
myTabRecycler.startNestedScroll(2);
return v;
}
}
the adapter for recyclerView:
public class adapterMainList extends RecyclerView.Adapter<adapterMainList.CatViewHolder> {
private List<retroCategory> cats;
public adapterMainList(List<retroCategory> catList) {
this.cats = catList;
}
@Override
public int getItemCount() {
return cats.size();
}
@Override
public void onBindViewHolder(final CatViewHolder catViewHolder, final int position) {
final retroCategory cat = cats.get(position);
Utiles.Log("BindNow");
}
@Override
public CatViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View itemView = LayoutInflater.
from(viewGroup.getContext()).
inflate(R.layout.adapter_item_category, viewGroup, false);
return new CatViewHolder(itemView);
}
public static class CatViewHolder extends RecyclerView.ViewHolder {
protected ListView vList;
protected ImageView vImg;
public CatViewHolder(View v) {
super(v);
vList = (ListView) v.findViewById(R.id.adapter_item_list);
vImg = (ImageView) v.findViewById(R.id.adapter_item_img);
}
}
}
the item Ui:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/tools"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/adapter_item_cat_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right|center_vertical|center_horizontal"
android:padding="16dp"
android:textAppearance="@style/TextAppearance.AppCompat.Title"
android:textColor="@color/black" />
<android.support.v7.widget.CardView
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
app:cardUseCompatPadding="true"
card_view:cardBackgroundColor="@color/white"
card_view:cardElevation="4dp">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/adapter_item_num_session"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:padding="4dp"
android:gravity="left|bottom"
android:textColor="@color/colorPrimaryDark" />
<ImageView
android:id="@+id/adapter_item_img"
android:layout_width="0dp"
android:layout_height="80dp"
android:layout_weight="0.3"
android:scaleType="fitXY"
android:src="@drawable/sherlock" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
<ListView
android:id="@+id/adapter_item_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="8dp"
android:paddingRight="8dp">
</ListView>
</LinearLayout>
and on runnig app when i want to scroll the listview, RecyclerView Scrolled instead of that ... how can i fix That?? thanks
sorry for bad english
回答1:
You should not use a ListView
inside another scrollable view.
In this case you are using a ListView inside a RecyclerView.
Use a LinearLayout instead of the ListView. Something like:
public class MyListLayout extends LinearLayout implements
View.OnClickListener {
private Adapter list;
private View.OnClickListener mListener;
public MyListLayout(Context context) {
super(context);
}
public MyListLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public MyListLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public void onClick(View v) {
if (mListener!=null)
mListener.onClick(v);
}
public void setList(Adapter list) {
this.list = list;
//Popolute list
if (this.list!=null){
for (int i=0;i<this.list.getCount();i++){
View item= list.getView(i, null,null);
this.addView(item);
}
}
}
public void setmListener(View.OnClickListener mListener) {
this.mListener = mListener;
}
}
来源:https://stackoverflow.com/questions/34665381/listview-inside-recyclerview-dont-scroll