アップデート :
MyActivity.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;
}
}
}
main.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 に使用できます。しかし、空白の領域にクリックリスナーを追加する方法は? 次の写真に示すように。