📜  安卓 |带有示例的水平 RecyclerView

📅  最后修改于: 2022-05-13 01:54:42.131000             🧑  作者: Mango

安卓 |带有示例的水平 RecyclerView

RecyclerView 是作为 GridView 和 ListView 的继承者添加到 android studio 中的 ViewGroup。这是对两者的改进,可以在最新的 v-7 支持包中找到。它的创建是为了使任何具有 XML 布局的列表成为可能,作为一个可以大量定制的项目,同时提高 ListViews 和 GridViews 的效率。这种改进是通过回收用户不可见的视图来实现的。

例如:如果用户向下滚动到项目 4 和 5 可见的位置;项目 1、2 和 3 将从内存中清除以减少内存消耗。

在本文中,我们将学习如何创建可以水平滚动的回收器视图。

以下是详细步骤:

  1. 第一步:在你的项目中添加 Recycler View 小部件的依赖
    • Recycler View 的最新依赖项是:
      implementation 'com.android.support:recyclerview-v7:28.0.0'
      
    • 还要添加卡片视图的依赖项。 Card View 的最新依赖项是:
      implementation 'com.android.support:cardview-v7:28.0.0'
      
  2. 第 2 步:设置 activity_main.xml 布局文件
    activity_main.xml 布局文件包括:
    • 包含回收站视图的相对布局
    • 回收站视图小部件

    以下是 activity_main.xml 的完整代码:

    activity_main.xml
    
      
    
    
      
        
        
      
    


    item.xml
    
      
    
    
      
        
        
      
    


    Adapter.java
    package com.geeksforgeeks.horizontalrecyclerview;
      
    import android.support.v7.widget.RecyclerView;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.TextView;
    import android.view.LayoutInflater;
    import java.util.List;
      
    // The adapter class which
    // extends RecyclerView Adapter
    public class Adapter
        extends RecyclerView.Adapter {
      
        // List with String type
        private List list;
      
        // View Holder class which
        // extends RecyclerView.ViewHolder
        public class MyView
            extends RecyclerView.ViewHolder {
      
            // Text View
            TextView textView;
      
            // parameterised constructor for View Holder class
            // which takes the view as a parameter
            public MyView(View view)
            {
                super(view);
      
                // initialise TextView with id
                textView = (TextView)view
                               .findViewById(R.id.textview);
            }
        }
      
        // Constructor for adapter class
        // which takes a list of String type
        public Adapter(List horizontalList)
        {
            this.list = horizontalList;
        }
      
        // Override onCreateViewHolder which deals
        // with the inflation of the card layout
        // as an item for the RecyclerView.
        @Override
        public MyView onCreateViewHolder(ViewGroup parent,
                                         int viewType)
        {
      
            // Inflate item.xml using LayoutInflator
            View itemView
                = LayoutInflater
                      .from(parent.getContext())
                      .inflate(R.layout.item,
                               parent,
                               false);
      
            // return itemView
            return new MyView(itemView);
        }
      
        // Override onBindViewHolder which deals
        // with the setting of different data
        // and methods related to clicks on
        // particular items of the RecyclerView.
        @Override
        public void onBindViewHolder(final MyView holder,
                                     final int position)
        {
      
            // Set the text of each item of
            // Recycler view with the list items
            holder.textView.setText(list.get(position));
        }
      
        // Override getItemCount which Returns
        // the length of the RecyclerView.
        @Override
        public int getItemCount()
        {
            return list.size();
        }
    }


    MainActivity.java
    package com.geeksforgeeks.horizontalrecyclerview;
      
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.support.v7.widget.RecyclerView;
    import android.support.v7.widget.LinearLayoutManager;
    import android.view.View;
    import android.widget.Toast;
    import java.util.ArrayList;
      
    public class MainActivity extends AppCompatActivity {
      
        // Recycler View object
        RecyclerView recyclerView;
      
        // Array list for recycler view data source
        ArrayList source;
      
        // Layout Manager
        RecyclerView.LayoutManager RecyclerViewLayoutManager;
      
        // adapter class object
        Adapter adapter;
      
        // Linear Layout Manager
        LinearLayoutManager HorizontalLayout;
      
        View ChildView;
        int RecyclerViewItemPosition;
      
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
      
            // initialisation with id's
            recyclerView
                = (RecyclerView)findViewById(
                    R.id.recyclerview);
            RecyclerViewLayoutManager
                = new LinearLayoutManager(
                    getApplicationContext());
      
            // Set LayoutManager on Recycler View
            recyclerView.setLayoutManager(
                RecyclerViewLayoutManager);
      
            // Adding items to RecyclerView.
            AddItemsToRecyclerViewArrayList();
      
            // calling constructor of adapter
            // with source list as a parameter
            adapter = new Adapter(source);
      
            // Set Horizontal Layout Manager
            // for Recycler view
            HorizontalLayout
                = new LinearLayoutManager(
                    MainActivity.this,
                    LinearLayoutManager.HORIZONTAL,
                    false);
            recyclerView.setLayoutManager(HorizontalLayout);
      
            // Set adapter on recycler view
            recyclerView.setAdapter(adapter);
        }
      
        // Function to add items in RecyclerView.
        public void AddItemsToRecyclerViewArrayList()
        {
            // Adding items to ArrayList
            source = new ArrayList<>();
            source.add("gfg");
            source.add("is");
            source.add("best");
            source.add("site");
            source.add("for");
            source.add("interview");
            source.add("preparation");
        }
    }


  3. 第 3 步:为 Recycler 视图设置 item.xml 布局文件
    item.xml 布局文件包含 Recycler View 项目的布局。项目布局包含一个带有文本视图的卡片视图,其中包含一些文本。
    这是 item.xml 的完整代码:

    项目.xml

    
      
    
    
      
        
        
      
    
    

  4. 第 4 步:设置 Recycler View Adapter 的代码
    适配器是负责 RecyclerView 的主要代码。它包含处理 RecyclerView 实现的所有重要方法。成功实施的基本方法是:
    • onCreateViewHolder ,
    • onBindViewHolder ,
    • 获取项目计数

    适配器用于将数据从数据源设置到 Recycler View。

    这是适配器的完整代码。Java:

    适配器。Java

    package com.geeksforgeeks.horizontalrecyclerview;
      
    import android.support.v7.widget.RecyclerView;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.TextView;
    import android.view.LayoutInflater;
    import java.util.List;
      
    // The adapter class which
    // extends RecyclerView Adapter
    public class Adapter
        extends RecyclerView.Adapter {
      
        // List with String type
        private List list;
      
        // View Holder class which
        // extends RecyclerView.ViewHolder
        public class MyView
            extends RecyclerView.ViewHolder {
      
            // Text View
            TextView textView;
      
            // parameterised constructor for View Holder class
            // which takes the view as a parameter
            public MyView(View view)
            {
                super(view);
      
                // initialise TextView with id
                textView = (TextView)view
                               .findViewById(R.id.textview);
            }
        }
      
        // Constructor for adapter class
        // which takes a list of String type
        public Adapter(List horizontalList)
        {
            this.list = horizontalList;
        }
      
        // Override onCreateViewHolder which deals
        // with the inflation of the card layout
        // as an item for the RecyclerView.
        @Override
        public MyView onCreateViewHolder(ViewGroup parent,
                                         int viewType)
        {
      
            // Inflate item.xml using LayoutInflator
            View itemView
                = LayoutInflater
                      .from(parent.getContext())
                      .inflate(R.layout.item,
                               parent,
                               false);
      
            // return itemView
            return new MyView(itemView);
        }
      
        // Override onBindViewHolder which deals
        // with the setting of different data
        // and methods related to clicks on
        // particular items of the RecyclerView.
        @Override
        public void onBindViewHolder(final MyView holder,
                                     final int position)
        {
      
            // Set the text of each item of
            // Recycler view with the list items
            holder.textView.setText(list.get(position));
        }
      
        // Override getItemCount which Returns
        // the length of the RecyclerView.
        @Override
        public int getItemCount()
        {
            return list.size();
        }
    }
    

  5. 步骤 5. 设置 MainActivity 的代码。用于 Recycler 视图的Java文件
    MainActivity 包含 RecyclerView。使用 LinearLayoutManager 类在 Recycler 视图上设置布局管理器。
    这是 MainActivity 的完整代码。Java:

    主要活动。Java

    package com.geeksforgeeks.horizontalrecyclerview;
      
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.support.v7.widget.RecyclerView;
    import android.support.v7.widget.LinearLayoutManager;
    import android.view.View;
    import android.widget.Toast;
    import java.util.ArrayList;
      
    public class MainActivity extends AppCompatActivity {
      
        // Recycler View object
        RecyclerView recyclerView;
      
        // Array list for recycler view data source
        ArrayList source;
      
        // Layout Manager
        RecyclerView.LayoutManager RecyclerViewLayoutManager;
      
        // adapter class object
        Adapter adapter;
      
        // Linear Layout Manager
        LinearLayoutManager HorizontalLayout;
      
        View ChildView;
        int RecyclerViewItemPosition;
      
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
      
            // initialisation with id's
            recyclerView
                = (RecyclerView)findViewById(
                    R.id.recyclerview);
            RecyclerViewLayoutManager
                = new LinearLayoutManager(
                    getApplicationContext());
      
            // Set LayoutManager on Recycler View
            recyclerView.setLayoutManager(
                RecyclerViewLayoutManager);
      
            // Adding items to RecyclerView.
            AddItemsToRecyclerViewArrayList();
      
            // calling constructor of adapter
            // with source list as a parameter
            adapter = new Adapter(source);
      
            // Set Horizontal Layout Manager
            // for Recycler view
            HorizontalLayout
                = new LinearLayoutManager(
                    MainActivity.this,
                    LinearLayoutManager.HORIZONTAL,
                    false);
            recyclerView.setLayoutManager(HorizontalLayout);
      
            // Set adapter on recycler view
            recyclerView.setAdapter(adapter);
        }
      
        // Function to add items in RecyclerView.
        public void AddItemsToRecyclerViewArrayList()
        {
            // Adding items to ArrayList
            source = new ArrayList<>();
            source.add("gfg");
            source.add("is");
            source.add("best");
            source.add("site");
            source.add("for");
            source.add("interview");
            source.add("preparation");
        }
    }
    

输出: