【问题标题】:Android: ListView with only one textview in each row cannot be clickedAndroid:无法点击每行只有一个textview的ListView
【发布时间】:2014-01-04 08:26:34
【问题描述】:

我无法点击列表中的任何项目。我的活动扩展了ListActivity,并且我已经实现了OnItemClickListener 并将其设置到列表中。我已经研究了这个错误,人们在listView 的多个视图中遇到了类似的问题,所以通过将focusable 设置为false 或android:descendantFocusability="blocksDescendants".. 通常解决了他们的问题.. 我的问题没有得到解决。

     protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

     movieListView = getListView(); // get the built-in ListView
     movieListView.setOnItemClickListener(viewMovieListener); 

     movieListView.setClickable(true);

     String[] from = new String[] { "name" };
     int[] to = new int[] { R.id.categoryTextView };
     movieAdapter = new SimpleCursorAdapter( MovieList.this, R.layout.activity_movie_list, null, from, to);

     value = getIntent().getIntExtra(MainActivity.CATEGORY, -1);
     if(value == 1)
         category = "Family";
     else if(value == 2)
         category = "Horror";
     else if(value == 3)
         category = "Comedy";

}


 OnItemClickListener viewMovieListener = new OnItemClickListener() 
   {
      @Override
      // This doesnt get called? no it doesnt
      public void onItemClick(AdapterView<?> arg0, View arg1, int position,
         long id) 
      {
         // create an Intent to launch the ViewContact Activity
         Intent viewMovie = 
            new Intent(MovieList.this, ViewMovie.class);

         // pass the selected contact's row ID as an extra with the Intent
         viewMovie.putExtra(ROW_ID, id);
         startActivity(viewMovie); // start the ViewContact Activity
      } 
   };

我的xml:

     <?xml version="1.0" encoding="utf-8"?>
     <TextView xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/categoryTextView"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="TextView"
      android:focusable = "false"
      android:descendantFocusability="blocksDescendants"
      android:clickable="true"
      ></TextView>

【问题讨论】:

  • 如果你删除android:clickable="true"会发生什么?
  • 它没有任何区别,.. 因为我无法点击它.. 我做到了.. 但什么也没发生

标签: android listview android-listview textview


【解决方案1】:

在ListActivity中,可以覆盖onListItemClick来点击Item

 @Override
 public void onListItemClick(ListView l, View v, int position, long id) {
     Log.i("example",position);

 }

【讨论】:

  • 应该自动调用吗?
  • 是的,当你点击item时它会被调用
  • 可以看这个教程developer.android.com/reference/android/app/…, android.view.View, int, long)
  • 删除这个再试一次? android:focusable = "false" android:descendantFocusability="blocksDescendants" android:clickable="true"
  • 我不确定,但我从不使用它。也许你可以试试。
【解决方案2】:

试试这个

    android:focusable="false"
    android:focusableInTouchMode="false"

【讨论】:

  • 这没有提供问题的答案。要批评或要求作者澄清,请在其帖子下方发表评论。
  • @JacekLaskowski 如果这段代码解决了他的问题,那么它就是答案。那就不用评论了。
  • 格式化和试试这个看起来像是一个澄清请求。更具体并提供完整的解决方案,以便答案独立。
  • 是的,我知道格式不是很好,很抱歉。但根据我的观点,它是具体而完整的解决方案。
  • 您介意更正格式并添加更多信息,以便答案吸引更多眼球吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多