【问题标题】:How to creating a multi layer list?如何创建多层列表?
【发布时间】:2016-08-14 08:02:57
【问题描述】:

我想创建一个带有分隔符的稍后列表,如下所示:

知道我该怎么做这个吗? 我熟悉简单的 RecyclerView 和 ListView 及其适配器,但仍然不明白这个列表背后的想法!

【问题讨论】:

  • 我正在为你做一个布局,给我几分钟时间

标签: android listview android-recyclerview material-design


【解决方案1】:

您将需要覆盖 recyclerview 适配器的 getItemViewType 方法,并区分标头和常规项目。然后在 onCreateViewHolder 上,根据类型填充正确的布局。最后,如果需要,ViewHolder 本身也可以做特定类型的事情。

为了使事情更简单,您可以使用不同的布局作为视图类型,因为它们是整数。这是一个例子: https://github.com/urandom/gearshift/blob/master/gearshift/src/main/java/org/sugr/gearshift/ui/TorrentListMenuFragment.java#L1084

【讨论】:

    【解决方案2】:

    您可以使用库 SectionedRecyclerViewAdapter 轻松实现此目的。您可以将项目分组并为每个部分添加标题:

    class MySection extends StatelessSection {
    
        String title;
        List<String> list;
    
        public MySection(String title, List<String> list) {
            // call constructor with layout resources for this Section header and items 
            super(R.layout.section_header, R.layout.section_item);
    
            this.title = title;
            this.list = list;
        }
    
        @Override
        public int getContentItemsTotal() {
            return list.size(); // number of items of this section
        }
    
        @Override
        public RecyclerView.ViewHolder getItemViewHolder(View view) {
            // return a custom instance of ViewHolder for the items of this section
            return new MyItemViewHolder(view);
        }
    
        @Override
        public void onBindItemViewHolder(RecyclerView.ViewHolder holder, int position) {
            MyItemViewHolder itemHolder = (MyItemViewHolder) holder;
    
            // bind your view here
            itemHolder.tvItem.setText(list.get(position));
        }
    
        @Override
        public RecyclerView.ViewHolder getHeaderViewHolder(View view) {
            return new SimpleHeaderViewHolder(view);
        }
    
        @Override
        public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder) {
            MyHeaderViewHolder headerHolder = (MyHeaderViewHolder) holder;
    
            // bind your header view here
            headerHolder.tvItem.setText(title);
        }
    }
    

    然后你用你的部分设置 RecyclerView:

    // Create an instance of SectionedRecyclerViewAdapter 
    SectionedRecyclerViewAdapter sectionAdapter = new SectionedRecyclerViewAdapter();
    
    // Create your sections with the list of data
    MySection testSection = new MySection("Test Group", testList);
    MySection commSection = new MySection("Communicate", commList);
    
    // add your sections to the adapter
    sectionAdapter.addSection(testSection);
    sectionAdapter.addSection(commSection);
    
    // Set up your RecyclerView with the SectionedRecyclerViewAdapter
    RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
    recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    recyclerView.setAdapter(sectionAdapter);
    

    在 section_header.xml 中,您可以使用部分标题和分隔线创建布局。在 section_item.xml 中创建项目图像和文本的布局。

    【讨论】:

      【解决方案3】:

      我做了这个,我把它做成了一个粗略的草图,现在你只需要根据自己的喜好调整它。希望对您有所帮助!

      <?xml version="1.0" encoding="utf-8"?>
      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical" android:layout_width="match_parent"
      android:layout_height="match_parent">
      
      <TextView
          android:padding="19dp"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:textSize="25sp"
          android:textAlignment="center"
          android:textColor="#000000"
          android:textStyle="bold"
          android:drawableStart="@drawable/ic_vulling"
          android:text="Import"
          android:id="@+id/textView1"
          android:layout_alignParentTop="true"
          android:layout_alignParentStart="true" />
      
      <TextView
          android:padding="19dp"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:textSize="25sp"
          android:textColor="#000000"
          android:textAlignment="center"
          android:textStyle="bold"
          android:drawableStart="@drawable/ic_vulling"
          android:text="Gallery"
          android:id="@+id/textView2"
          android:layout_below="@+id/textView1"
          android:layout_alignParentStart="true" />
      
      <TextView
          android:padding="19dp"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:textSize="25sp"
          android:textColor="#000000"
          android:textAlignment="center"
          android:textStyle="bold"
          android:drawableStart="@drawable/ic_vulling"
          android:text="Slideshow"
          android:id="@+id/textView3"
          android:layout_below="@+id/textView2"
          android:layout_alignParentStart="true" />
      
      <TextView
          android:padding="19dp"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:textSize="25sp"
          android:textColor="#000000"
          android:textAlignment="center"
          android:textStyle="bold"
          android:drawableStart="@drawable/ic_vulling"
          android:text="Tools"
          android:id="@+id/textView4"
          android:layout_below="@+id/textView3"
          android:layout_alignParentStart="true"/>
      
      <View android:layout_width="fill_parent"
          android:layout_height="1dp"
          android:background="#000000"
          android:layout_alignBottom="@+id/textView4"
          android:layout_alignParentStart="true"
          android:id="@+id/view" />
      
      <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:textSize="30sp"
          android:text="Test Group"
          android:id="@+id/textView5"
          android:layout_below="@+id/view"
          android:layout_alignParentStart="true" />
      
      <TextView
          android:padding="19dp"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:textSize="25sp"
          android:textColor="#000000"
          android:textAlignment="center"
          android:textStyle="bold"
          android:drawableStart="@drawable/ic_vulling"
          android:text="Test"
          android:id="@+id/textView6"
          android:layout_below="@+id/textView5"
          android:layout_alignParentStart="true"/>
      
      <View android:layout_width="fill_parent"
          android:layout_height="1dp"
          android:background="#000000"
          android:id="@+id/view2"
          android:layout_alignBottom="@+id/textView6"
          android:layout_alignParentStart="true" />
      
      <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:textSize="30sp"
          android:text="Communicate"
          android:id="@+id/textView7"
          android:layout_below="@+id/view2"
          android:layout_alignParentStart="true" />
      
      <TextView
          android:padding="19dp"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:textSize="25sp"
          android:textColor="#000000"
          android:textAlignment="center"
          android:textStyle="bold"
          android:drawableStart="@drawable/ic_vulling"
          android:text="Share"
          android:id="@+id/textView8"
          android:layout_below="@+id/textView7"
          android:layout_alignParentStart="true"/>
      
      <TextView
          android:padding="19dp"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:textSize="25sp"
          android:textColor="#000000"
          android:textAlignment="center"
          android:textStyle="bold"
          android:drawableStart="@drawable/ic_vulling"
          android:text="Send"
          android:id="@+id/textView9"
          android:layout_below="@+id/textView8"
          android:layout_alignParentStart="true"/>
      

      【讨论】:

      • 要使它们可点击添加:android:clickable="true"
      【解决方案4】:

      如果你想为导航抽屉创建这个,我会说看看this

      菜单组使得创建组和状态跟踪帮助更改所选选项的颜色变得非常简单

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-21
        • 1970-01-01
        • 2021-09-04
        相关资源
        最近更新 更多