Note that there are some explanatory texts on larger screens.

plurals
  1. POhorizontal scrollview with numbers in android
    text
    copied!<p>I have scrollview when I put 200 x TextView with values.</p> <p>I have three problems: 1. Scroll stoped in different position. I need stoped it value on center scroll. 2. I can get value from scroll only I stoped scroll. I want get it when value on center change. 3. I have scroll with values 0 - 100 and values on center is 4. I want 0. And blank field on left.</p> <p>Please help me.</p> <p>CODE:</p> <pre><code>for(int i = 0 ;i&lt;221;i++) { valueTV = new TextView(getActivity().getApplicationContext()); valueTV.setText(Integer.toString(i)); valueTV.setWidth(50); valueTV.setHeight(30); valueTV.setGravity(Gravity.CENTER_HORIZONTAL); valueTV.setTextColor(Color.BLACK); valueTV.setTag(i-1); line.addView(valueTV); } final int chilrenNum = line.getChildCount(); scroll = (MyScrollView) rootView.findViewById(R.id.horizontalScrollView1); scroll.setOnScrollStoppedListener(new OnScrollStoppedListener() { @Override public void onScrollStopped() { int center = scroll.getScrollX() + scroll.getWidth() / 2; for (int i = 0; i &lt; chilrenNum; i++) { View v = line.getChildAt(i); int viewLeft = v.getLeft(); int viewWidth = v.getWidth(); if (center &gt;= viewLeft &amp;&amp; center &lt;= viewLeft + viewWidth) { int center_at=(viewLeft+5 + viewWidth / 2) - center; scroll.scrollBy(center_at, 0); height.setText(v.getTag().toString()); break; } } } }); </code></pre> <p>AND SCROLL</p> <pre><code>public class MyScrollView extends HorizontalScrollView{ private Runnable scrollerTask; private int initialPosition; private int newCheck = 100; public interface OnScrollStoppedListener{ void onScrollStopped(); } private OnScrollStoppedListener onScrollStoppedListener; public MyScrollView(Context context, AttributeSet attrs) { super(context, attrs); scrollerTask = new Runnable() { public void run() { int newPosition = getScrollX(); if(initialPosition - newPosition == 0){//has stopped if(onScrollStoppedListener!=null){ onScrollStoppedListener.onScrollStopped(); } }else{ initialPosition = getScrollX(); MyScrollView.this.postDelayed(scrollerTask, newCheck); } } }; } public void setOnScrollStoppedListener(OnScrollStoppedListener listener){ onScrollStoppedListener = listener; } public void startScrollerTask(){ initialPosition = getScrollX(); MyScrollView.this.postDelayed(scrollerTask, newCheck); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload