📜  如何在 Android 中禁用 GridView 滚动?

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

如何在 Android 中禁用 GridView 滚动?

GridView 是一个 ViewGroup,它可以以由行和列组成的类似网格的结构显示对象或数据库列表中的数据。网格视图需要一个适配器来从资源中获取数据。该视图可以水平和垂直滚动。 GridView 的滚动能力默认设置为启用。

但是,在本文中,我们将展示如何禁用 GridView 的滚动功能。

分步实施

第 1 步:在 Android Studio 中创建一个新项目

要在 Android Studio 中创建新项目,请参阅如何在 Android Studio 中创建/启动新项目。我们在 Kotlin 中演示了该应用程序,因此请确保在创建新项目时选择 Kotlin 作为主要语言。



步骤 2:使用 activity_main.xml 文件

导航到app > res > layout > activity_main.xml并将以下代码添加到该文件中。下面是activity_main.xml文件的代码。在布局中创建这个简单的 GridView。

XML


  
    
  


Kotlin
import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.AbsListView
import android.widget.BaseAdapter
import android.widget.ImageView
  
// Array of Images, 
// we used GeeksforGeeks logo
private val myImages = arrayOf(
    R.drawable.logo,
    R.drawable.logo,
    R.drawable.logo,
    R.drawable.logo,
    R.drawable.logo,
    R.drawable.logo,
    R.drawable.logo,
)
  
class MyGridViewAdapter constructor(c:Context): BaseAdapter() {
    private val context: Context = c
    
    override fun getCount(): Int {
        return myImages.size
    }
      
    override fun getItem(position: Int): Any? {
        return null
    }
      
    override fun getItemId(position: Int): Long {
        return 0
    }
      
    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
        val imageView: ImageView
        
        if (convertView == null) {
            imageView = ImageView(context)
            imageView.layoutParams = AbsListView.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
            )
            imageView.scaleType = ImageView.ScaleType.CENTER_CROP
            imageView.setPadding(30, 30, 30, 30)
        }
        else {
            imageView = convertView as ImageView
        }
          
        imageView.setImageResource(myImages[position])
          
        return imageView
    }
}


Kotlin
import android.annotation.SuppressLint
import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.widget.*
  
class MainActivity : AppCompatActivity() {
  
    @SuppressLint("ClickableViewAccessibility")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
        // Declare the GridView from the layout
        val gridView = findViewById(R.id.gridLayout)
          
        // Specify number of columns
        gridView.numColumns = 1
        
        // Setting the grid view 
        // adapter as MyGridViewAdapter
        gridView.adapter = MyGridViewAdapter(this)
    }
}


Kotlin
import android.annotation.SuppressLint
import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.widget.*
  
class MainActivity : AppCompatActivity() {
  
    @SuppressLint("ClickableViewAccessibility")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
        val gridView = findViewById(R.id.gridLayout)
        gridView.numColumns = 1
        gridView.adapter = MyGridViewAdapter(this)
  
  
        // What should happen when 
        // the Grid View is touched
        gridView.setOnTouchListener { _, event ->
            
            // A Toast is generated for every touch and 
            // event action is set as ACTION_MOVE
            Toast.makeText(applicationContext, "Scrolling is Disabled", Toast.LENGTH_SHORT).show()
            event.action == MotionEvent.ACTION_MOVE
        }
    }
}


步骤 3:为网格视图创建一个适配器 (MyGridViewAdapter.kt)

我们必须创建一个适配器来将数据(在我们的例子中是图像)发送到网格视图。因此,创建一个新类并为其指定一个相关名称。要查看我们如何添加这些照片,请参阅步骤 6

科特林

import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.AbsListView
import android.widget.BaseAdapter
import android.widget.ImageView
  
// Array of Images, 
// we used GeeksforGeeks logo
private val myImages = arrayOf(
    R.drawable.logo,
    R.drawable.logo,
    R.drawable.logo,
    R.drawable.logo,
    R.drawable.logo,
    R.drawable.logo,
    R.drawable.logo,
)
  
class MyGridViewAdapter constructor(c:Context): BaseAdapter() {
    private val context: Context = c
    
    override fun getCount(): Int {
        return myImages.size
    }
      
    override fun getItem(position: Int): Any? {
        return null
    }
      
    override fun getItemId(position: Int): Long {
        return 0
    }
      
    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
        val imageView: ImageView
        
        if (convertView == null) {
            imageView = ImageView(context)
            imageView.layoutParams = AbsListView.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
            )
            imageView.scaleType = ImageView.ScaleType.CENTER_CROP
            imageView.setPadding(30, 30, 30, 30)
        }
        else {
            imageView = convertView as ImageView
        }
          
        imageView.setImageResource(myImages[position])
          
        return imageView
    }
}

步骤 4:使用MainActivity.kt 文件

转到MainActivity.kt文件并参考以下代码。下面是MainActivity.kt文件的代码。将适配器链接到主代码 (MainActivity.kt) 中的网格视图。代码中添加了注释以更详细地理解代码。



科特林

import android.annotation.SuppressLint
import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.widget.*
  
class MainActivity : AppCompatActivity() {
  
    @SuppressLint("ClickableViewAccessibility")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
        // Declare the GridView from the layout
        val gridView = findViewById(R.id.gridLayout)
          
        // Specify number of columns
        gridView.numColumns = 1
        
        // Setting the grid view 
        // adapter as MyGridViewAdapter
        gridView.adapter = MyGridViewAdapter(this)
    }
}

输出:现在运行应用程序

您可以看到我们能够垂直滚动 GridView。

第五步:在主代码中添加这段代码来禁用滚动(MainActivity.kt)

科特林

import android.annotation.SuppressLint
import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.widget.*
  
class MainActivity : AppCompatActivity() {
  
    @SuppressLint("ClickableViewAccessibility")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
        val gridView = findViewById(R.id.gridLayout)
        gridView.numColumns = 1
        gridView.adapter = MyGridViewAdapter(this)
  
  
        // What should happen when 
        // the Grid View is touched
        gridView.setOnTouchListener { _, event ->
            
            // A Toast is generated for every touch and 
            // event action is set as ACTION_MOVE
            Toast.makeText(applicationContext, "Scrolling is Disabled", Toast.LENGTH_SHORT).show()
            event.action == MotionEvent.ACTION_MOVE
        }
    }
}

输出:运行应用程序

您可以看到我们无法垂直滚动网格视图。

注意:您可以在资源中添加以下图片

从网上下载的图片

现在只需将其复制粘贴到res文件夹中的Drawables文件夹中。为它们命名并单击“确定”

想要一个更快节奏和更具竞争力的环境来学习 Android 的基础知识吗?
单击此处前往由我们的专家精心策划的指南,旨在让您立即做好行业准备!