无法在滚动视图内拖动搜索栏 [英] Cannot drag seekbar inside scrollview
问题描述
如果此问题已在其他地方得到解答,我深表歉意 - 我在 SO 上找不到任何有同样问题的人.
I apologize if this question has been answered elsewhere - I couldn't find anyone on SO with the same problem.
我向位于嵌套 ScrollView 内的 RelativeLayout 添加了一个 SeekBar.但是,我无法拖动搜索栏的拇指.我可以点击它的路径来改变它的值,但我不能拖动它.任何帮助将不胜感激.
I added a SeekBar to a RelativeLayout that sits inside a nested ScrollView. However, I am unable to drag the thumb of the seekbar. I can click along its path to change it's value, but I can't drag it. Any help would be appreciated.
我的 XML 看起来像:
My XML looks like:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/containerView"
android:background="@android:color/transparent"
android:padding="0dip"
android:layout_margin="0dip"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbarStyle="insideOverlay"
android:id="@+id/scrollView" android:layout_alignParentTop="true" android:layout_alignParentLeft="true"
android:fillViewport="true">
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbarStyle="insideInset"
android:id="@+id/horizontalScrollView" android:fillViewport="true">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="@+id/scrollContainer">
</RelativeLayout>
</HorizontalScrollView>
</ScrollView>
</RelativeLayout>
而我的 Java 是:
And my Java is:
SeekBar thisElement = new SeekBar(getActivity());
thisElement.setEnabled(true);
thisElement.setLayoutParams(lp);
thisElement.setAlpha(alphaVal);
thisElement.setMinimumHeight(elementHeight);
final int min = Integer.parseInt(getJsonPropertyValue(tmpJson, "elementSliderMinimum", "0"));
int max = Integer.parseInt(getJsonPropertyValue(tmpJson, "elementSliderMaximum", "100"));
int def = Integer.parseInt(getJsonPropertyValue(tmpJson, "elementSliderDefault", "50"));
thisElement.setMax(max - min);
thisElement.setProgress(def - min);
thisElement.setTag(elementCounter);
thisElement.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
String msg = "onTouch:" + v.getClass().toString();
Log.w("ZZ",msg);
return false;
}
});
thisElement.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
String elementSliderAction = getJsonPropertyValue(tmpJson, "elementSliderAction", "");
if (elementSliderAction.equalsIgnoreCase("changeLabel")) {
int labelNumber = -1;
String elementSliderLabel = getJsonPropertyValue(tmpJson, "elementSliderLabel", "");
for (int j=0; j<childItems.size(); j++) {
BT_item tmpElement = childItems.get(j);
if (tmpElement.getItemId().equalsIgnoreCase(elementSliderLabel)) {
TextView labelToChange = (TextView)scrollContainer.findViewWithTag(j+1000);
String newVal = "" + (progress - min);
labelToChange.setText(newVal);
}
}
}
else if (elementSliderAction.equalsIgnoreCase("saveToPref")) {
String elementSliderPrefName = getJsonPropertyValue(tmpJson, "elementSliderPrefName", "");
String newVal = "" + (progress - min);
setPrefString(elementSliderPrefName, newVal);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
scrollContainer.addView(thisElement);
我怀疑这是因为我的 XML 文件的结构方式,但我不确定正确的解决方案是什么.我偶尔可以让它拖动,但只有在一遍又一遍地尝试之后.此外,OnTouchListener 正确地将其识别为当我尝试拖动时正在触摸的 SeekBar.
I suspect this is because of the way my XML file is structured, but am not sure what the correct solution is. I can occasionally get it to drag, but only after trying over and over and over. Also, the OnTouchListener correctly identifies it as the SeekBar that's being touched when I try to drag.
推荐答案
试试这个
thisElement.setOnTouchListener(new SeekBar.OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
int action = event.getAction();
switch (action)
{
case MotionEvent.ACTION_DOWN:
// Disallow ScrollView to intercept touch events.
v.getParent().requestDisallowInterceptTouchEvent(true);
break;
case MotionEvent.ACTION_UP:
// Allow ScrollView to intercept touch events.
v.getParent().requestDisallowInterceptTouchEvent(false);
break;
}
// Handle Seekbar touch events.
v.onTouchEvent(event);
return true;
}
});
这篇关于无法在滚动视图内拖动搜索栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!