I have a simple ListView containing a set of TextViews.
I always want the top of a TextView at the top of the page.
I can use onScrollStateChanged and adjust the position a bit in the SCROLL_STATE_IDLE.
(similar to List view snap to item)
But this is an a-posteriori correction which doesn't work very smooth.
Is it possible to control, a-priori, the end-position of a fling?
So I want to modify the end-position of a fling as soon as the fling is initiated.
Is this possible?
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
public class Test extends Activity
{
private final static int N = 20;//number of HorizontalScrollView
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
LinearLayout layout = new LinearLayout(this);
//add a ListView
ListView list = new ListView(this);
layout.addView(list);
list.setAdapter(new BaseAdapter()
{
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
TextView t = new TextView(Test.this);
t.setText("line "+position);
t.setMinimumHeight(120);
return t;
}
@Override
public long getItemId(int position)
{
return 0;
}
@Override
public Object getItem(int position)
{
return null;
}
@Override
public int getCount()
{
return N;
}
});
setContentView(layout);
}
}
The fling follows basic physics laws: Let s (distance), v(speed), t(time) and a (acceleration of deceleration).
For a given initial speed (starting from s_0=0), we have
v(t) = a*t + v_0 (1)
s(t) = a*t^2+v*t (2)
a is a system constant (don't know by heart which API). Hence you can compute, for a given initial speed v_0
By inverting the computation, you can compute the initial speed needed to end in a position 's'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With