问题
I've implemented the two-way-gridview library by adding libs and two-way-gridview to my Android project.
The 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" />
The Java code:
//set up adapter
CustomCursorAdapter dataAdapter = new CustomCursorAdapter(context, cursor);
TwoWayGridView gridView = (TwoWayGridView) findViewById(R.id.listView1);
//assign adapter to ListView
gridView.setAdapter(dataAdapter);
This happens:
- Grid can scroll horizontally - yay!
- All items are
TextViews
with the same element (the first item) - not yay. - No item in the grid is clickable - the
setOnItemClickListener
is not compatible - not yay.
Eclipse error:
The method setOnItemClickListener(TwoWayAdapterView.OnItemClickListener) in the type TwoWayAdapterView is not applicable for the arguments (new AdapterView.OnItemClickListener(){})
on this piece of code:
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
}
}
});
Should I write a custom onItemClickListener for the two-way-gridview?
回答1:
You should probably just change :
gridView.setOnItemClickListener(new OnItemClickListener() {}
by
gridView.setOnItemClickListener(new TwoWayAdapterView.OnItemClickListener() {}
来源:https://stackoverflow.com/questions/18076104/android-twowaygridview-onitemclicklistenter-not-working