I have created a custom listview that have one webview per element.
My problem is that when I click on an element, it takes the bad element-positon.
Here is my custom listview
public class ListViewCustomAdapter extends BaseAdapter {
public ArrayList<String> mywebview;
public Activity context;
public LayoutInflater inflater;
public ListViewCustomAdapter(Activity context, ArrayList<String> mywebview) {
//super();
this.mywebview = new ArrayList<String>();
this.context = context;
this.mywebview = mywebview;
this.inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return mywebview.size();
}
@Override
public String getItem(int position) {
return mywebview.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public static class ViewHolder {
WebView mywebviewholder;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.mylistview2, null, false);
holder.mywebviewholder = (WebView) convertView
.findViewById(R.id.weblistview);
holder.mywebviewholder
.setOnTouchListener(new my.student.application.WebViewClickListener(
holder.mywebviewholder, parent, position));
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.mywebviewholder.loadDataWithBaseURL("", mywebview.get(position)
.toString(), "text/html", "UTF-8", "");
return convertView;
}
my webview click listener
public class WebViewClickListener implements View.OnTouchListener {
private int position;
private ViewGroup vg;
private WebView wv;
public WebViewClickListener(WebView wv, ViewGroup vg, int position) {
this.vg = vg;
this.position = position;
this.wv = wv;
}
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_CANCEL:
return true;
case MotionEvent.ACTION_UP:
sendClick();
return true;
}
return false;
}
public void sendClick() {
ListView lv = (ListView) vg;
lv.performItemClick(wv, position, 0);
}
Thanks for your help
Edit: Something weird I found out. If in my listview.xml I change the height size of my webview, when I click on an element, It changes the position of the element
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="vertical" android:padding="3dip">
<WebView android:id="@+id/weblistview" android:layout_width="fill_parent"
android:layout_height="200dip"></WebView>
With this heigh the order if I have 5 elements is: 2 1 0 2 1
I really don’t see where is the problem
In
getView(), you are setting the touch listener to the view in the holder. That code only gets called for the first visible elements in the list (whenconvertViewisnull)You should be setting the listener to the current item right before returning the
convertViewso your code should look like this:The idea with the holder is to avoid searching through the xml for every findViewByID.