package com.bi.demo; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.AbsListView; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { private ListView listView1,listView2; private TextView tv; private List1Adapter adapter1; private int item; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView1= (ListView) findViewById(R.id.listView1); listView2= (ListView) findViewById(R.id.listView2); tv= (TextView) findViewById(R.id.tv); ArrayList <Lei>list1=new ArrayList<Lei>(); list1.add(new Lei("weocio",R.mipmap.ic_launcher)); list1.add(new Lei("cvnxmvm",R.mipmap.ic_launcher)); list1.add(new Lei("wwwww",R.mipmap.ic_launcher)); list1.add(new Lei("ckkkkkkm",R.mipmap.ic_launcher)); adapter1=new List1Adapter(this,list1); listView1.setAdapter(adapter1); listView1.setOnScrollListener(new AbsListView.OnScrollListener() { @Override public void onScrollStateChanged(AbsListView view, int scrollState) { if(scrollState== AbsListView.OnScrollListener.SCROLL_STATE_IDLE) { View v=adapter1.getView(0, null, listView1); v.measure(0, 0); int itemHeight=v.getMeasuredHeight(); int total=getScrollY(); if((total%itemHeight)>(itemHeight/2))item++; //Log.d("xx",""+getScrollY()); //Log.d("xxy",itemHeight+""); //listView1.setSelection(item); listView1.setSelection(item); tv.setText("" + item); } } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { /* View c = listView1.getChildAt(0); int firstVisiblePosition = listView1.getFirstVisiblePosition(); Log.d("xx",firstVisiblePosition+""); int top = c.getTop(); Log.d("xxy",top+""); Log.d("xxyy", -top + firstVisiblePosition * c.getHeight() + ""); */ item=firstVisibleItem; } }); } public int getScrollY() { View c = listView1.getChildAt(0); if (c == null) { return 0; } int firstVisiblePosition = listView1.getFirstVisiblePosition(); int top = c.getTop();//这里计算的是最上边能看到的item滑上去的距离 Log.d("xxy",top+""); return -top + firstVisiblePosition * c.getHeight() ; } }