[英]How to add click listener in listview footer view button
我已经创建了一个带有列表视图的活动,并在语法上添加了页脚视图。 页脚视图包含两个按钮(后退和下一个)。 问题是,当我单击按钮时,它不会触发点击事件,但是当我专注于View(footerview)时,它会触发点击事件。
这是我的代码,
listView.addFooterView(footer_controls,null,true); Button NextButton =(Button) footer_controls.findViewById(R.id.btnNext); NextButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Toast.makeText(context, "forward clicked", Toast.LENGTH_LONG).show(); LoadData(); } });
提前致谢。
在添加页脚视图之前,请编写OnclickListener并确保将xml内的focusable =“ false”设置为根布局
在用于使footer_controls膨胀的xml布局中,尝试将android:descendantFocusability="blocksDescendants"
放在最高的根视图上
例如
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants"
android:orientation="vertical" >
<Button ...>
<!--Other Views -->
</LinearLayout>
尝试以下对我有用的代码...无需持有人调用您的按钮,您可以直接找到viewbyid,如下
listSearchResult = (ListView) findViewById(R.id.listSearchResult);
footerView1 = View.inflate(this, R.layout.no_listing_found, null);
listSearchResult.addFooterView(footerView1,null,false);
Button NextButton =(Button) findViewById(R.id.btnNext);
NextButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "forward clicked", Toats.LENGTH_LONG).show();
LoadData();
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.