视图组的 Clicklistener Android
Clicklistener for View Group Android
我正在开发 android 应用程序,我在其中使用 ViewGroup 在列表视图中显示我的页脚。现在我想为该视图组创建一个侦听器事件,以便用户可以按该页脚。下面给出了我制作该页脚和视图组的代码以及 xml.
ViewGroup footer = (ViewGroup) getLayoutInflater().inflate(R.layout.footer_view, mListView, false);
View header = getLayoutInflater().inflate(R.layout.footer_view, null);
Button headerButton = (Button)header.findViewById(R.id.footerRefreshBtn);
mListView.addFooterView(footer);
headerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "I am clicked", Toast.LENGTH_LONG).show();
}
});
// It is not showing toast message on click.
XML 对于页脚:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:orientation="vertical" >
<Button
android:id="@+id/footerRefreshBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:layout_gravity="center_horizontal"
android:text="Refresh Button"
android:textColor="@color/white"
android:background="@color/gray" />
</LinearLayout>
我找不到如何 setOnClickListener
页脚,但我找到了不同的解决方案。我认为您可以使用它,只需在添加页脚视图之前或之后找到 footerRefreshBtn
并设置按钮的 OnClickListener
即可。两种方式都有效。
LayoutInflater inflater = getLayoutInflater();
ViewGroup footer = (ViewGroup) inflater.inflate(R.layout.footer, mListView, false);
mListView.addFooterView(footer, null, false);
mListView.setAdapter(mAdapter);
Button footerRefreshBtn = (Button) footer.findViewById(R.id.footerRefreshBtn);
footerRefreshBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "I am clicked", Toast.LENGTH_LONG).show();
}
});
这样,您只需分配 Button 的 onClick 事件。
不过,如果您仍想为整个页脚设置一个 onClickListener,您可以使用我上面提到的方法获取页脚的布局并为此布局设置一个 onClickListener。
我正在开发 android 应用程序,我在其中使用 ViewGroup 在列表视图中显示我的页脚。现在我想为该视图组创建一个侦听器事件,以便用户可以按该页脚。下面给出了我制作该页脚和视图组的代码以及 xml.
ViewGroup footer = (ViewGroup) getLayoutInflater().inflate(R.layout.footer_view, mListView, false);
View header = getLayoutInflater().inflate(R.layout.footer_view, null);
Button headerButton = (Button)header.findViewById(R.id.footerRefreshBtn);
mListView.addFooterView(footer);
headerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "I am clicked", Toast.LENGTH_LONG).show();
}
});
// It is not showing toast message on click.
XML 对于页脚:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:orientation="vertical" >
<Button
android:id="@+id/footerRefreshBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:layout_gravity="center_horizontal"
android:text="Refresh Button"
android:textColor="@color/white"
android:background="@color/gray" />
</LinearLayout>
我找不到如何 setOnClickListener
页脚,但我找到了不同的解决方案。我认为您可以使用它,只需在添加页脚视图之前或之后找到 footerRefreshBtn
并设置按钮的 OnClickListener
即可。两种方式都有效。
LayoutInflater inflater = getLayoutInflater();
ViewGroup footer = (ViewGroup) inflater.inflate(R.layout.footer, mListView, false);
mListView.addFooterView(footer, null, false);
mListView.setAdapter(mAdapter);
Button footerRefreshBtn = (Button) footer.findViewById(R.id.footerRefreshBtn);
footerRefreshBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "I am clicked", Toast.LENGTH_LONG).show();
}
});
这样,您只需分配 Button 的 onClick 事件。
不过,如果您仍想为整个页脚设置一个 onClickListener,您可以使用我上面提到的方法获取页脚的布局并为此布局设置一个 onClickListener。