如何以编程方式将ListView滚动到android中的特定位置 [英] how to scroll listview to specific position in android programmatically

查看:137
本文介绍了如何以编程方式将ListView滚动到android中的特定位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使listview滚动到位置,我尝试了所有可能的功能,但都无法正常工作

I try to make listview to scroll to position , i have tried all possible functions but all not work

有关代码的更多说明首先:我在地图上放置标记,然后在呼叫列表适配器上L如果用户在地图上单击标记,则列表视图应将所选位置显示为标记单击

for more explanation of code firstly : i put markers on map then call list adapter secondly L if user click marker on map the list view should show the selected position as marker clicked

private void Putting_places_on_Map(final List<PlaceModel> Places) {
    // TODO Auto-generated method stub
    for (int i = 0; i < Places.size(); i++) {
        LatLng place_loc = new LatLng(Double.parseDouble(Places.get(i)
                .getLatitude()), Double.parseDouble(Places.get(i)
                .getLongitude()));
        map.addMarker(new MarkerOptions().position(place_loc).icon(
                BitmapDescriptorFactory
                        .fromResource(R.drawable.glossygreencirclemarker)));
    }

    // when marker clicked
    map.setOnMarkerClickListener(new OnMarkerClickListener() {

        @Override
        public boolean onMarkerClick(Marker marker) {
            // TODO Auto-generated method stub
            LatLng PlaceLocation = marker.getPosition();
            int selected_marker=-1;
            for (int i = 0; i < Places.size(); i++) {
                if (PlaceLocation.latitude == Double.parseDouble(Places
                        .get(i).getLatitude())
                        && PlaceLocation.longitude == Double
                                  .parseDouble(Places.get(i).getLongitude())) {
                    selected_marker=i;
                }
            }
            Update_List_Places(Places, selected_marker);
            return false;
        }
    });

}
private void Update_List_Places(List<PlaceModel> Places,
        int selected_marker_position) {
    // TODO Auto-generated method stub
    list_places_returned = true;
    ListPlacesNearbyAdapter placesNearbyAdapter = new ListPlacesNearbyAdapter(
            (ArrayList<PlaceModel>) Places, activity,
            selected_marker_position);
    placesNearbyAdapter.notifyDataSetChanged();

    if(selected_marker_position != -1){

        //listview_nearby_locations.setSelection(selected_marker_position);
        //listview_nearby_locations.smoothScrollToPosition(selected_marker_position);
        //listview_nearby_locations.setSelectionFromTop(selected_marker_position, 100);
        int y= selected_marker_position*40;
        listview_nearby_locations.scrollTo(0,y);
    }
    listview_nearby_locations.setAdapter(placesNearbyAdapter);
}

查看此图片

推荐答案

尝试一下:

listview_nearby_locations.setSelection(9);

这篇关于如何以编程方式将ListView滚动到android中的特定位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆