我有RecyclerView
,其中包含一堆CardViews
。然后每个CardView
都有自己的RecyclerView
。问题是只有"主要" RecyclerView
正在滚动。 CardView
中嵌套的一个没有获得焦点,所以我无法滚动它。我怎样才能使它们都可以滚动?
家长布局
<android.support.design.widget.CoordinatorLayout android:id="@+id/coordinatorLayout_main"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="false">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView_savedFiles"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:padding="8dp"/>
</android.support.design.widget.CoordinatorLayout>
父适配器
public class AdapterSavedFilesCards extends RecyclerView.Adapter<AdapterSavedFilesCards.ViewHolderSavedFilesCard>{
/**
* List of cards in the recycler view
*/
private final ArrayList<CardViewSavedFiles> cards;
private final Context context;
public AdapterSavedFilesCards(final Context context){
this.context = context;
cards = new ArrayList<>(Arrays.asList(new CardViewSavedFiles[]{
new CardViewSavedFiles("Card One"),
new CardViewSavedFiles("Card Two"),
new CardViewSavedFiles("Card Three")
}));
}
@Override
public ViewHolderSavedFilesCard onCreateViewHolder(ViewGroup parent, int viewType){
final View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view_saved_files, parent, false);
return new AdapterSavedFilesCards.ViewHolderSavedFilesCard(view);
}
@Override
public void onBindViewHolder(ViewHolderSavedFilesCard holder, int position){
//Get the card at this position
final CardViewSavedFiles card = cards.get(position);
holder.recyclerView.setLayoutManager(new LinearLayoutManager(context));
holder.recyclerView.setItemAnimator(null);
holder.recyclerView.setHasFixedSize(true);
holder.recyclerView.setAdapter(card.getAdapterSavedFiles());
holder.title.setText(card.getTitle());
holder.subTitle.setText("Subtitle");
((CardView)holder.itemView).setCardBackgroundColor(ContextCompat.getColor(holder.itemView.getContext(), card.getBackgroundColor()));
}
@Override
public int getItemCount(){
return cards.size();
}
public ArrayList<CardViewSavedFiles> getCards(){
return cards;
}
protected class ViewHolderSavedFilesCard extends RecyclerView.ViewHolder{
private final TextView title;
private final TextView subTitle;
private final RecyclerView recyclerView;
public ViewHolderSavedFilesCard(final View itemView){
super(itemView);
title = (TextView) itemView.findViewById(R.id.title);
subTitle = (TextView) itemView.findViewById(R.id.subTitle);
recyclerView = (RecyclerView) itemView.findViewById(R.id.recyclerView);
}
}
}
儿童布局
<android.support.v7.widget.CardView
android:id="@+id/cardView_savedFactors"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/rootView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/subTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="128dp"
android:paddingStart="@dimen/content_padding_start"
android:paddingEnd="@dimen/content_padding_end"/>
<Button
android:id="@+id/button"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</android.support.v7.widget.CardView>
儿童适配器
public class AdapterSavedFiles extends RecyclerView.Adapter<AdapterSavedFiles.ViewHolderSavedFiles>{
private final ArrayList<File> files = new ArrayList<>();
private final FILE_TYPE fileType;
public AdapterSavedFiles(final Context context, final FILE_TYPE fileType){
this.fileType = fileType;
switch (fileType){
case ONE:
files.addAll(Arrays.asList(FileManager.getInstance(context).getFilesDirOne().listFiles()));
break;
case TWO:
files.addAll(Arrays.asList(FileManager.getInstance(context).getFilesDirTwo().listFiles()));
break;
}
}
@Override
public ViewHolderSavedFiles onCreateViewHolder(ViewGroup parent, int viewType){
final View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_saved_file, parent, false);
return new AdapterSavedFiles.ViewHolderSavedFiles(view);
}
@Override
public void onBindViewHolder(ViewHolderSavedFiles holder, int position){
holder.fileName.setText("List item " + position);
switch (fileType){
case ONE:
holder.icon.setBackgroundTintList(ColorStateList.valueOf(ContextCompat.getColor(holder.itemView.getContext(), R.color.primary)));
holder.icon.setText("P");
break;
case TWO:
holder.icon.setBackgroundTintList(ColorStateList.valueOf(ContextCompat.getColor(holder.itemView.getContext(), R.color.green)));
holder.icon.setText("F");
break;
}
}
@Override
public int getItemCount(){
return files.size();
}
public ArrayList<File> getFiles(){
return files;
}
protected class ViewHolderSavedFiles extends RecyclerView.ViewHolder{
private final TextView fileName;
private final TextView icon;
public ViewHolderSavedFiles(final View itemView){
super(itemView);
icon = (TextView) itemView.findViewById(R.id.icon);
fileName = (TextView) itemView.findViewById(R.id.file_name);
}
}
}
布局屏幕截图
答案 0 :(得分:0)
您可以在onBindViewHolder中使用ChildView中的ListView而不是TextView。这可以解决在CardView中滚动子项。