簡體   English   中英

如何以編程方式將ListView滾動到android中的特定位置

[英]how to scroll listview to specific position in android programmatically

我試圖使listview滾動到位置,我嘗試了所有可能的功能,但都無法正常工作

首先對代碼進行更多說明:我將標記放在地圖上,然后再調用列表適配器L如果用戶在地圖上單擊標記,則列表視圖應將選中的位置顯示為標記

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

直接滾動:

getListView().setSelection(21);

為了順利滾動:

getListView().smoothScrollToPosition(21);

從此處獲取:以編程方式滾動到Android ListView中的特定位置

使用以下代碼。

context.yourListView.smoothScrollToPosition(10);

謝謝大家我用這個

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM