“我最感兴趣的是根据材料设计指南和/或最常见的方式了解执行此操作的标准方式。”
我不确定我的答案是材料设计还是通用,但我已经完成了基于 Google“照片”应用中的选择处理的 GridView。我已经对其进行了测试,并且知道它将包括一个突出显示的边框和一个“复选框”。
** 免责声明 **
我考虑过使用选择器,但如果您的视图滚动,似乎会出现问题(请参阅this)。另外,我使用了一个自定义复选框(实际上只是两个不同的可绘制对象),部分原因是this。此外,这个答案基本上只是在纵向模式下处理手机。需要添加更多来处理不同的配置(屏幕尺寸、方向等)
这里有很多,但就像我说的,我已经测试过了。整个项目已发布到GitHub。
1、在MainActivity onCreate()
mList = new ArrayList<>();
// fill your list here
mAdapter = new GridAdapter(getApplicationContext(), R.layout.grid_item, mList);
GridView gridView = (GridView)findViewById(R.id.grid_view);
gridView.setAdapter(mAdapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Log.d(TAG, "item clicked = " + i);
boolean isSelected = mList.get(i).isSelected();
mList.get(i).setSelected(!isSelected);
mAdapter.notifyDataSetChanged();
}
});
接下来,覆盖适配器中的 getView()(我使用的是 ArrayAdapter)
@NonNull
@Override
public View getView(int position, View convertView, @NonNull ViewGroup parent) {
RelativeLayout itemLayout;
GridItem gridItem = (GridItem)getItem(position);
// use existing Views when we can
if(convertView == null) {
LayoutInflater inflater = (LayoutInflater)
getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
itemLayout = (RelativeLayout) inflater.inflate(R.layout.grid_item, null);
} else {
itemLayout = (RelativeLayout) convertView;
}
// get your bitmap or list item etc. here
BitmapDrawable bitmap = gridItem.getBitmap();
ImageView imageView = (ImageView)itemLayout.findViewById(R.id.image_view);
imageView.setBackground(bitmap);
if(gridItem.isSelected()) {
final int PADDING = 16;
Log.d(TAG, "position " + position + " is selected");
itemLayout.findViewById(R.id.image_frame).setPadding(PADDING, PADDING, PADDING, PADDING);
itemLayout.findViewById(R.id.custom_check_box)
.setBackgroundResource(R.drawable.custom_check_box_selected);
} else {
Log.d(TAG, "postion " + position + " is NOT selected");
itemLayout.findViewById(R.id.image_frame).setPadding(0,0,0,0);
itemLayout.findViewById(R.id.custom_check_box)
.setBackgroundResource(R.drawable.custom_check_box_unselected);
}
return itemLayout;
}
这是 GridItem 类的核心,省略了 getter 和 setter。
public class GridItem {
private BitmapDrawable bitmap = null;
private boolean isSelected = false;
public GridItem(BitmapDrawable bitmap) {
this.bitmap = bitmap;
}
}
Java 就是这样。现在来一些xml
grid_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
>
<FrameLayout
android:layout_width="140dp"
android:layout_height="140dp"
android:id="@+id/image_frame"
>
<!-- view for the main image -->
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="center"
android:background="@android:color/white"
android:id="@+id/image_view"
/>
</FrameLayout>
<!-- view for the 'checkbox' in upper left corner -->
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:src="@drawable/custom_check_box_unselected"
android:id="@+id/custom_check_box"
/>
</RelativeLayout>
这将进入 content_main.xml 或 activity_main.xml 等。
<GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="2"
android:verticalSpacing="10dp"
android:horizontalSpacing="20dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:id="@+id/grid_view"
>
</GridView>
现在你的可绘制文件夹有 2 个文件。
custom_check_box_unselected.xml
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="20dp"
android:width="20dp"
android:viewportWidth="400"
android:viewportHeight="400">
<!-- the outside box -->
<!-- top line & top left corner -->
<path android:pathData="M 340 30 H 62 c -20 0 -35 15 -35 35 "
android:strokeColor="#000000" android:strokeWidth="20" />
<!-- left line & bottom left corner -->
<path android:pathData="M 27 64 v271 c0 20 15 35 35 35 "
android:strokeColor="#000000" android:strokeWidth="20" />
<!-- bottom line & bottom right corner -->
<path android:pathData="M 60 370 h275 c20 0 35 -15 35 -35"
android:strokeColor="#000000" android:strokeWidth="20" />
<!-- right line & top right corner -->
<path android:pathData="M 370 336 v -271 c0 -20 -15 -35 -35 -35"
android:strokeColor="#000000" android:strokeWidth="20" />
</vector>
custom_check_box_selected.xml
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="20dp"
android:width="20dp"
android:viewportWidth="400"
android:viewportHeight="400">
<!-- the outside box -->
<path android:pathData="M 340 30 H 62 c -20 0 -35 15 -35 35
v271 c0 20 15 35 35 35
h275 c20 0 35 -15 35 -35
v -271 c0 -20 -15 -35 -35 -35 "
android:fillColor="#FDD835" android:strokeColor="#000000" android:strokeWidth="20" />
<!-- the check mark -->
<path android:pathData="M 140 320 l -100 -100 25 -30
l 75 75 l 190 -190
l 25 30 l -190 190"
android:fillColor="#000000" android:strokeColor="#000000" android:strokeWidth="2" />
</vector>