【发布时间】: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_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<GridView
android:id="@+id/gvImages"
android:layout_width="match_parent"
android:layout_height="match_parent"
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_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="center"
tools:srcCompat="@tools:sample/avatars" />
</LinearLayout>```
*Thanks in advance*
【问题讨论】:
-
请添加您的xml文件,(grid_layout.xml)
-
你的意思,我没听懂?
-
对不起,更新您的问题,并添加您的 xml 文件。您是否需要使用 Grid_layout 自定义并添加标签,例如
-
完成,请看...
标签: java android view binding base