[アップデート]
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 (ListFragment を拡張) の goToTop メソッドのコードは次のとおりです。
public static void goToTop() {
getListView().setSelectionAfterHeaderView();
}
しかし、私はこのエラーが発生します:
Cannot make a static reference to the non-static method getListView() from the type ListFragment
たぶん、私がやりたいことを行うためのより簡単な方法があります。
私のアプリは以下で構成されています:
- Activity を拡張する MainActivity
- リストを作成する 1 つの ListFragment
- ArrayAdapter を拡張する AlsaceAdapter
助けてくれてどうもありがとう