简体   繁体   English

如何检测滚动的滚动视图

[英]How to detect which scroll view is scrolled

I had a view with multiple scroll view so my question is how to detect which scroll view is scrolled? 我有一个带有多个滚动视图的视图,所以我的问题是如何检测哪个滚动视图被滚动了?

For example if I had 3 vertical scroll view, how to know which scroll is scrolled by the user? 例如,如果我有3个垂直滚动视图,如何知道用户滚动了哪个滚动?

this is my code: 这是我的代码:

public void fillFormules(List<Category> objectsTextToShow)
{
    LinearLayout layoutItemDetail = (LinearLayout) view.findViewById(R.id.middle);
    LinearLayout relativeLayout = (LinearLayout) view.findViewById(R.id.titles);

     LinearLayout layout = new LinearLayout(getActivity());
    layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    layout.setOrientation(LinearLayout.HORIZONTAL);
     ScrollView scrollView = null;
    for (int j = 0; j <objectsTextToShow.size() ; j++){

        TextView textCat = new TextView(getActivity());
        textCat.setText(Check.Languages(objectsTextToShow.get(j).getName(), LANGUAGE_ID));
        textCat.setTextSize(24);
        textCat.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1f));
        textCat.setTextColor(colorUtils.TITLE);
        textCat.setGravity(Gravity.CENTER);

        relativeLayout.addView(textCat);
        textCat.setBackgroundColor(colorUtils.backgroundColor);
        LinearLayout parentLayout = new LinearLayout(getActivity());
        parentLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, (float) 1.0));
        parentLayout.setOrientation(LinearLayout.VERTICAL);
        scrollView = new ScrollView(getActivity());
        scrollView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, (float) 1.0));


        for (int i = 0; i <objectsTextToShow.get(j).getItems().size() ; i++)

                {
                    TextView textView = new TextView(getActivity());
                    textView.setTextSize(24);

                    textView.setText(Check.Languages(objectsTextToShow.get(j).getItems().get(i).getName(), LANGUAGE_ID));

                    textView.setTextColor(colorUtils.TITLE);
                    LinearLayout separator = new LinearLayout(getActivity());
                    separator.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 100));
                    parentLayout.addView(separator);
                    parentLayout.addView(textView);
                }
        scrollView.setVisibility(View.VISIBLE);
        scrollView.addView(parentLayout);
        layout.addView(scrollView);
    }
    layoutItemDetail.addView(layout);
}

Wherever you want to know it, you'll be in some callback, that will get a View argument passed to it. 无论您想知道它的什么地方,都将在某个回调中,该回调将传递一个View参数。 That's the view in question, or maybe view.getParent(). 那是有问题的视图,或者也许是view.getParent()。

scrollView = new ScrollView(getActivity());
// add a tag to each scrollView. It can be any Object, I'll use the
// the `j` index from the for
scrollView.setTag(Integer.valueOf(j));

scrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
    @Override
    public void onScrollChanged() {
        onScrollViewScrolled(finalScrollView);
    }

    public void onScrollViewScrolled(ScrollView scroll) {
        height = 10; // or view.getHeight();
        scroll.scrollTo(0, height);
        // scroll.getTag() returns the object we set by setTag()
        Log.e("j: " + scroll.getTag() + ", id : "+scroll.getId(),"height: "+height);
    }
});

You can do something like this: 您可以执行以下操作:

mScrollView.getViewTreeObserver().addOnScrollChangedListener(new OnScrollChangedListener() {

    @Override
    public void onScrollChanged() {
        onScrollViewScrolled(mScrollView.getId());
    }
});

You will get notified when someone scroll mScrollView . 当有人滚动mScrollView时,您会收到通知。 Inside the callback you get the ID of the scrolled view and pass it to onScrollViewScrolled . 在回调内部,您可以获得滚动视图的ID,并将其传递给onScrollViewScrolled This way you will know the ID of the scrolled view. 这样,您将知道滚动视图的ID。

the problem was in scrollview declaration 问题出在滚动视图声明中

    public void fillFormules(List<Category> objectsTextToShow) {
    LinearLayout layoutItemDetail = (LinearLayout) view.findViewById(R.id.middle);
    LinearLayout relativeLayout = (LinearLayout) view.findViewById(R.id.titles);

     LinearLayout layout = new LinearLayout(getActivity());
    layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    layout.setOrientation(LinearLayout.HORIZONTAL);
     **ScrollView scrollView = null;**
    for (int j = 0; j <objectsTextToShow.size() ; j++){

        TextView textCat = new TextView(getActivity());
        textCat.setText(Check.Languages(objectsTextToShow.get(j).getName(), LANGUAGE_ID));
        textCat.setTextSize(24);
        textCat.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1f));
        textCat.setTextColor(colorUtils.TITLE);
        textCat.setGravity(Gravity.CENTER);

        relativeLayout.addView(textCat);
        textCat.setBackgroundColor(colorUtils.backgroundColor);
        LinearLayout parentLayout = new LinearLayout(getActivity());
        parentLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, (float) 1.0));
        parentLayout.setOrientation(LinearLayout.VERTICAL);
        *ScrollView* scrollView = new ScrollView(getActivity());
        scrollView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, (float) 1.0));


        for (int i = 0; i <objectsTextToShow.get(j).getItems().size() ; i++)

                {
                    TextView textView = new TextView(getActivity());
                    textView.setTextSize(24);

                    textView.setText(Check.Languages(objectsTextToShow.get(j).getItems().get(i).getName(), LANGUAGE_ID));

                    textView.setTextColor(colorUtils.TITLE);
                    LinearLayout separator = new LinearLayout(getActivity());
                    separator.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 100));
                    parentLayout.addView(separator);
                    parentLayout.addView(textView);


                }


        scrollView.setVisibility(View.VISIBLE);
        scrollView.addView(parentLayout);
        layout.addView(scrollView);


    }
    layoutItemDetail.addView(layout);


}

it works now, thank you guys ! 现在就可以了,谢谢大家!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何以编程方式找到哪个视图正在滚动到屏幕顶部? - How to programatically find which view is being scrolled to the top in the screen? 每当使用 java 在 android 工作室滚动回收视图时,我如何隐藏(基本上向下滑动)按钮(在底部对齐) - How can I hide (slide down basically) the Button (which is aligned at the bottom) whenever the recycle view is scrolled in android studio using java 尝试绘制自定义视图如何检测单击了哪个切片 - Trying to draw custom view How to Detect which slice is clicked 如何使用 android 无障碍服务来检测用户触摸了哪个视图? - How to use an android accessibility service to detect which view the user touched? 如何在SWT中使用鼠标滚轮滚动滚动的合成 - How to scroll a scrolled composite using mouse wheels in SWT libGDX:如何检测Scrollpane以最大Y滚动? - libGDX: How can I detect Scrollpane is scrolled at max Y? 如何为SWT滚动复合设置启用水平滚动 - How to set Horizontal scroll enabled for SWT Scrolled Composite 如何在水平滚动视图中滚动 - How to scroll in horizontal scroll view 如何添加滚动视图 - How to add a scroll View 如何设置滚动视图内并包含两个 TextView 的线性布局的可见性消失? - How to set visibility GONE of a Linear Layout which is inside a scroll view and contains two TextViews inside?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM