我正在开发一个打开epub文件的电子书阅读器。为了在目录之间创建链接,我创建了新的活动以显示所有这些。
Here is the picture of my MainActivity.class
从图像中我可以清楚地看到我使用webviews项创建gridview。通过webview加载电子酒吧页面是最简单的方法。
现在,由于webview的可点击功能,我在此活动中遇到了大麻烦,因此gridview无法响应方法setOnItemClickListener。
现在我正在分享我的代码:
MainActivity.class 和 activity_main.xml
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setColumnWidth(width / 2);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// Send intent to SingleViewActivity
Intent i = new Intent(getApplicationContext(), MainActivity.class);
// Pass image index
i.putExtra("position", position);
startActivity(i);
}
});
<GridView
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="90dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="2"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" />
&#13;
最后是我的 WebViewAdapter.class
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import static com.tehedligmail.e_bookaz.LookAtAllChapters.height;
import static com.tehedligmail.e_bookaz.LookAtAllChapters.tinyDB;
import static com.tehedligmail.e_bookaz.LookAtAllChapters.width;
public class WebViewAdapter extends BaseAdapter {
private Context mContext;
// Constructor
public WebViewAdapter(Context c) {
mContext = c;
}
@Override
public int getCount() {
return tinyDB.getListString("BookSections").size();
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new WebView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
final WebView SingleWebView;
if (convertView == null) {
SingleWebView = new WebView(mContext);
GridView.LayoutParams GridviewLayoutParams = new GridView.LayoutParams(width / 2, height / 2);
SingleWebView.setLayoutParams(GridviewLayoutParams);
} else {
SingleWebView = (WebView) convertView;
}
SingleWebView.loadDataWithBaseURL("", tinyDB.getListString("BookSections").get(position), "text/html", "UTF-8", "");
return SingleWebView;
}
}
&#13;
任何建议都会受到欢迎,因为我已经调查了很多网站,请帮我解决问题。提前致谢。