[英]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.