Mar*_*rek 5 android scroll focus list scrollview
滚动之前:

滚动期间:

我在滚动期间的期望:

我的HorizontalScrollView有问题.当我从这个视图中选择一个元素时,我通过调用将焦点设置为该元素:
else if (v.getParent() == candidatesScrollView.getChildAt(0))
{
Button candidateButton = (Button) v;
v.requestFocusFromTouch();
v.setSelected(true);
(...)
}
Run Code Online (Sandbox Code Playgroud)
之后,当我滚动列表而不选择其他元素时,我失去了先前所选元素的焦点.我对这个主题做了一些研究,但没有解决方案可以对我有用......如何在不丢失选定元素焦点的情况下滚动我的HorizontalScrollList?任何帮助都会得到赞赏.自从我提出这个问题以来,已经过了大约14天,但仍未找到解决方案.请帮忙.
这是我的XML的一部分:
<HorizontalScrollView
android:id="@+id/CandidatesHorizontalScrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/linearLayout2"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:visibility="gone" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:orientation="horizontal" >
<Button
android:id="@+id/horizontalscrollview1_button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:textSize="25sp" />
(...)
// 11 more buttons
</LinearLayout>
</HorizontalScrollView>
Run Code Online (Sandbox Code Playgroud)
UPDATE
MY CURRENT SOLUTION#1(无法正常工作): 滚动后再滚动(例如向后滚动),从所选元素开始滚动.
我创建了自定义HorizontalScrollView类,在其中我重写了onTouchEvent()方法.我认为这不是最佳方式,因为在这种情况下,每次移动一个像素时我都要进行计算.例如,如果我将toast.show()添加到下面的方法中,它将尝试显示尽可能多的我移动像素的吐司(如果我移动10个像素,它将尝试显示10个Toast).无论如何,它对我有用,并且保留了选择和关注.
请帮我修改这段代码,最后为这个已知问题找到一个好的答案:
@Override
public boolean onTouchEvent(MotionEvent ev)
{
int i = 0;
Button button = null;
for (; i < 11; i++)
{
button = (Button)((LinearLayout)getChildAt(0)).getChildAt(i);
if(button.isSelected())
break;
}
super.onTouchEvent(ev);
button.setSelected(true);
button.requestFocusFromTouch();
return true;
}
Run Code Online (Sandbox Code Playgroud)
为了确保上面的代码能够工作,你需要在HorizontalScrollView中一次只有一个选定的项目,即当你按不同的按钮时,你需要使前一个代码设置为setSelected(false)
我目前的解决方案#2(工作不正常):
我试图实现的解决方案#2,认为第一个不够优雅,涉及使用手势检测器.在我的自定义HorizontalListView类中,我添加了以下代码:
构造函数:
public MyHorizontalScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
gestureDetector = new GestureDetector(context, new MyHorizontalScrollViewGestureDetector());
this.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
gestureDetector.onTouchEvent(event);
return true;
}
});
// TODO Auto-generated constructor stub
}
Run Code Online (Sandbox Code Playgroud)
MyHorizontalScrollViewGestureDetector内部类:
public class MyHorizontalScrollViewGestureDetector extends SimpleOnGestureListener
{
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
{
//Here code similar like that one in solution #1
//But the View is not scrolling, even without that code
super.onScroll(e1, e2, distanceX, distanceY);
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
但是,列表不会滚动该解决方案.我可以添加onScroll方法:
ScrollBy((int)positionX, (int)positionY);
Run Code Online (Sandbox Code Playgroud)
这使得列表将滚动,但不是一个好的方式广告它有时会冻结.我想知道为什么滚动不被超级调用.方法.
我当前的解决方案#3(工作,但它是四处走动):
因为解决方案1和解决方案2都不起作用,所以我决定不再使用焦点.我现在所做的是,每当我点击它时每次更改Button Button时都会更改Button Drawable.我使用与用于聚焦按钮(Holo)相同的Drawable.在这种情况下,我不必担心在HorizontalScrollView中滚动.这个解决方案是一种循环,所以我期待任何评论,建议和编辑.