2014-05-25 4 views
0

그물에 다운로드되는 데이터가 포함 된 조각에 ListView가 있습니다. 캐시에 데이터를 보관하여 서버에 대한 요청이 완료 될 때까지 정보를 표시 할 수 있습니다.ListView를 제대로 스크롤 할 수 없습니다.

문제는 목록보기 시작 아래로 스크롤하여 아래로 스크롤하면 몇 초 후에 자동으로 상단으로 이동하므로 시간이 없기 때문에 목록의 맨 아래에있는 요소를 선택할 수 없습니다.

목록보기의 레이아웃

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <ProgressBar 
     android:id="@+id/progressbar_line" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     style="?android:attr/progressBarStyleHorizontal" 
     android:indeterminate="true"/> 

    <ListView 
     android:id="@+id/list_stop" 
     android:layout_width="match_parent" 
     android:layout_height="0dip" 
     android:layout_weight="1" 
     android:choiceMode="singleChoice" 
     android:visibility="invisible" 
     android:divider="@color/divider_color" 
     android:dividerHeight="1dp" /> 

</LinearLayout> 

난 당신이 setUpContentListStop에게 캐시 파일이 변경된 때마다 호출 이해할 수있는만큼 지금까지 목록보기를

public class LineFragment extends Fragment implements RestoreActionBar { 

    private static final String ARG_LINE_NAME = "line_name"; 
    private static final String ARG_LINE_COLOR = "line_color"; 

    /** 
    * A pointer to the current callbacks instance (the Activity). 
    */ 
    private LineFragmentCallbacks mCallbacks; 

    private Line mLine; 

    private ProgressBar mProgressBar; 
    private ListView mListView; 

    private DownloadToCache mDownloadToCacheAsyncTask = null; 

    /** 
    * Returns a new instance of this fragment for the given line 
    * name. 
    */ 
    public static LineFragment newInstance(String lineName, String lineColor) { 
     LineFragment fragment = new LineFragment(); 
     Bundle args = new Bundle(); 
     args.putString(ARG_LINE_NAME, lineName); 
     args.putString(ARG_LINE_COLOR, lineColor); 
     fragment.setArguments(args); 
     return fragment; 
    } 

    public LineFragment() { 
     mLine = null; 
    }; 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     if (mDownloadToCacheAsyncTask != null) 
      mDownloadToCacheAsyncTask.cancel(true); 
    } 

    @Override 
    public void onDestroyView() { 
     super.onDestroyView(); 
     if (mDownloadToCacheAsyncTask != null) 
      mDownloadToCacheAsyncTask.cancel(true); 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_line, container, false); 
     mProgressBar = (ProgressBar) rootView.findViewById(R.id.progressbar_line); 
     mListView = (ListView) rootView.findViewById(R.id.list_stop); 
     mListView.setOnItemClickListener(new OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, 
        int position, long id) { 
       selectItem(position); 
      }   
     }); 

     setUpContentListStop(false); 

     return rootView; 
    } 

    private void selectItem(int position) { 
     if (mListView != null) { 
      mListView.setItemChecked(position, true); 
     } 
     if (mCallbacks != null) { 
      mCallbacks.onLineFragmentItemSelected(position); 
     } 
    } 

    @Override 
    public void onAttach(Activity activity) { 
     super.onAttach(activity); 

     mCallbacks = (LineFragmentCallbacks) activity; 

     File jsonFile = new File(activity.getCacheDir() 
       + getArguments().getString(ARG_LINE_NAME) 
       + ".json"); 
     if (jsonFile.exists()) { 
       mLine = new Line(jsonFile); 
     } 

     ((MainActivity) activity).onSectionAttached(
       getArguments().getString(ARG_LINE_NAME), 
       getArguments().getString(ARG_LINE_COLOR)); 
     ((MainActivity) activity).restoreActionBar(); 
    } 

    @Override 
    public void onDetach() { 
     super.onDetach(); 
     mCallbacks = null; 
    } 

    @Override 
    public void restoreActionBar(Activity activity) { 
     if (mListView != null) { 
      mListView.setItemChecked(mListView.getCheckedItemPosition(), false); 
     } 
     ((MainActivity) activity).setActionBarProperty(
       getArguments().getString(ARG_LINE_NAME), 
       Color.parseColor(getArguments().getString(ARG_LINE_COLOR))); 
    } 

    private void onFileInCacheChanged() { 
     setUpContentListStop(true); 
    } 

    /** 
    * Set up the content of the list 
    * @param downloaded true if the json has been download 
    */ 
    private void setUpContentListStop(boolean downloaded) { 
     if (getActivity() == null) 
      return; 

     if (Network.isConnected(getActivity())) { 
      mDownloadToCacheAsyncTask = new DownloadToCache(); 
      mDownloadToCacheAsyncTask.execute(
         getArguments().getString(ARG_LINE_NAME) + ".json", 
         Line.URL_LINE_INFO); 
     } 

     File jsonFile = new File(getActivity().getCacheDir() + "/" 
       + getArguments().getString(ARG_LINE_NAME) 
       + ".json"); 
     if (jsonFile.exists()) { 
       mLine = new Line(jsonFile); 
       return; 
      }   

     mListView.setAdapter(new StopListAdapter(getActivity(), mLine.getListStops())); 
     mListView.setVisibility(View.VISIBLE); 
     } 
    } 

    /* -------------AsyncTask class------------ */ 
    private class DownloadToCache extends AsyncTask<String, Void, Boolean> { 

    } 

    /** 
    * Callbacks interface that all activities using this fragment must implement. 
    */ 
    public static interface LineFragmentCallbacks { 
     /** 
     * Called when an item in the list is selected. 
     */ 
     void onLineFragmentItemSelected(int position); 
    } 

} 

답변

0

를 사용하는 단편 (onFileInCacheChanged) .

setUpContentListStop 메서드에서는 매번 목록보기의 어댑터를 만들고 설정합니다 : mListView.setAdapter(new StopListAdapter(getActivity(), mLine.getListStops()));. 대신 어댑터를 한 번만 (onCreateView) 만들고 새 데이터를 사용할 수있을 때 데이터를 업데이트 할 수 있습니다.

편집

당신의 StopListAdapter 당신이 개체의 목록에 새 항목을 추가하기위한 추가 방법을 만들 수 있습니다 BaseAdapter 확장합니다.

public void add(Object item) { 
    list.add(item); 
    notifyDataSetChanged(); 
} 

그리고

+0

예를 이해 adapter.add(mLine.getListStops()))처럼 setUpContentListStop 방법이 메소드를 호출하지만, 내가 어떻게 어댑터의 데이터를 업데이트 할 수 있습니다? – Guillaume

+0

어떤 종류의 어댑터'StopListAdapter'입니까? –

+0

'BaseAdapter'를 확장합니다. – Guillaume

관련 문제