如何禁止 Android 中 GridView 滚动?
本示例演示如何在 Android 中禁用 gridView 滚动。
步骤 1 - 在 Android Studio 中创建新项目,转到“文件 ⇒ 新项目”,填写所有必需详情以创建新项目。
步骤 2 - 向 res/layout/activity_main.xml 添加以下代码。
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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:padding="8dp" tools:context=".MainActivity"> <GridView android:id="@+id/gridLayout" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout>
步骤 3 - 向 src/MainActivity.java 添加以下代码
import androidx.appcompat.app.AppCompatActivity; import android.annotation.SuppressLint; import android.content.Context; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.GridView; import android.widget.ImageView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { GridView gridView; Integer[] imageIDs = {R.drawable.one, R.drawable.two, R.drawable.three, R.drawable.four, R.drawable.five, R.drawable.six, R.drawable.seven, R.drawable.eight, R.drawable.nine, R.drawable.ten}; @SuppressLint("ClickableViewAccessibility") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); gridView = findViewById(R.id.gridLayout); gridView.setAdapter(new ImageAdapterGridView(this)); gridView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { Toast.makeText(MainActivity.this, "Scrolling is Disabled", Toast.LENGTH_SHORT).show(); return event.getAction() == MotionEvent.ACTION_MOVE; } }); } public class ImageAdapterGridView extends BaseAdapter { private Context context; ImageAdapterGridView(Context c) { context = c; } public int getCount() { return imageIDs.length; } public Object getItem(int position) { return null; } public long getItemId(int position) { return 0; } public View getView(int position, View convertView, ViewGroup parent) { ImageView imageView; if (convertView == null) { imageView = new ImageView(context); imageView.setLayoutParams(new GridView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT)); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); imageView.setPadding(20, 20, 20, 20); } else { imageView = (ImageView) convertView; } imageView.setImageResource(imageIDs[position]); return imageView; } } }
步骤 4 - 向 androidManifest.xml 添加以下代码
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="app.com.sample"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
让我们尝试运行您的应用程序。我认为您已将实际的 Android 移动设备连接到计算机。要从 android studio 运行应用程序,请打开一个项目的活动文件,然后单击工具栏中的运行 图标。选择您的移动设备作为选项,然后检查会显示您默认界面的移动设备 −
广告