Clicklistener for View Group Android



我正在使用ViewGroup在我的列表视图中显示我的页脚的android应用程序。现在我想为那个视图组创建一个监听器事件,这样用户就可以按下那个页脚。我的代码,使页脚和视图组随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 for footer:

<?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();
        }
    });

这样,你只分配按钮的onClick事件。

然而,如果你仍然想设置一个onClickListener为整个页脚,你可以得到页脚的布局,并使用我上面提到的方法设置onClickListener为这个布局

最新更新