【问题标题】:setOnItemClickListener on ListView doesn't firingListView 上的 setOnItemClickListener 没有触发
【发布时间】:2012-08-28 12:16:47
【问题描述】:

我得到了我的 SD 卡的文件列表并在 listView 中显示它,就像在自定义适配器的帮助下一样:

adapter = new ArrayAdapter<Item>(this,
            R.layout.file_manager, R.id.checkedTextItem,
            fileList) 
            {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // creates view
            LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view = inflater.inflate(R.layout.check_item, null);

            CheckedTextView textView = (CheckedTextView) view
                    .findViewById(R.id.checkedTextItem);

            // put the image on the text view
            textView.setCompoundDrawablesWithIntrinsicBounds(
                    fileList[position].icon, 0, 0, 0);

            textView.setTextColor(Color.WHITE);
            textView.setText(fileList[position].file);
            if(fileList[position].icon == R.drawable.directory_icon)
                textView.setCheckMarkDrawable(null);


            // add margin between image and text (support various screen
            // densities)
            int dp5 = (int) (5 * getResources().getDisplayMetrics().density + 0.5f);
            textView.setCompoundDrawablePadding(dp5);

            return view;

        }
    };

我想实现 setonitemclicklistener 或类似的东西来监听检测项目的点击事件。我在 Activity 中的 onCreate() 方法:

    public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
     setContentView(R.layout.file_manager);
    lv = (ListView)findViewById(R.id.fileManagerList);
    loadFileList();
    file_list = findViewById(R.id.filesList);
    lv.setAdapter(adapter);

    lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
          public void onItemClick(AdapterView<?> myAdapter, View myView, int myItemInt, long mylng) {
            //String selectedFromList = (lv.getItemAtPosition(myItemInt).toString());
            Toast toast = Toast.makeText(getApplicationContext(), "Hello world!", Toast.LENGTH_LONG);
            toast.show();
          }                 
    });

}

我的 Activity xml:

  <?xml version="1.0" encoding="UTF-8"?>

 <RelativeLayout

              xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/fileManager"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical"
               >
    <ListView

        android:background="#000000"
        android:focusable="false"
        android:id="@+id/fileManagerList"
        android:layout_width="fill_parent" 
         android:layout_above="@+id/closecalmlayout"      
        android:layout_height="wrap_content" >
    </ListView>


         <LinearLayout
            android:id="@+id/closecalmlayout"
            android:layout_alignParentBottom="true"
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content"
            android:weightSum="1.0" >

              <Button
                  android:id="@+id/btnOk"
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent" 
                  android:layout_marginLeft="5dip"
                  android:layout_marginTop="5dip"
                  android:layout_weight=".50"
                  android:text="Attach files"
                  />

              <Button
                  android:id="@+id/btnCancel"
                    android:layout_width="fill_parent"
                  android:layout_height="fill_parent" 
                  android:layout_marginLeft="5dip"
                  android:layout_marginRight="5dip"
                  android:layout_marginTop="5dip"
                  android:layout_weight=".50"
                  android:text="Do not attach"
                   />

            </LinearLayout>
</RelativeLayout>

还有我的 CheckedTextView 活动

   <?xml version="1.0" encoding="UTF-8"?>
    <LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp">

      <CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android" 
             android:id="@+id/checkedTextItem" 
             android:layout_width="fill_parent" 
             android:layout_height="wrap_content" 
             android:gravity="center_vertical" 
             android:checkMark="?android:attr/listChoiceIndicatorMultiple"
             android:textColor="#000000"
             android:focusable="false"
             android:paddingLeft="10dip" 
             android:paddingRight="6dip" 
             android:typeface="sans" android:textSize="16dip"/> 

</LinearLayout>

但是当我点击项目时,什么也没有发生。我尝试在 onResume() 方法中设置 setOnItemClickListener,但效果相同。我也试过 onclicklistener - 同样的效果。这是什么原因?

【问题讨论】:

  • 似乎列表视图中的某些东西成为焦点
  • 可能是CheckedTextView,试试把它去掉看看能不能用!
  • 你想从列表中获取点击项,我是对的。

标签: android listview actionlistener onclicklistener onitemclicklistener


【解决方案1】:

在我看来,某些视图正在从您的列表视图中获取焦点,当您知道它是哪个视图时,请在 xml 中的该视图上使用 android:focusable="false",它应该可以解决问题。

我尝试了您的代码,并且正在调用 onItemClicked,这是我的 getView():

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if(view == null){
            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = vi.inflate(R.layout.row, null);
        }

        CheckedTextView textView = (CheckedTextView) view.findViewById(R.id.checked);
        textView.setText("Hello"); //test, you can do whatever you want with this

        int dp5 = (int) (5 * getResources().getDisplayMetrics().density + 0.5f);
        textView.setCompoundDrawablePadding(dp5);

        return view;
    }

我如何设置适配器(MyAdapter):

MyAdapter adapter = new MyAdapter(this, 0, arrayList);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {

        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            // TODO Auto-generated method stub
            Toast.makeText(PlayingAroundActivity.this, "Hello", Toast.LENGTH_LONG).show();
        }                 
    });

【讨论】:

  • 我试着用我的 listView 来做,也用 CheckedTextView,但它没有帮助......
  • android:focusable 不应该被列表视图使用,而是在每一行的焦点视图中使用。你还有什么?
  • 我怎样才能知道现在正在使用什么视图?
  • 我可以看到你在每一行中只有“TextView”和“CheckedTextView”,对吧?或者你还有什么别的吗?
  • 我只有 CheckedTextView - textView 变量....我尝试添加 textView.setFocusable(false) 并在 xml 中向此项添加相等参数,但它不起作用...
【解决方案2】:

从列表视图中获取所选项目。

 lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
          public void onItemClick(AdapterView<?> myAdapter, View myView, int myItemInt, long mylng) {
            //String selectedFromList = (lv.getItemAtPosition(myItemInt).toString());
            Toast toast = Toast.makeText(getApplicationContext(), "Hello world!"+fileList(myItemInt), Toast.LENGTH_LONG);
            toast.show();
          }                 
    });

【讨论】:

  • 我用的是同样的方法,你可以在我帖子的onCreate()方法中看到。它对我不起作用。
【解决方案3】:

@覆盖 公共布尔areAllItemsEnabled(){ 返回; //如果使用 ListAdapter,则返回 true }

@Override
public boolean isEnabled(int position) {
    return **true**;  //return True  if using ListAdapter
}

【讨论】:

    猜你喜欢
    • 2015-12-17
    • 1970-01-01
    • 2011-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-23
    • 1970-01-01
    • 2011-06-10
    相关资源
    最近更新 更多