0

[更新]滾動到列表視圖頂部:如何從我的適配器獲取列表視圖?

這裏是AlsaceFragment類的代碼:

public class AlsaceNewsFragment extends ListFragment implements PullToRefreshAttacher.OnRefreshListener { 

    private static final String STATE_ACTIVATED_POSITION = "activated_position"; 
    private int mActivatedPosition = ListView.INVALID_POSITION; 
    private RssServiceAlsace rssService; 
    private static final String URL_LALSACE = "http://www.lalsace.fr/actualite/alsace/rss"; 

    private PullToRefreshAttacher mPullToRefreshAttacher; 

    /** 
    * Constructor 
    */ 
    public AlsaceNewsFragment() { 
     // Empty constructor required for fragment subclasses 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     refreshList(true); 
    } 

    @Override 
    public void onViewCreated(View view, Bundle savedInstanceState) { 
     super.onViewCreated(view, savedInstanceState); 
     if (savedInstanceState != null && savedInstanceState.containsKey(STATE_ACTIVATED_POSITION)) { 
      setActivatedPosition(savedInstanceState.getInt(STATE_ACTIVATED_POSITION)); 
     } 

     // CACHER SEPARATEURS 
     SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getActivity()); 
     boolean enleverseparateur = settings.getBoolean("enleverseparateur", false); 
     if (enleverseparateur == true){ 
      getListView().setDividerHeight(0); 
      getListView().setDivider(null); 
     } 
     // FIN CACHER SEPARATEURS 

     ListView listView = getListView(); 
     mPullToRefreshAttacher = ((MainActivity) getActivity()).getPullToRefreshAttacher(); 
     mPullToRefreshAttacher.setRefreshableView(listView, this); 

    } 

    public void setActivatedPosition(int position) { 
     if (position == ListView.INVALID_POSITION) { 
      getListView().setItemChecked(mActivatedPosition, false); 
     } else { 
      getListView().setItemChecked(position, true); 
     } 
     mActivatedPosition = position; 
    } 

    @Override 
    /** 
    * Called when pullToRefresh has been started 
    */ 
    public void onRefreshStarted(View view) { 
     refreshList(false); // We don't want to show progress dialog in this case 
    } 

    private void refreshList(boolean displayLoading){ 
     rssService = new RssServiceAlsace(this, displayLoading); 

     // UNE SOURCE 
     SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getActivity()); 
     boolean unesource = settings.getBoolean("unesource", false);  
     if (unesource == true) { 
      rssService.execute(URL_LALSACE); 
     } else { 
      rssService.execute(URL_LALSACE); 
     } 
     // FIN UNE SOURCE 

    } 

    public void notifyPullFinished() { 
     // Notify PullToRefreshAttacher that the refresh has finished 
     mPullToRefreshAttacher.setRefreshComplete(); 
    } 

    @Override 
    public void onListItemClick(ListView l, View v, int position, long id) { 
     ArticleAlsace article = (ArticleAlsace) this.getListAdapter().getItem(position); 
     Bundle arguments = new Bundle(); 
     arguments.putString("URL", article.getUrl()); 
     arguments.putString("TITRE", article.getTitle()); 
     arguments.putString("SOURCE", article.getSource()); 
     arguments.putString("DESCRIPTION", article.getDescription()); 
     Fragment fragment = new WebBrowserFragment(); 
     fragment.setArguments(arguments); 
     FragmentTransaction fragmentTransaction = getActivity().getFragmentManager().beginTransaction(); 
     fragmentTransaction.replace(R.id.content_frame, fragment); 
     fragmentTransaction.addToBackStack(null); 
     fragmentTransaction.commit(); 
    } 
} 

我試圖實現我的行動吧,這將是可見的菜單項,當用戶在列表視圖開始滾動我的應用程序

我想綁定按鈕的點擊與負責滾動到列表視圖的頂部的方法。

要做到這一點,我想用:

getListView().setSelectionAfterHeaderView(); 

這裏是一塊我的主要活動代碼:

@Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle item selection 
     if (mDrawerToggle.onOptionsItemSelected(item)) { 
      return true; 
     } 

     switch (item.getItemId()) { 
     case R.id.action_arrow_top : 

        // I would like to scroll to the top of my list here, but in this class (MainActivity) i don't have access to the list .. :( 
      AlsaceNewsFragment.goToTop(); 

      return true; 
     default: 
      return super.onOptionsItemSelected(item); 
     } 
    } 

這裏是goToTop方法在AlsaceNewFragment代碼(它擴展了ListFragment):

public static void goToTop() { 
     getListView().setSelectionAfterHeaderView(); 
    } 

但是我得到這個錯誤:

Cannot make a static reference to the non-static method getListView() from the type ListFragment 

也許有一個更好的簡單方法來做我想做的事。

我的應用程序是由上:

  • MainActivity延伸活動
  • 一個ListFragment其創建列表
  • 的AlsaceAdapter延伸ArrayAdapter

非常感謝您的幫助

+1

閱讀關於靜態關鍵字:http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html – nhaarman

回答

0

嘗試從g中刪除static關鍵字oToTop()方法。靜態方法不能訪問實例數據。

+0

當我刪除靜態keywork時,我的MainActivity類中出現錯誤:'Can not對類型爲AlsaceNewsFragment的非靜態方法goToTop()進行靜態引用 – wawanopoulos

+0

您是否有權訪問AlsaceNewsFragment類的部分代碼?也許你可以擺脫所有這些靜態關鍵字,問題可能會消失。 – nio

+0

我在我的文章中添加了代碼。你能看看嗎? – wawanopoulos