0

내용을 업데이트 한 후 RecyclerView에 현재 스크롤 위치를 유지하려면 어떻게해야합니까? 그때 전화 작업이 완료되면스크롤 위치를 유지하면서 RecyclerView 업데이트

private class getArrivals extends AsyncTask<Long, Void, Long>{ 

     @Override 
     protected void onPreExecute(){ 
      //emptyMessage.setVisibility(VISIBLE); 
      //recyclerView.setVisibility(GONE); 
     } 
     @Override 
     protected Long doInBackground(Long... params) { 
      long station_id = params[0]; 
      station = db.stationModel().getStationById(station_id); 
      arrivals = db.arrivalModel().getNextArrivalsByStation(station_id, StaticClass.getDays()); 
      return station_id; 
     } 

     @Override 
     protected void onPostExecute(Long result){ 
      if(getSupportActionBar() != null) { 
       getSupportActionBar().setTitle(capitalize(station.name)); 
      } 

      refreshList(); 
     } 
} 

:

private void setRepeatingAsyncTask() { 

    final Handler handler = new Handler(); 
    Timer timer = new Timer(); 

    TimerTask task = new TimerTask() { 
     @Override 
     public void run() { 
      handler.post(new Runnable() { 
       public void run() { 
        try { 
         new getArrivals().execute(station_id); 
        } catch (Exception e) { 
         // error, do something 
        } 
       } 
      }); 
     } 
    }; 
    timer.schedule(task, 0, 30000); // interval of 30 seconds 
} 

AsyncTask 최신 목록의 내용에 대한 데이터베이스를 조회합니다 다음과 같이

onCreate에서 나는 반복 작업을 설정 목록 새로 고침 :

private void refreshList(){ 

    arrivalListAdapter = new ArrivalListAdapter(getApplicationContext(), arrivals); 
    staggeredGridLayoutManager = new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL); 
    recyclerView.setLayoutManager(staggeredGridLayoutManager); 
    recyclerView.setAdapter(arrivalListAdapter); 
    Log.d("arrivals", arrivals.size()+""); 
    arrivalListAdapter.notifyDataSetChanged(); 

    if(arrivals.size() == 0){ 
     emptyMessage.setVisibility(VISIBLE); 
     recyclerView.setVisibility(GONE); 

     new fetchArrivalsFromSource().execute(station.id); 
    }else{ 
     emptyMessage.setVisibility(GONE); 
     recyclerView.setVisibility(VISIBLE); 
    } 
} 

나는 사전입니다. tty 내 문제에 대한 이유는 내가 매번 어댑터를 설정하고 있는지 확인하십시오. 나는 초기 작업으로 설정하려고 시도했지만 목록이 전혀 업데이트되지 않습니다.

답변

5

당신은 현재의 위치를 ​​얻을이 같은 이전 위치로 어댑터와 부드러운 스크롤 새로 고칠 수 있습니다 :

smoothScroller.setTargetPosition(position); 
layoutManager.startSmoothScroll(smoothScroller); 
:

RecyclerView.SmoothScroller smoothScroller = new LinearSmoothScroller(context) { 
    @Override protected int getVerticalSnapPreference() { 
    return LinearSmoothScroller.SNAP_TO_START; 
    } 
}; 

은 다음으로 스크롤 위치를 설정을