0

更新 :

我的活动.java

    public class MyActivity extends FragmentActivity implements AdapterView.OnItemClickListener, View.OnClickListener {
    private ListView mListView;
    private ViewGroup mParent;
    private SimpleArrayAdapter mAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mListView = (ListView) findViewById(android.R.id.list);
        mListView.setOnItemClickListener(this);
        mAdapter = new SimpleArrayAdapter(this);
        mAdapter.add(System.currentTimeMillis() + "");
        mListView.setAdapter(mAdapter);

        mParent = (ViewGroup) findViewById(android.R.id.content);
        mParent.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Toast.makeText(MyActivity.this, v.getClass().getName(), 100).show();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Toast.makeText(MyActivity.this, position + " " + view.getClass().getName(), 100).show();
    }

    class SimpleArrayAdapter extends ArrayAdapter<String> {
        public SimpleArrayAdapter(Context context) {
            this(context, android.R.layout.simple_list_item_1);
        }

        private SimpleArrayAdapter(Context context, int textViewResourceId) {
            super(context, textViewResourceId);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = new TextView(getContext());
                convertView.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.MATCH_PARENT));
            }
            ((TextView) convertView).setText(getItem(position));
            return convertView;
        }
    }
}

主要的.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
             android:id="@android:id/content"
             android:layout_width="fill_parent"
             android:layout_height="fill_parent"
        >
    <ListView android:id="@android:id/list"
              android:background="@android:color/white"
              android:layout_height="match_parent"
              android:layout_width="match_parent"/>
</LinearLayout >

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.EditListViewDemo"
          android:versionCode="1"
          android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17"/>
    <application android:label="@string/app_name" android:icon="@drawable/ic_launcher">
        <activity android:name="MyActivity"
                  android:label="@string/app_name"
                  android:windowSoftInputMode="adjustPan">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>
</manifest>

问题: 我可以使用 getListView().setOnItemClickListener(listener) 到 ListFragment ListView。但是如何将点击监听器添加到空白区域?如下图所示。

在此处输入图像描述

4

1 回答 1

1

只需将您的添加ListView到一些Parent View,如 aLinearLayout或 a RelativeLayout,并clickListener为此实现Parent

这样,当您单击时ListViewItemsetOnItemClickListener(listener)会调用 ,而单击其他地方将调用parent.setOnClickListener(listener)

于 2013-02-21T07:15:04.087 回答