我通过在我的 Android 项目中添加库和双向网格视图来实现双向网格视图库。
XML:
<com.jess.ui.TwoWayGridView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#000000"
android:id="@+id/listView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
app:cacheColorHint="#E8E8E8"
app:columnWidth="80dp"
app:rowHeight="80dp"
app:numColumns="3"
app:numRows="auto_fit"
app:verticalSpacing="16dp"
app:horizontalSpacing="16dp"
app:stretchMode="spacingWidthUniform"
app:scrollDirectionPortrait="horizontal"
app:scrollDirectionLandscape="horizontal"
app:gravity="left" />
Java 代码:
//set up adapter
CustomCursorAdapter dataAdapter = new CustomCursorAdapter(context, cursor);
TwoWayGridView gridView = (TwoWayGridView) findViewById(R.id.listView1);
//assign adapter to ListView
gridView.setAdapter(dataAdapter);
有时候是这样的:
- 网格可以水平滚动 - 耶!
- 所有项目都
TextViews
具有相同的元素(第一项) - 不是。 - 网格中没有可点击的项目 -
setOnItemClickListener
不兼容 - 不是。
日食错误:
TwoWayAdapterView 类型中的方法 setOnItemClickListener(TwoWayAdapterView.OnItemClickListener) 不适用于参数(新的 AdapterView.OnItemClickListener(){})
在这段代码上:
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> listView, View view, int position, long id) {
//get the cursor, positioned to the corresponding row in the result set
Cursor cursor = (Cursor) listView.getItemAtPosition(position);
//get the media file from this row in the database
String fileName = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.FIELD_RESOURCE));
String suffix = ".jpg";
if(fileName.equalsIgnoreCase("null")){
//Text item so do stuff for that
}else if(fileName.endsWith(suffix)){
//Image item so do stuff for that
}else {
//Video item so do stuff for that
}
}
});
我应该为双向网格视图编写自定义 onItemClickListener 吗?