如何检测列表视图的特定项目到达屏幕顶部?

时间:2015-08-11 02:00:49

标签: android

我想要的是当我滚动(例如Title2)到顶部时,大标题将根据当时顶部的标题(Title2)进行更改。

大标题只是ListView上方的LinearLayout,Listview中的小标题是List的项目。所以我有一个想法是检测特定项目是否达到顶部,但我不知道如何。请帮帮我!

enter image description here

1 个答案:

答案 0 :(得分:0)

首先将AbsListView.OnScrollListener设置为ListView。然后使用AdapterView.getFirstVisiblePosition()获取第一个显示的项目。

public class MainActivity extends Activity implements AbsListView.OnScrollListener {
    private static final String TAG = "MainActivity";

    private ListView mList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mList = (ListView) findViewById(R.id.list);
        mList.setAdapter(new MyListAdapter(50));
        mList.setOnScrollListener(this);
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {

    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
            int totalItemCount) {
        // This is the position within the adapter's data set
        // for the first item displayed on screen.
        int firstPosition = mList.getFirstVisiblePosition();
        // Determine whether change title or not

    }
}