Android 如何将单击侦听器添加到ListFragment空白区域
更新: MyActivity.javaAndroid 如何将单击侦听器添加到ListFragment空白区域,android,fragment,onclicklistener,android-listfragment,onitemclicklistener,Android,Fragment,Onclicklistener,Android Listfragment,Onitemclicklistener,更新: MyActivity.java public class MyActivity extends FragmentActivity implements AdapterView.OnItemClickListener, View.OnClickListener { private ListView mListView; private ViewGroup mParent; private SimpleArrayAdapter mAdapter; @
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;
}
}
}
公共类MyActivity扩展了FragmentActivity实现了AdapterView.OnItemClickListener、View.OnClickListener{
私有列表视图;
私人视窗组;
私人Simplearlayadapter mAdapter;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mListView=(ListView)findViewById(android.R.id.list);
mListView.setOnItemClickListener(此);
mAdapter=新的SimpleRayaAdapter(本);
mAdapter.add(System.currentTimeMillis()+);
mListView.setAdapter(mAdapter);
mParent=(ViewGroup)findviewbyd(android.R.id.content);
mParent.setOnClickListener(this);
}
@凌驾
公共void onClick(视图v){
Toast.makeText(MyActivity.this,v.getClass().getName(),100).show();
}
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
Toast.makeText(MyActivity.this,position+“”+view.getClass().getName(),100.show();
}
类SimpleArrayAdapter扩展了ArrayAdapter{
公共SimpleRayaAdapter(上下文){
这(上下文,android.R.layout.simple\u list\u item\u 1);
}
私有SimpleArrayaAdapter(上下文上下文,int textViewResourceId){
super(上下文,textViewResourceId);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
if(convertView==null){
convertView=newtextView(getContext());
convertView.setLayoutParams(新的AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_父项,AbsListView.LayoutParams.MATCH_父项));
}
(TextView)convertView.setText(getItem(position));
返回视图;
}
}
}
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 >
<?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>
AndroidManifest.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 >
<?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。
但是如何在空白区域添加点击侦听器?
如下图所示
只需将您的
列表视图
添加到一些父视图
,如线性布局
或相对布局
,并为该父视图实现单击侦听器
这样,当您单击ListViewItem
时,将调用setOnItemClickListener(listener)
,单击其他位置将调用parent.setOnClickListener(listener)
谢谢@Archie.bpgc。你的意思是(视图组)(getListView().getParent()).setOnClickListener()?是的。你可以做到。但是请确保您的列表视图
有父视图。哦,但它不起作用。我在onResume()ViewGroup listContainer=(ViewGroup)getListView().getParent()中添加了一些代码;setOnClickListener(这个);请张贴您的布局代码。然后尝试在FrameLayoutandroid:id=“@android:id/primary”