【问题标题】:Android - Checkbox not responding to clicks inside of FrameLayoutAndroid - 复选框不响应 FrameLayout 内的点击
【发布时间】:2018-04-17 13:58:24
【问题描述】:

我正在开发一个音乐播放器应用程序,并且我创建了一个 FrameLayout,它用作一张“卡片”,显示我正在尝试添加的每首歌曲的信息(专辑封面、标题、艺术家等)这张卡片中的复选框,我计划对其进行自定义以创建赞成和反对按钮。但是,卡片中的复选框不会响应点击。如果我在应用程序的主要活动中绘制一个复选框(我正在绘制卡片),它工作正常。

这里是卡片视图(“MusicCardView”)的onDraw()方法:

    protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);


    //Draw the voting controls
    try {

        CheckBox upvoteButton = new CheckBox(getContext());
        upvoteButton.setX(20);
        upvoteButton.setTop(-60);
        upvoteButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                // Is the button now checked?
                Log.d("MainActivity", "upvote clicked");
            }
        });
        CheckBox downvoteButton = new CheckBox(getContext());
        downvoteButton.setX(20);
        downvoteButton.setTop(-150);
        downvoteButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                // Is the button now checked?
                Log.d("MainActivity", "downvote clicked");
            }
        });

        upvoteButton.draw(canvas);
        downvoteButton.draw(canvas);

    } catch (Exception e) {
        Log.e("MusicCardView", "exception", e );
    }

}

主要活动的xml(“PartyActivity”):

    <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_activity_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.musique.PartyActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:id="@+id/main_linear_view"
        android:layout_marginBottom="56dp">
        <com.musique.MusicCardView
            android:layout_width="match_parent"
            android:id="@+id/now_playing_view"
            android:layout_height="80dp"
            android:background="#ccc"
            android:paddingBottom="5dp"
            android:paddingLeft="5dp"
            app:exampleColor="#000000"
            app:songTitle="Money"
            app:artist="Pink Floyd"
            app:album="The Dark Side of the Moon"
            app:exampleDimension="18sp"
            app:exampleDrawable="@drawable/darkside"
            app:exampleString="example" />
        <Space
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:id="@+id/controller_space"/>

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/query"
            android:layout_gravity="center"
            android:layout_marginTop="20dp" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/searchBtn"
            android:layout_gravity="center"
            android:layout_marginTop="20dp"
            android:text="Search"
            android:background="#5eca99"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tvData"/>
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:animateLayoutChanges="true"
            android:id="@+id/main_scroll_view">
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:id="@+id/scroll_child">

            </LinearLayout>
        </ScrollView>
    </LinearLayout>



    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="0dp"
        android:layout_marginStart="0dp"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/navigation" />


</android.support.constraint.ConstraintLayout>

MusicCardView 是 PartyActivity 中的一个示例

LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
MusicCardView v = (MusicCardView) vi.inflate(R.layout.music_card_template, null);
v.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT, 210));
LinearLayout linearLayout = findViewById(R.id.scroll_child);
v.setSongAttributes(song);
new RetrieveArtTask(v).execute(song);
Log.d("PartyActivity", "SongAttributes Set");
linearLayout.addView(v);    

我查看了this questionthis question,但对发布的解决方案没有任何帮助,如果您提供任何建议,我将不胜感激。

【问题讨论】:

  • upvoteButton.draw(canvas); 这是错误的:您必须使用ViewGroup#addView 而不是“绘制”您的复选框,它们会自己绘制并响应您的点击
  • 好吧,我不是在回答你的问题,而是想知道你会在哪里使用 upArrow 和 downArrow drawable?
  • @AJay 哎呀,那是旧代码。已编辑删除。

标签: java android checkbox


【解决方案1】:

如果您将控件直接绘制到画布上,它们基本上只是位图,您需要将它们添加到视图层次结构(活动/片段布局)中,以便它们接收触摸事件。

附带说明,在 onDraw 中实例化事物是一个很大的禁忌,尤其是视图。

【讨论】:

  • 或将触摸事件发送到复选框
  • 谢谢!请问为什么在onDraw中实例化不好?
  • 要达到所需的 60fps 并避免 UI 卡顿,您有大约 16 毫秒的时间来测量、布局和绘制整个视图层次结构(系统也使用一些毫秒)。根据您的视图层次结构,这可能是一个相当短的时间来完成任务,因此您希望在这三种方法之外做尽可能多的工作。实例化对象既费时又可能导致垃圾收集器启动,既浪费大量时间,也可能浪费帧。
猜你喜欢
  • 2013-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-20
  • 1970-01-01
  • 1970-01-01
  • 2013-03-29
相关资源
最近更新 更多