java - 如何在java android中为Gridview的父类BaseAdapter实现viewBinding?
Posted
技术标签:
【中文标题】java - 如何在java android中为Gridview的父类BaseAdapter实现viewBinding?【英文标题】:How do i implement viewBinding in parent class BaseAdapter for Gridview in java android? 【发布时间】:2021-06-02 21:01:41 【问题描述】:这是我的 BaseAdapter 类,我想在这里实现视图绑定。我是怎么做到的。从而我可以减少没有。我的应用程序中的行数。
public class MyAdapter extends BaseAdapter
Context c;
int items[];
MyAdapter(Context c, int arr[])
this.c = c;
items = arr;
@Override
public int getCount()
return items.length;
@Override
public Object getItem(int position)
return null;
@Override
public long getItemId(int position)
return 0;
@Override
public View getView(int position, View convertView, ViewGroup parent)
if (convertView == null)
LayoutInflater inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.grid_layout, null);
ImageView imageView = convertView.findViewById(R.id.imageView);
imageView.setImageResource(items[position]);
return convertView;
这是 MainActivity.java 文件,基适配器的调用来自这个类文件
public class MainActivity extends AppCompatActivity
ActivityMainBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
int[] itemsarray = new int[]
R.drawable.ic_launcher_background, R.drawable.ic_launcher_foreground,
R.drawable.ic_launcher_background, R.drawable.ic_launcher_foreground,
R.drawable.ic_launcher_background, R.drawable.ic_launcher_foreground,
R.drawable.ic_launcher_background, R.drawable.ic_launcher_foreground,
R.drawable.ic_launcher_background, R.drawable.ic_launcher_foreground,
R.drawable.ic_launcher_background, R.drawable.ic_launcher_foreground,
;
MyAdapter adapter = new MyAdapter(this, itemsarray);
binding.gvImages.setAdapter(adapter);
这是activity_main.xml,MainActivity.java类的xml文件。这里是我们设计的所有地方
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
tools:context=".MainActivity">
<GridView
android:id="@+id/gvImages"
android:layout_
android:layout_
android:numColumns="auto_fit"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
这个id grid_layout.xml,也就是这个xml文件是用来自定义grid view中的view
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
xmlns:tools="http://schemas.android.com/tools">
<ImageView
android:id="@+id/imageView"
android:layout_
android:layout_
android:scaleType="center"
tools:srcCompat="@tools:sample/avatars" />
</LinearLayout>```
*Thanks in advance*
【问题讨论】:
请添加您的xml文件,(grid_layout.xml) 你的意思,我没听懂? 对不起,更新您的问题,并添加您的 xml 文件。您是否需要使用 Grid_layout 自定义并添加标签,例如像这样更改您的 BaseAdapter 代码
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import com.aijishnu.baseadaptersample.databinding.GridLayoutBinding;
public class MyAdapter extends BaseAdapter
Context c;
int items[];
MyAdapter(Context c, int arr[])
this.c = c;
items = arr;
@Override
public int getCount()
return items.length;
@Override
public Object getItem(int position)
return null;
@Override
public long getItemId(int position)
return 0;
@Override
public View getView(int position, View convertView, ViewGroup parent)
ViewHolder holder;
if (convertView == null)
GridLayoutBinding itemBinding = GridLayoutBinding.inflate(LayoutInflater.from(parent.getContext()), parent,false);
holder = new ViewHolder(itemBinding);
holder.view = itemBinding.getRoot();
holder.view.setTag(holder);
else
holder = (ViewHolder) convertView.getTag();
holder.binding.imageView.setImageResource(items[position]);
return holder.view;
private static class ViewHolder
private View view;
private GridLayoutBinding binding;
ViewHolder(GridLayoutBinding binding)
this.view = binding.getRoot();
this.binding = binding;
【讨论】:
以上是关于java - 如何在java android中为Gridview的父类BaseAdapter实现viewBinding?的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 OS Lollipop 在 Java 中为 Android 裁剪图像
如何在 android studio 中为线性布局制作“波浪”边框?
如何在 Kotlin Android 中为数据类创建空构造函数