tView控件setOnItemClickListener不开火

tView控件setOnItemClickListener不开火

本文介绍了在ListView控件setOnItemClickListener不开火的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到我的SD卡的文件列表,并在ListView像这样的自定义适配器的帮助下显示出来:

I get the list of files of my SD card and display it in the listView like with the help of custom adapter like that:

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或类似的东西听的检测项目单击事件。我的onCreate()在活动方式:

I want to implement setonitemclicklistener or something like that to listen to the detect items click events. My onCreate() method in Activity:

    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();
          }
    });

}

我的活动的XML:

My xml of Activity:

  <?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活动

And my CheckedTextView Activity

   <?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>

但是,当我点击的项目,没有任何反应。我试图让setOnItemClickListener在onResume()方法,但有相同的效果。我也试过onclicklistener - 同样的效果。这有什么原因吗?

But when I click on items, nothing happens. I tried to make setOnItemClickListener in the onResume() method, but had the same effect. I also tried onclicklistener - the same effect. What's the reason of it?

推荐答案

在我看来,有些观点正在从你的列表视图的焦点,当你知道它查看它,使用的android:可聚焦=FALSE在XML中的这一观点,它应该做的伎俩。

It seems to me that some view is taking the focus from your list view, when you know which view it is, use android:focusable="false" on that view in your xml, and it should do the trick.

我想你的code和onItemClicked被调用,这是我getView():

I tried your code and onItemClicked is being called, this is my 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):

How I set the adapter (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控件setOnItemClickListener不开火的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 21:56