ListItem中的WebView - >项目无法点击| Android的

时间:2013-12-28 05:35:40

标签: android listview android-webview

我已经读过在ListView中使用WebView的一个坏主意,但我必须这样做,因为我有用JavaScript提供的公式。

现在我遇到了WebView'捕获'点击的问题,虽然它被设置为notFocusable(也不是xml以编程方式改变任何东西)。 因此,ListItem几乎不可点击,因为WebView几乎填满整个ListItem ......

如何使用可点击的WebView获取ListItem?

3 个答案:

答案 0 :(得分:2)

此网页有一个非常好的答案:http://jaydomagala.blogspot.co.il/2010/12/handling-onclick-in-listview-with.html

总结代码: 设置webview以满足您的需求

<linearlayout android:background="#ffffff" android:layout_height="wrap_content" android:layout_width="fill_parent" android:padding="4dip" xmlns:android="http://schemas.android.com/apk/res/android">

 <webview android:id="@+id/book_display" android:layout_height="fill_parent" android:layout_marginright="10dip" android:layout_width="75dip">  

<linearlayout android:background="#ffffff" android:layout_height="wrap_content" android:layout_width="fill_parent" android:orientation="vertical">
    <textview android:id="@+id/toptext" android:layout_height="wrap_content" android:layout_width="fill_parent" android:textcolor="#000000">
     <textview android:id="@+id/middletext" android:layout_height="wrap_content" android:layout_width="fill_parent" android:textcolor="#000000">
     <textview android:id="@+id/bottomtext" android:layout_height="wrap_content" android:layout_width="fill_parent" android:textcolor="#000000"> 
    <textview android:id="@+id/moreinfo_ext" android:layout_gravity="right" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="@string/more_info_text" android:textcolor="#000000">           
</textview></textview></textview></textview></linearlayout>

这是正常的ListView点击的样子:

bookListView.setOnItemClickListener(new OnItemClickListener() {    
  public void onItemClick(AdapterView parent, View view, 
    int position, long id) {

   Intent myIntent = new Intent(view.getContext(), BookDetailApp.class);

   //save serialized book object to pass to BookDetailApp
   Bundle bBooks = new Bundle();    
   currentBook = aBooks.get(position);
   bBooks.putSerializable("currentBook", currentBook);
   myIntent.putExtras(bBooks);

   startActivityForResult(myIntent, 0);        
  }  
 }); 

解决方案: 处理Webview ontouch:

// Get a handle to our current WebView
    WebView wv = (WebView) row.findViewById(R.id.book_display);

    // Load it up with the properly formatted URL
    wv.loadUrl(url);

    // Set up its onTouchListener
    // This is essentially the same as an onClickListener
    // It just listens for more different types of input
    wv.setOnTouchListener(new View.OnTouchListener() 
    {
     // The definition of your actual onClick-type method
              public boolean onTouch(View v, MotionEvent event) 
              {
               // Switching on what type of touch it was
                  switch (event.getAction()) 
                  {
                   // ACTION_UP and ACTION_DOWN together make up a click
                   // We're handling both to make sure we grab it
                      case MotionEvent.ACTION_DOWN:
                      case MotionEvent.ACTION_UP:
                      {
                       // All of this is just like the previous Intent handler
// Do you own stuff here

                       Intent myIntent = new Intent(v.getContext(), BookDetailApp.class);

               //save serialized book object to pass to BookDetailApp
               Bundle bBooks = new Bundle();    
               currentBook = aBooks.get(bookListView.getPositionForView((View) v.getParent()));
               bBooks.putSerializable("currentBook", currentBook);
               myIntent.putExtras(bBooks);

               startActivityForResult(myIntent, 0);

               // The code will pause here until you exit the new Activity
               // It will then go back to what it was doing
               // In our case, waiting for more input
                          break;
                      }
                  }

                  // Returning false means that we won't be handling any other input
                  // Any un-handled gestures are tossed out
                  return false;
              }
          });

答案 1 :(得分:1)

在我的情况下,我必须设置

  

机器人:可聚焦= “假”   机器人:focusableInTouchMode = “假”

在WebView中

,一切正常。希望它有所帮助。

答案 2 :(得分:0)

我解决了这个问题:

webView.setFocusable(假);

webView正在从listView

中获得焦点