首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >安卓SeekBarChangeListener和TouchListener无事件

安卓SeekBarChangeListener和TouchListener无事件
EN

Stack Overflow用户
提问于 2015-07-16 23:45:14
回答 5查看 526关注 0票数 16

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

这是我的布局:

代码语言:javascript
复制
<?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>

下面是监听者:

代码语言:javascript
复制
@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);
}
EN

回答 5

Stack Overflow用户

发布于 2015-07-21 15:56:59

试试这个,用FrameLayout替换LinearLayout,它在性能上不是最好的,但工作得很好

代码语言:javascript
复制
<?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
EN

Stack Overflow用户

发布于 2015-07-21 15:36:08

尝试将您的代码更改为:

代码语言:javascript
复制
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);
票数 0
EN

Stack Overflow用户

发布于 2015-07-24 16:29:29

这可以通过移除GLSurfaceView来重现吗?

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31458849

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档