在水平滚动型滚动有失焦点元素的焦点, [英] Scrolling in Horizontal ScrollView loses focus of focused element,

查看:175
本文介绍了在水平滚动型滚动有失焦点元素的焦点,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

之前滚动:

Before Scrolling:

在滚动:

During Scrolling:

滚动的时候,我期待什么:

What I expect during scrolling:

我有Horizo​​ntalScrollView一个问题。当我选择从这个视图中的一个元素,我将焦点设置到该元素调用:

I have a problem with HorizontalScrollView. When I choose an element from this View, I set focus to that element by calling:

else if (v.getParent() == candidatesScrollView.getChildAt(0))
{
    Button candidateButton = (Button) v;
    v.requestFocusFromTouch();
    v.setSelected(true);
            (...)
}

之后,当我滚动列表而不选择其他的元素,我失去了pviously所选元素$ P $的焦点。我做了有关此主题的一些研究,但没有解决方案,可以为我工作......我怎么可以滚动我Horizo​​ntalScrollList不受所选元素失去焦点?任何帮助是鸭preciated。它已经14天左右,因为我问这个问题,仍然没有找到解决办法。请大家帮帮忙。

After that, when I scroll the list without choosing other element, I lose focus of previously selected element. I made some research about this topic, but there was no solution that could work for me... How can I scroll my HorizontalScrollList without loosing focus from selected element? Any Help is Appreciated. It has been about 14 days since I asked that question and still didn't find solution. Please help.

下面是我的XML的一部分:

Here is part of my 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>


更新

我当前的解决方案#1(不正常): 滚动,然后再次滚动后(例如滚动背),选自元素开始滚动。

MY CURRENT SOLUTION #1 (not working correctly): After scrolling, and then scrolling again (for example scrolling back), scrolling starts from selected element.

我在里面,我覆盖的onTouchEvent()方法的自定义Horizo​​ntalScrollView类。我不认为这是这样做的最佳方式,因为在这种情况下,我有我每次移动,甚至一个像素进行计算。例如,如果我添加toast.show(),以下面的方法,它会尝试显示尽可能多的面包尽可能多的让我感动的像素(如果我移动了10个像素,它会尽量展现10吐司)。无论如何,这对我的作品的选择和重点被关押。

请帮我修改此code键使最终为已知问题的一个很好的答案:

I created custom HorizontalScrollView class inside which I overridden onTouchEvent() method. I don't think this is optimal way of doing that, because in that case I have to do calculations every time I move even one pixel. for example, if I add toast.show() to the below method, it will try to show as many toast as many I moved pixels (If I move by 10 pixels, it will try to show 10 Toast). Anyway, it works for me and the selection and focus are being kept.

Please help me modify this code to make finally a good answer for that known issue:

@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;
}

要肯定的是,上述code将工作,你需要有在同一时间在你的Horizo​​ntalScrollView只有一个选择项,即当你preSS diferent按钮,你需要做的previous 1的setSelected(假)

To be sure that the above code will work, you need to have only one selected item in your HorizontalScrollView at a time, i.e when you press diferent button, you need to make the previous one setSelected(false)

我当前的解决方案#2(不正常):

解决方案#2,我试图实现,以为第一个是不够优雅,涉及的姿态探测器的使用。在我的自定义Horizo​​ntalListView I类增加了以下code:

Solution #2 that I tried to implement, thinking that first one is not elegant enough, involves usage of gesture detector. In my custom HorizontalListView class I have added the following code:

构造器:

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
}

MyHorizo​​ntalScrollViewGestureDetector内部类:

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; 
        }
    }   

不过,列表不会滚动与解决方案。我可以添加到onScroll方式:

However, the list is not scrolling with that solution. I can add to onScroll method:

ScrollBy((int)positionX, (int)positionY);

这使得该列表会滚动,但不是一个好办法广告它有时会结冰。 我很奇怪,为什么滚动不调用超。方法。

which makes the list will scroll, but not in a good way ad it will freeze sometimes. I am wondering why scrolling is not called by the super. method.

我当前的解决方案#3(工作,但它是绕行):

由于这两个解决方案1和2不工作,我决定不与焦点玩了。 我现在要做的,就是改变按钮可绘制每当我点击它,每次当我改变不同的按钮。我用同样的绘制对象作为用于聚焦按钮(全息)。在这种情况下,我不必担心滚动Horizo​​ntalScrollView。该解决方案是某种步行约的,所以我很期待有任何意见,建议和修改。

Because both solution 1 and 2 were not working, I decided to not play with focus anymore. What I do now, is to change the Button Drawable whenever I click it and every time when I change to different Button. I use same Drawable as is used for focused button (Holo). In that case, I don't have to be worried about scrolling in HorizontalScrollView. This solution is some kind of walk-around, so I am looking forward to any comments, suggestions and edits.

推荐答案

我已经找到了解决我的问题。你可以看到详细的更新问题。而不是使用焦点基本上,我决定使用可绘制选择,这是更为容易。

I have found a solution to my problem. You can see the updated question for details. Basically, instead of using focus I decided to use Drawable selector, which is much more easier.

这篇关于在水平滚动型滚动有失焦点元素的焦点,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆