【问题标题】:Expandable recycler view and a textview below issue可扩展的回收器视图和问题下方的文本视图
【发布时间】:2019-07-23 01:54:37
【问题描述】:

我正在尝试实现一个带有 2 个标题的 2 个可扩展回收器视图。所以我的观点有 1. 2. 3. 4. 由于我的 recyclerview 是可扩展的,并且当我扩展第一个 recyclerview 时,它会进入我的第二个 textview 中。 即,我的回收站视图有一个单独的滚动条。

我希望在展开第一个 recyclerview 时将第二个 textview 和 recyclerview 向下移动。

activity.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_marginBottom="10dp"
    tools:context=".ListActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:textSize="22sp"
        android:text="Weightage: 40%"/>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#fafafa" />
</LinearLayout>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:textSize="22sp"
        android:layout_marginBottom="10dp"
        android:text="Weightage: 60%"/>
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recview2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#fafafa" />
</LinearLayout>
</ScrollView>

第二个 recyclerview 也是如此。在展开第二个回收器视图时,recyclerview 项目有一个单独的滚动,上面的文本停留在一个地方。整个滚动不起作用。

【问题讨论】:

    标签: android android-recyclerview


    【解决方案1】:

    您只需使用一个 Recyclerview 即可实现此目的。为您的数据创建一个名为“Model”的模型类,其中添加一个变量,如“isHeader”。当您准备要在 Recyclerview 中显示的数据列表时,请使用 'isHeader = true'

    另外,制作两个 (item_layout.xml & header_layout.xml) xml 文件,一个用于标题,另一个用于原始数据项。

    在 Recyclerview 适配器类中,对于上述每个布局,您都有两个单独的 ViewHolder 类。 refer this for more details and example

    public class MultiViewTypeAdapter extends RecyclerView.Adapter {
        public static final int HEADER_VIEW = 0;
        public static final int LIST_VIEW = 1;
    
        private Resources resources;
        private DownloadListener downloadListener;
    
        public MyToursAdapter(List<Model> objects) {
            super(objects);
            resources = App.getApp().getResources();
        }
    
        @NonNull
        @Override
        public RecycleView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            if (viewType == HEADER_VIEW) {
                return new SectionHeaderItemViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.header_layout, parent, false));
            } else
                return new ListItemViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout, parent, false));
        }
    
        @Override
        public void onBindViewHolder(@NonNull BaseRecycleAdapter.ViewHolder holder, int position) {
            if (getItemViewType(position) == HEADER_VIEW)
                ((HeaderItemViewHolder) holder).bindHeaderViewHolder(objects.get(position));
            else
                ((ListItemViewHolder) holder).bindListViewHolder(objects.get(position));
        }
    
        @Override
        public int getItemViewType(int position) {
            if (objects.get(position).isHeader())
                return HEADER_VIEW;
            else
                return LIST_VIEW;
        }
    
      public class ListItemViewHolder extends BaseRecycleAdapter.ViewHolder {
         //write code to show data from list object.
      }
    
      public class HeaderItemViewHolder extends BaseRecycleAdapter.ViewHolder {
         //write code to show data from list object.
      }
    }

    enter code here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-23
      • 1970-01-01
      • 1970-01-01
      • 2014-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多