如何以编程方式将listview滚动到android中的特定位置

时间:2015-01-28 11:43:15

标签: android android-listview

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

有关代码的更多说明 首先:我把标记放在地图上然后调用列表适配器 其次,如果用户点击地图上的标记,则列表视图应显示所选位置作为标记点击

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

看到这张照片 enter image description here

4 个答案:

答案 0 :(得分:3)

试试这个:

listview_nearby_locations.setSelection(9);

答案 1 :(得分:2)

直接滚动:

getListView().setSelection(21);

顺畅滚动:

getListView().smoothScrollToPosition(21);

从这里采取:Programmatically scroll to a specific position in an Android ListView

答案 2 :(得分:1)

使用以下代码..

context.yourListView.smoothScrollToPosition(10);

答案 3 :(得分:1)

谢谢你们,我用过这个

Runnable runnable = new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            if(selected_marker_position != -1){                 
                listview_nearby_locations.setSelection(selected_marker_position);
            }
        }
    };
    runnable.run();