Доброго!
Пытаюсь наладить работу с фрагментами, делал по уроку и всё работает, но мне не нравится, что код в куче:
package www.finefood.main;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import www.finefood.CreateNewUser.CreateNewUser;
import www.finefood.R;
import www.finefood.TestData.TestData;
import www.finefood.main.Fragment.AbstractionFragment;
import www.finefood.main.Fragment.CreateMenuFragment;
import www.finefood.main.Fragment.SettingMenuFragment;
import www.finefood.main.Fragment.WatchProgressFragment;
public class MainActivity extends FragmentActivity {
private String[] titles;
private ListView drawerList;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
titles = getResources().getStringArray(R.array.menu_fragment);
drawerList = (ListView) findViewById(R.id.drawer);
drawerList.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_activated_1, titles));
drawerList.setOnItemClickListener(new DrawerItemClickListener());
}
@Override
protected void onStart() {
super.onStart();
//Заплатака
if (!TestData.testNewUser)
//Заплатка
{
Intent intent = new Intent(getApplicationContext(), CreateNewUser.class);
startActivity(intent);
this.finish();
}
}
/////////////////////////////////////////////////////////////////////////////////
private class DrawerItemClickListener implements ListView.OnItemClickListener {
FragmentTransaction transaction;
FragmentManager manager;
AbstractionFragment createMenuFragment = new CreateMenuFragment();
AbstractionFragment watchProgressFragment = new WatchProgressFragment();
AbstractionFragment settingMenuFragment = new SettingMenuFragment();
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
manager = getSupportFragmentManager();
transaction = manager.beginTransaction();
switch (position) {
case 0:
if (manager.findFragmentByTag("CreateMenuFragment") == null)
transaction.replace(R.id.container, createMenuFragment, createMenuFragment.getTitle());
break;
case 1:
if (manager.findFragmentByTag("WatchProgressFragment") == null)
transaction.replace(R.id.container, watchProgressFragment, watchProgressFragment.getTitle());
break;
case 2:
if (manager.findFragmentByTag("SettingMenuFragment") == null)
transaction.replace(R.id.container, settingMenuFragment, settingMenuFragment.getTitle());
break;
}
transaction.commit();
}
}
}
хочу отделить класс DrawerItemClickListener, но проблема, не могу получить manager = getSupportFragmentManager();
подчёркивается красным и не собирается соответсвенно
package www.finefood.main.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import www.finefood.R;
public class DrawerItemClickListener implements ListView.OnItemClickListener {
FragmentTransaction transaction;
FragmentManager manager;
AbstractionFragment createMenuFragment = new CreateMenuFragment();
AbstractionFragment watchProgressFragment = new WatchProgressFragment();
AbstractionFragment settingMenuFragment = new SettingMenuFragment();
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
manager = getSupportFragmentManager();//тут проблема
transaction = manager.beginTransaction();
switch (position) {
case 0:
if (manager.findFragmentByTag("CreateMenuFragment") == null)
transaction.replace(R.id.container, createMenuFragment, createMenuFragment.getTitle());
break;
case 1:
if (manager.findFragmentByTag("WatchProgressFragment") == null)
transaction.replace(R.id.container, watchProgressFragment, watchProgressFragment.getTitle());
break;
case 2:
if (manager.findFragmentByTag("SettingMenuFragment") == null)
transaction.replace(R.id.container, settingMenuFragment, settingMenuFragment.getTitle());
break;
}
transaction.commit();
}
}
Собственно весь вопрос в том, можно ли получить ссыль на FragmentManager без передачи в конструктор FragmentManager отдельно в новый класс DrawerItemClickListener?