Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we control the end position of a fling in a ListView

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

}


}
like image 357
Marc Van Daele Avatar asked Nov 13 '22 14:11

Marc Van Daele


1 Answers

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

  • from (1): the t for which v(t) will become zero (=> end of the fling)
  • from (2): the resulting distance

By inverting the computation, you can compute the initial speed needed to end in a position 's'

like image 175
Marc Van Daele Avatar answered Feb 16 '23 06:02

Marc Van Daele