【问题标题】:Android SeekBarChangeListener and TouchListener No EventsAndroid SeekBarChangeListener 和 TouchListener 无事件
【发布时间】:2015-10-06 04:08:58
【问题描述】:

我遇到了按钮单击事件和搜索栏事件未触发的问题。当我在搜索栏上拖动时,滑块会在视觉上改变位置,但在我的 OnSeekBarChangedListener 中没有触发任何事件。我还有一个ImageButton 应该接收点击事件,但它也不会触发。此处底部的 MyView 正确处理触摸事件。它有一个GLSurfaceView,我可以平移和缩放。什么可能导致事件不触发?

这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/tec_root">

    <ImageButton
        android:id="@+id/button_star"
        android:contentDescription="@string/star_description"
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:layout_marginTop="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginEnd="16dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:src="@drawable/star_empty"
        android:background="#00000000"
        android:scaleType="fitXY"/>

    <LinearLayout
        android:id="@+id/view_plot"
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_below="@id/button_star"/>

    <SeekBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/button_star"
        android:layout_toStartOf="@id/button_star"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:progress="50"
        android:layout_alignBaseline="@id/button_star"
        android:id="@+id/seekbar" />

</RelativeLayout>

这里是听众:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.my_activity);

    prefs = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
    starred = prefs.getBoolean("starred", false);

    final ImageButton starButton = (ImageButton) findViewById(R.id.button_star);
    starButton.setImageDrawable(ContextCompat.getDrawable(this, (starred ? R.drawable.star_filled :
            R.drawable.star_empty)));

    starButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            starred = !starred;

            prefsEditor = prefs.edit();
            prefsEditor.putBoolean("starred", starred);
            prefsEditor.apply();

            starButton.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(),
                    (starred ? R.drawable.star_filled : R.drawable.star_empty)));
        }
    });

    ((SeekBar) findViewById(R.id.seekbar)).setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            Log.e("TEST", "Seekbar onProgressChanged called");
            Toast.makeText(getApplicationContext(), "Seekbar fired", Toast.LENGTH_SHORT).show();
            // respond to update
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });

    MyView myView = new MyView(this, points, false);
    ((LinearLayout) findViewById(R.id.view_plot)).addView(myView);
}

【问题讨论】:

  • 能否覆盖搜索栏中的 onTouchEvent 并发布日志?从文档和 SeekBar 的源代码来看,它的左/右视图似乎可以“窃取”触摸事件,从而导致重绘但不会出现进度更新通知。
  • 嗯,它出于某种原因突然开始工作了。我稍微改变了课程,但不记得改变任何与SeekBar 的逻辑有关的东西。
  • 在设置监听器之前尝试启动搜索栏。
  • 你能发布你的MyView吗?我看到了你的代码,除了你的GlSurfaceView,没问题。您应该发布它以获得帮助。
  • 您的代码正在运行并在我检查时触发了祝酒词,但我删除了您的此代码 MyView myView = new MyView(this, points, false); ((LinearLayout) findViewById(R.id.view_plot)).addView(myView);那么您能否发布此代码及其相关代码,以便我们为您提供帮助

标签: android input


【解决方案1】:

尝试将您的代码更改为:

MyView myView = new MyView(this, points, false);
    myView.setLayoutParams(new LinearLayout.LayoutParams(
                                         LinearLayout.LayoutParams.MATCH_PARENT,
                                         LinearLayout.LayoutParams.MATCH_PARENT));
((LinearLayout) findViewById(R.id.view_plot)).addView(myView);

【讨论】:

    【解决方案2】:

    试试这个,把LinearLayout换成FrameLayout,性能不是最好的,但是效果很好

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/tec_root"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >
    
                <SeekBar
                    android:id="@+id/seekbar"
                    android:layout_width="0dip"
                    android:layout_height="wrap_content"
                    android:background="#ABABAB"
                    android:layout_weight="1"
                    android:progress="50" />
    
                <ImageButton
                    android:id="@+id/button_star"
                    android:layout_width="32dp"
                    android:layout_height="32dp"
                    android:background="#00000000"
                    android:contentDescription="@string/star_description"
                    android:scaleType="fitXY"
                    android:src="@drawable/star_empty" />
            </LinearLayout>
    
            <FrameLayout
                android:id="@+id/view_plot"
                android:layout_width="match_parent"
                android:layout_height="0dip"
                android:layout_weight="1"      
                />
     </LinearLayout>
    

    【讨论】:

      【解决方案3】:

      是否可以通过删除 GLSurfaceView 来重现?

      如果是这样,更改 SurfaceView.setZOrderOnTop 可能会有所帮助。 http://developer.android.com/reference/android/view/SurfaceView.html#setZOrderOnTop(boolean)

      【讨论】:

        【解决方案4】:

        可能存在布局设计问题,请重新排列ui的组件。 seekbar 位于 imagebutton 上方,因此 android 平台无法检测到触摸事件。

        在搜索栏中设置 layout_below

        【讨论】:

          【解决方案5】:

          造成这种情况的一个可能原因是自定义 MyView 中的触摸处理。我们必须非常小心 onTouchEvent() 函数的返回值。如果您正在消费事件,请尝试从您的 onTouchEvent() 返回“true”,否则返回“false”。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2023-03-12
            • 2019-04-24
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多