我有一个带有多个滚动视图的视图,所以我的问题是如何检测滚动的滚动视图?
例如,如果我有3个垂直滚动视图,如何知道用户滚动哪个滚动?
这是我的代码:
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);
}
答案 0 :(得分:0)
无论你想知道什么,你都会进行一些回调,这会将一个View参数传递给它。这是有问题的观点,或者可能是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);
}
});
答案 1 :(得分:-1)
您可以这样做:
mScrollView.getViewTreeObserver().addOnScrollChangedListener(new OnScrollChangedListener() {
@Override
public void onScrollChanged() {
onScrollViewScrolled(mScrollView.getId());
}
});
当有人滚动mScrollView
时,您会收到通知。在回调中,您将获得滚动视图的ID并将其传递给onScrollViewScrolled
。这样您就可以知道滚动视图的ID。
答案 2 :(得分:-1)
问题出在滚动视图声明
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);
}
现在有效,谢谢你们!