2011-07-28 35 views
3

我創建了一個自定義列表視圖,每個元素具有一個webview。我的自定義列表視圖元素的位置順序的問題

我的問題是,當我點擊一個元素,它需要壞元素positon。

這裏是我的自定義列表視圖

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

我實在看不出哪裏出了問題

回答

3

getView(),你觸摸監聽器設置在支架上的視圖。該代碼只被調用列表中的第一個可見的元素(當convertViewnull

您應該設置聽者當前項權返回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搜索。

+0

啊,最後它的工作。非常感謝您的回答,現在看起來更簡單和邏輯。 – joris

相關問題