We have a GridView with a bunch of items in it, so much so that the user needs to scroll down to see them all. Each item in the GridView is composed by a Textview and a Webview with some events attached to them.
Problem: OnItemLongClickListener is not fired for items that are not visible when the Activity is first created (the ones you need to scroll down to see).
Relevant code:
Activity --
public class MyCoursesActivity extends ActivityBase implements ILoginListener, IUpdateListener, OnDragListener {
private ArrayList<Course> myCoursesList = new ArrayList<Course>();
private int draggedIndex = -1;
CoursesListAdapter coursesAdapter = new CoursesListAdapter(MyCoursesActivity.this, myCoursesList);
MyCourseAdapter mca = new MyCourseAdapter();
private void Load() throws ParseException
{
myCoursesList = mca.Load();
coursesAdapter = new CoursesListAdapter(MyCoursesActivity.this, myCoursesList);
final GridView gv = (GridView) findViewById(R.id.mycourses_Grid);
gv.setAdapter(coursesAdapter);
gv.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView gridView, View view,
int position, long row) {
view = gridView.getChildAt(position);
ClipData.Item item = new ClipData.Item("");
ClipData clipData = new ClipData("",
new String[] { ClipDescription.MIMETYPE_TEXT_PLAIN }, item);
view.startDrag(clipData, new View.DragShadowBuilder(view), null, 0);
View trashCan = findViewById(R.id.trash_can);
trashCan.setVisibility(View.VISIBLE);
trashCan.setOnDragListener(MyCoursesActivity.this);
draggedIndex = position;
return true;
}
});
}
public boolean onDrag(View view, DragEvent dragEvent) {
switch (dragEvent.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
if (view.getId() == R.id.trash_can) {
view.animate().scaleX(1.0f);
view.animate().scaleY(1.0f);
return true;
} else
if (dragEvent.getClipDescription().hasMimeType(
ClipDescription.MIMETYPE_TEXT_PLAIN)) {
return true;
} else {
return false;
}
case DragEvent.ACTION_DRAG_ENTERED:
if (view.getId() == R.id.trash_can) {
view.animate().scaleX(1.5f);
view.animate().scaleY(1.5f);
}
return true;
case DragEvent.ACTION_DRAG_EXITED:
if (view.getId() == R.id.trash_can) {
view.animate().scaleX(1.0f);
view.animate().scaleY(1.0f);
}
view.invalidate();
return true;
case DragEvent.ACTION_DRAG_LOCATION:
return true;
case DragEvent.ACTION_DROP:
if (view.getId() == R.id.trash_can) {
//do stuff
}
coursesAdapter.notifyDataSetChanged();
case DragEvent.ACTION_DRAG_ENDED:
// Hide the trash can
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
findViewById(R.id.trash_can).setVisibility(View.INVISIBLE);
}
}, 1000l);
if (view.getId() == R.id.trash_can) {
view.animate().scaleX(1.0f);
view.animate().scaleY(1.0f);
} else {
view.setVisibility(View.VISIBLE);
}
// remove drag listeners
view.setOnDragListener(null);
return true;
}
return false;
}
}
GridView Adapter --
public class CoursesListAdapter extends BaseAdapter {
private Context context;
private ArrayList<Course> courses;
public CoursesListAdapter(Context context, ArrayList<Course> courses){
this.context = context;
this.courses = courses;
}
<@Override getCount etc.>
@Override
public View getView(int position, View convertView, ViewGroup parentView) {
final Course course = (Course) getItem(position);
LayoutInflater inflater = LayoutInflater.from(context);
convertView = inflater.inflate(R.layout.course_view, parentView, false);
if(context instanceof OnDragListener){
convertView.setTag(course);
convertView.setOnDragListener((OnDragListener) context);
}
TextView title = (TextView) convertView.findViewById(R.id.courseview_titleTv);
title.setText(course.getTitle());
WebView wv = (WebView) convertView.findViewById(R.id.courseview_imageWv);
wv.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View arg0, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP){
Intent myIntent = new Intent(context, CourseDetailsActivity.class);
myIntent.putExtra("Course", course);
context.startActivity(myIntent);
return true;
}
return false;
}
});
convertView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent myIntent = new Intent(context, CourseDetailsActivity.class);
myIntent.putExtra("Course", course);
context.startActivity(myIntent);
}
});
return convertView;
}
}
Any idea? Thank you.
EDIT: Tried to put ImageViews instead of WebViews and there doesn't seem to be a problem with events anymore. We tried with a fixed drawable. We're trying to get the actual content of webview to be displayed in the ImageView on project of its own to ease troubleshooting. Here's the code so far. The ImageView doesn't display any image (e.g. google doodle)
WebView to ImageView --
final WebView mWeb = (WebView) findViewById(R.id.webView1);
//final WebView mWeb = new WebView(this);
final ImageView imgView = (ImageView) findViewById(R.id.imageView1);
mWeb.setInitialScale(100);
mWeb.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView v, String url){
Picture picture = mWeb.capturePicture();
Bitmap bitmap = Bitmap.createBitmap(picture.getWidth(), picture.getHeight(), Bitmap.Config.ARGB_8888);
final Canvas c =new Canvas(bitmap);
picture.draw(c);
imgView.setMinimumWidth(bitmap.getWidth());
imgView.setMinimumHeight(bitmap.getHeight());
imgView.setImageBitmap(bitmap);
imgView.setVisibility(View.VISIBLE);
}
});
mWeb.loadUrl("http://www.google.com");