【问题标题】:how to scroll listview to specific position in android programmatically如何以编程方式将listview滚动到android中的特定位置
【发布时间】:2015-01-28 11:43:15
【问题描述】:

我尝试让列表视图滚动到位置,我尝试了所有可能的功能但都不起作用

更多代码解释 首先:我在地图上放置标记然后调用列表适配器 其次,如果用户单击地图上的标记,则列表视图应将所选位置显示为单击标记

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

看这张照片

【问题讨论】:

  • smoothScrollToPosition
  • 可以使用 setSelection() 方法
  • 我已经用过了,但是不行

标签: android android-listview


【解决方案1】:

试试这个:

listview_nearby_locations.setSelection(9);

【讨论】:

    【解决方案2】:

    对于直接滚动:

    getListView().setSelection(21);
    

    为了平滑滚动:

    getListView().smoothScrollToPosition(21);
    

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

    【讨论】:

    • getlistview() 未在我的班级中定义
    【解决方案3】:

    谢谢大家,我用了这个

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

    【讨论】:

      【解决方案4】:

      使用下面的代码..

      context.yourListView.smoothScrollToPosition(10);
      

      【讨论】:

      • 你能提供更多解释吗?
      猜你喜欢
      • 2011-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-22
      • 2015-07-29
      • 1970-01-01
      相关资源
      最近更新 更多