我创建了一个自定义列表视图,每个元素都有一个webview。
我的问题是,当我点击一个元素时,它会占用坏元素 - 位置。
这是我的自定义列表视图
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;
}
我的webview点击监听器
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);
}
感谢您的帮助
编辑:我发现了一些奇怪的东西。如果在我的listview.xml中我改变了我的webview的高度大小,当我点击一个元素时,它会改变元素的位置
<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>
如果我有5个元素,那么这个顺序就是:2 1 0 2 1
我真的不知道问题在哪里
答案 0 :(得分:3)
在getView()
中,您将触摸侦听器设置为持有者中的视图。该代码仅针对列表中的第一个可见元素进行调用(convertView
为null
时)
您应该在返回convertView
之前将侦听器设置为当前项目,这样您的代码应如下所示:
@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);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.mywebviewholder
.setOnTouchListener(new my.student.application.WebViewClickListener(
holder.mywebviewholder, parent, position));
holder.mywebviewholder.loadDataWithBaseURL("", mywebview.get(position)
.toString(), "text/html", "UTF-8", "");
return convertView;
}
持有者的想法是避免在每个findViewByID中搜索xml。