这是您的 hour_listView。只需为其设置一个字符串适配器。假设您可以使用 ArrayAdapter 填充 listView。
hr_list.setOnScrollListener(new OnScrollListener() {
int currentcount,topitem,firstitem,lastitem;
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (scrollState == SCROLL_STATE_IDLE && currentcount == 4) {
while (scrollState == SCROLL_STATE_TOUCH_SCROLL) {
}
LinearLayout item = (LinearLayout) view.getChildAt(0);
int height = item.getHeight();
int top = Math.abs(item.getTop()); // top is a negative value
if (top <= height/2){
view.smoothScrollToPositionFromTop(topitem, 0, 3000);
Log.d("Switch", "up");
}
if(top> height/2){
view.smoothScrollToPositionFromTop(topitem+1, 0,3000);
Log.d("Switch", "down");
}
}
}
然后在 button.clickListener 上设置,你会得到可见 listView 的中心行号。
int list_hr = hr_list.getFirstVisiblePosition() - hr_list.getHeaderViewsCount();
几个关键点。
* 为 listView 1dp 设置分隔线高度。
*使用线性布局在listView中插入
行高应该是列表视图总高度的 1/3(以 dp 为单位)。
列表视图和按钮的 XML
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Snap List"
android:textSize="25sp" />
<ListView
android:id="@+id/listView1"
android:layout_width="100dp"
android:layout_height="120dp"
android:fadingEdgeLength="33dp"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:layout_marginTop="73dp"
android:background="@drawable/lisbg"
android:scrollbars="none" >
</ListView>
<StackView
android:id="@+id/stackView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/listView1"
android:layout_marginLeft="103dp"
android:layout_marginTop="57dp"
android:layout_toRightOf="@+id/textView1" >
</StackView>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/textView1"
android:layout_marginRight="58dp"
android:text="OK" />
<Button
android:id="@+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:text="Scroll 5" />
行的 XML。
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:textSize="25sp"
android:gravity="center_vertical|center_horizontal"
android:layout_gravity="center_vertical|center_horizontal"
android:text="TextView" />