【问题标题】:Clicklistener for View Group AndroidView Group Android 的 Clicklistener
【发布时间】:2015-05-20 13:29:04
【问题描述】:

我正在使用 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:

<?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>

【问题讨论】:

  • @XaverKapeller 感谢您的回复。我正在使用上面的代码,但视图组不可点击。当我点击它时没有任何反应。请在这里帮助我
  • 你为什么使用addFooterView(View, Object, boolean)?只需使用addFooterView(View)。我认为这是错误的根源。或者更具体地说是最后的false,但我不确定,试试addFooterView(View)
  • @XaverKapeller 感谢您的回复。请看我上面编辑的代码,我已经按照你的说明进行了更改,但仍然无法正常工作。

标签: android android-listview android-viewgroup


【解决方案1】:

我不知道如何将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 事件。

不过如果你还想给整个footer设置一个onClickListener,你可以通过我上面提到的方法获取footer的布局,然后给这个布局设置一个onClickListener。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-23
    • 1970-01-01
    • 2014-09-30
    相关资源
    最近更新 更多