0

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?

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

Jason Aller
  • 3,391
  • 28
  • 37
  • 36
Euphor08
  • 564
  • 1
  • 10
  • 30

3 Answers3

0

Wherever you want to know it, you'll be in some callback, that will get a View argument passed to it. That's the view in question, or maybe 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);
    }
});
Gavriel
  • 18,088
  • 12
  • 63
  • 98
  • Can you give me an example ? – Euphor08 Feb 03 '16 at 17:09
  • yes, if you explain "how to detect which scroll view is scrolled" by showing your relevant code and point at the code where you would like to detect it – Gavriel Feb 03 '16 at 20:45
  • this is the next part of my code : `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); Log.e("id : "+scroll.getId(),"height: "+height); } });` – Euphor08 Feb 04 '16 at 09:29
-1

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. Inside the callback you get the ID of the scrolled view and pass it to onScrollViewScrolled. This way you will know the ID of the scrolled view.

Danail Alexiev
  • 6,536
  • 1
  • 16
  • 27
  • Thank's but how I get the scrolled scrollview ? I can't find something like : getScrollFromId, – Euphor08 Feb 03 '16 at 17:08
  • it gives me the same id = -1 – Euphor08 Feb 03 '16 at 17:11
  • This means you are not setting the ID of your scroll view. You can go the other way and pass the scroll view instance to the `onScrollViewScrolled()` method. – Danail Alexiev Feb 03 '16 at 17:12
  • I passed the scroll view, but only the last scroll is actif and scrolled to height value `scrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() { @Override public void onScrollChanged() { onScrollViewScrolled(finalScrollView); } public void onScrollViewScrolled(ScrollView scroll ) { height = 10; scroll.scrollTo(0, height); Log.e("id : "+scroll.getId(),"height: "+height); } });` – Euphor08 Feb 03 '16 at 17:30
  • Is your goal to scroll all views when one is scrolled? – Danail Alexiev Feb 03 '16 at 21:32
  • No my goal is to custom the scroll value in scrollTo methode, to be more clear when the user scroll the view, I want that the scroll view display only ONE view, so every scroll equal one view. – Euphor08 Feb 04 '16 at 09:26
  • @DanailAlexiev getId returns the id in the xml layout, like R.id.foo, (as int), but he doesn't have it because he created the ScrollViews programatically by `new ScrollView()`, that's why getId() returns -1 – Gavriel Feb 04 '16 at 09:39
  • @Euphor08 So, basically, you want to have only one displayed view in the Scroll Views? In that case, I would recommend you use a vertical view pager. You can see an example here: http://stackoverflow.com/questions/13477820/android-vertical-viewpager. Is this more or less what you want to have? – Danail Alexiev Feb 04 '16 at 10:17
  • Something like that, but not one scroll, I must have more than one, I add a picture to my question. – Euphor08 Feb 04 '16 at 10:22
-1

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 !

Euphor08
  • 564
  • 1
  • 10
  • 30
  • I don't understand this answer. What's the difference between the code in the question and in the answer? How does this answer the question about how to detect which scroll is scrolling? – Gavriel Feb 04 '16 at 16:25
  • the declaration of scrollview was outside the loop. – Euphor08 Feb 04 '16 at 17:15
  • I bet that only moving it inside couldn't fix the problem! It should work just the same way whether it's inside or outside. You don't have any code whatsovever for detecting which scroller is scrolled, so I still don't see how this solves the problem. My answer (might not work for you) at least has a way to detect which scroller is moving – Gavriel Feb 04 '16 at 17:19