編集: 私のスライド タブ フラグメント ATabs に問題があるようです。ATabs フラグメントを使用せずに、ナビゲーション ドロワーを使用してフラグメント A1 と B を切り替えるだけで、すべて正常に動作します。ListItems の追加が可能です。編集された MainActivity:
@Override
public void onNavigationDrawerItemSelected(int position) {
mFragment = null;
switch (position) {
case 0:
mFragment = A1.newInstance(position + 1, this);
break;
case 1:
mFragment = B.newInstance(position + 1);
break;
}
onSectionAttached(position);
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, mFragment)
.commit();
mFragment = null;
}
しかし、ATabs フラグメント (以下の MainActivity を参照) を使用して切り替えて元に戻すとすぐに、ListItems を追加できなくなります。Log.v(LOG_TAG, "Added Item");
追加(+)を押すたびにLogcatが表示されますが。
何が問題の原因なのかわかりません。ATabs がフラグメントではなくアクティビティである必要がある場合、または PagerAdapter が問題を引き起こしている場合。私の ATabs クラスでは、2 つの異なる PagerAdapter を使用してみました。
public static class MyAdapter extends FragmentStatePagerAdapter {
...
}
と
public static class MyAdapter extends FragmentPagerAdapter {
...
}
後者の場合、フラグメント全体が正しく表示されず、タブ間の切り替えもできませんでした。
2 つの異なるフラグメント ATabs と B を表示するナビゲーション ドロワーがあります。フラグメント ATabs には 2 つのスライド タブが含まれています。各タブには、別のフラグメント A1 と A2 が保持されます。今まで A2 と B は単純な TextView を持つ単なる空のフラグメントなので、基本的に同じ A2 = B です。フラグメント A1 には、アプリの起動時にListView
いくつかの事前定義された項目が含まれています。EditText
ユーザーは、この特定のフラグメント (A1) の「カスタム」アクション バー内で [追加] を押すことにより、追加の EditText アイテムを追加できます。
一般に、フラグメント A1 は、フラグメント A2 および B とは異なるアクション バー項目を持つ必要があります。これは既に機能しているようです。そのために、各フラグメントにセットを追加し、リソースから目的のメニューを含めて設定するメソッドをsetHasOptionsMenu(true);
オーバーライドします。onCreateOptionsMenu
menu.clear()
これが私の最初の質問です:とにかく、デフォルトのメインアクティビティアクションバーを使用するすべてのフラグメント内でこれを行う必要がありますか?
そして、私の主な問題は、リスト ビューに項目を追加することです。
アプリの起動時に表示される最初のフラグメント A1 からフラグメント A2 に切り替えて、ATabs フラグメント内の A1 に戻ると、すべて正常に動作します。アイテムを追加でき、以前に追加したアイテムも引き続き表示されます。しかし、ナビゲーション ドロワーを使用してフラグメント B と A1 を切り替えると、アイテムを追加できなくなり、以前に追加したアイテムも表示されません。後でデータベースを使用してアイテムを保存しますが、以前と同様にアイテムを追加することも可能です。
私の問題をよりよく示すためのいくつかの画面を次に示します。
アプリはA1フラグメントで起動、A1とA2を切り替え、+アイテムで追加可能
ナビゲーション ドロワーを使用して ATabs と B を切り替えた後、追加項目 (+) が表示されますが、フラグメント A1 には影響しません。
ナビゲーション ドロワーを保持する MainActivity は次のとおりです。フラグメント間の
内部の切り替えonNavigationDrawerItemSelected
が正しいかどうか、または切り替えるたびに新しいフラグメントを作成するかどうかはよくわかりません。Android は以前のフラグメントを削除していますか、それとも複数回の切り替え後にメモリ オーバーフローを引き起こしていますか? そして、私の「最初の」A1フラグメントは、新しく作成されたフラグメントの「後ろに隠れていますか?」私は Android にまったく慣れていないので、ナビゲーション ドロワー フラグメントを切り替えた後にアイテムを追加できない理由が本当にわかりません。
public class MainActivity extends ActionBarActivity
implements NavigationDrawerFragment.NavigationDrawerCallbacks, A1.OnFragmentInteractionListener, A2.OnFragmentInteractionListener, B.OnFragmentInteractionListener {
private NavigationDrawerFragment mNavigationDrawerFragment;
private CharSequence mTitle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNavigationDrawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
}
@Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
fragment = ATabs.newInstance(position + 1, this);
break;
case 1:
fragment = B.newInstance(position + 1);
break;
}
onSectionAttached(position);
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, fragment)
.commit();
}
public void onSectionAttached(int number) {
switch (number+1) {
case 1:
mTitle = getString(R.string.title_ATabs);
break;
case 2:
mTitle = getString(R.string.title_B);
break;
}
}
public void restoreActionBar() {
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle(mTitle);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
if (!mNavigationDrawerFragment.isDrawerOpen()) {
// Only show items in the action bar relevant to this screen
// if the drawer is not showing. Otherwise, let the drawer
// decide what to show in the action bar.
getMenuInflater().inflate(R.menu.main, menu);
restoreActionBar();
return true;
}
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onFragmentInteraction(Uri uri) {
}
}
スライド タブ付きのフラグメント ATab:
public class ATabs extends Fragment {
static final String LOG_TAG = "ATabs";
private static final String ARG_PARAM1 = "param1";
private static Context mContext;
private SlidingTabLayout mSlidingTabLayout;
private ViewPager mViewPager;
public static ATabs newInstance(int sectionNumber, Context context) {
ATabs fragment = new ATabs();
mContext = context;
Bundle args = new Bundle();
args.putInt(ARG_PARAM1, sectionNumber);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.main, menu);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_atabs, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
mViewPager = (ViewPager) view.findViewById(R.id.viewpager);
mViewPager.setAdapter(new MyAdapter(getFragmentManager()));
mSlidingTabLayout = (SlidingTabLayout) view.findViewById(R.id.sliding_tabs);
mSlidingTabLayout.setViewPager(mViewPager);
}
public static class MyAdapter extends FragmentStatePagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
@Override
public int getCount() {
return 2;
}
@Override
public Fragment getItem(int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment = A1.newInstance(position, mContext);
break;
case 1:
fragment = A2.newInstance(position);
break;
}
return fragment;
}
@Override
public CharSequence getPageTitle(int position) {
CharSequence pageTitle = "NOT SET";
switch (position) {
case 0:
pageTitle = "A1 Tab";
break;
case 1:
pageTitle = "A2 Tab";
break;
}
return pageTitle;
}
}
}
すでに質問されているように:デフォルトのメイン アクティビティ アクション バーを使用するフラグメント内で呼び出しsetHasOptionsMenu(true)
てオーバーライドする必要がありますか?onCreateOptionsMenu()
以下は、デフォルトのメイン アクティビティ アクション バーを使用したフラグメント A2 (クラス B と同じ) です。
public class A2 extends Fragment {
private static final String ARG_PARAM1 = "param1";
private String mParam1;
private OnFragmentInteractionListener mListener;
public static A2 newInstance(int sectionNumber) {
A2 fragment = new A2();
Bundle args = new Bundle();
args.putInt(ARG_PARAM1, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public A2() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
}
setHasOptionsMenu(true);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.main, menu);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_a2, container, false);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnFragmentInteractionListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
public void onFragmentInteraction(Uri uri);
}
}
フラグメント A1 は、独自の a1 アクション バー メニュー(追加の追加項目) を使用します。
public class A1 extends Fragment {
private static final String ARG_PARAM1 = "param1";
private final String LOG_TAG = A1.class.getSimpleName();
private ArrayAdapter<String> mForecastAdapter;
private static Context mContext = null;
private ListView myList;
private MyAdapter myAdapter;
private String mParam1;
private OnFragmentInteractionListener mListener;
public static A1 newInstance(int sectionNumber, Context context) {
A1 fragment = new A1();
mContext = context;
Bundle args = new Bundle();
args.putInt(ARG_PARAM1, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public A1() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
}
setHasOptionsMenu(true);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
menu.clear();
inflater.inflate(R.menu.a1, menu);
}
public ArrayList myItems = new ArrayList();
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// handle item selection
switch (item.getItemId()) {
case R.id.action_add_item:
ListItem listItem = new ListItem();
listItem.caption = "Added Item";
myItems.add(listItem);
Log.v(LOG_TAG, "Added Item");
myAdapter.notifyDataSetChanged();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public class MyAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public MyAdapter() {
mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for (int i = 0; i < 3; i++) {
ListItem listItem = new ListItem();
listItem.caption = "Caption" + i;
myItems.add(listItem);
}
notifyDataSetChanged();
}
public int getCount() {
return myItems.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.list_item_a1, null);
holder.caption = (EditText) convertView
.findViewById(R.id.list_item_a1_edit_text);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
//Fill EditText with the value you have in data source
ListItem item = (ListItem) myItems.get(position);
holder.caption.setText(item.caption);
holder.caption.setId(position);
//we need to update adapter once we finish with editing
holder.caption.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus){
final int position = v.getId();
final EditText Caption = (EditText) v;
ListItem item = (ListItem) myItems.get(position);
item.caption = Caption.getText().toString();
}
}
});
return convertView;
}
}
class ViewHolder {
EditText caption;
}
class ListItem {
String caption;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_a1, container, false);
// Get a reference to the ListView, and attach this adapter to it.
myList = (ListView) rootView.findViewById(R.id.listview_a1);
myList.setItemsCanFocus(true);
myAdapter = new MyAdapter();
myList.setAdapter(myAdapter);
return rootView;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnFragmentInteractionListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
public void onFragmentInteraction(Uri uri);
}
}
そして最後にアクションバーのメニュー: main:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item
android:id="@+id/action_example"
android:title="@string/action_example"
app:showAsAction="never" />
<item
android:id="@+id/action_settings"
android:title="@string/action_settings"
android:orderInCategory="100"
app:showAsAction="never" />
<item
android:id="@+id/action_help"
android:title="@string/action_help"
android:orderInCategory="100"
app:showAsAction="never" />
</menu>
a1:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item
android:id="@+id/action_add_item"
android:title="@string/action_add_item"
android:icon="@drawable/ic_action_new"
app:showAsAction="always" />
<item
android:id="@+id/action_example"
android:title="@string/action_example"
app:showAsAction="never" />
<item
android:id="@+id/action_settings"
android:title="@string/action_settings"
android:orderInCategory="100"
app:showAsAction="never" />
<item
android:id="@+id/action_help"
android:title="@string/action_help"
android:orderInCategory="100"
app:showAsAction="never" />
</menu>
私の問題を解決するために他のコードが必要な場合はお知らせください。