[UPDATE]
以下是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
也许有一种更简单的方法可以做我想要的事情。
我的应用程序包含在:
非常感谢你的帮助
答案 0 :(得分:0)
尝试从goToTop()方法中删除static
关键字。静态方法无法访问实例数据。