gpt4 book ai didi

Android SeekBarChangeListener 和 TouchListener 无事件

转载 作者:IT老高 更新时间:2023-10-28 23:35:56 25 4
gpt4 key购买 nike

我遇到了按钮单击事件和搜索栏事件未触发的问题。当我在搜索栏上拖动时, slider 会在视觉上改变位置,但在我的 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);
}

最佳答案

试试这个,用FrameLayout代替LinearLayout,性能不是最好的,但效果很好

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

关于Android SeekBarChangeListener 和 TouchListener 无事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31458849/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com