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

我尝试在我的操作栏中实现一个新菜单项,当用户在我的应用程序的 listView 中开始滚动时,该菜单项将可见。

我想将按钮的单击与负责滚动到列表视图顶部的方法绑定。

为此,我想使用:

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

这是 AlsaceNewFragment 中 goToTop 方法的代码(它扩展了 ListFragment):

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

但我得到这个错误:

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

也许有更好的简单方法来做我想做的事。

我的应用程序由以下组成:

  • 扩展 Activity 的 MainActivity
  • 一个创建列表的 ListFragment
  • 扩展 ArrayAdapter 的 AlsaceAdapter

非常感谢你的帮助

4

1 回答 1

0

尝试static从 goToTop() 方法中删除关键字。静态方法不能访问实例数据。

于 2013-08-04T12:54:04.940 回答