我最近更新了我的应用程序以使用AppCompat(v7-22)库,以便我可以在所有设备上为我的应用程序提供Material Design外观。除了一项活动外,所有工作都很好。我有一个BaseActivity,代码如下:
package com.waldm.proverbica;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
public abstract class BaseActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutResource());
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(hasParentActivity());
}
}
protected abstract boolean hasParentActivity();
protected abstract int getLayoutResource();
}
我的所有活动都扩展了我的BaseActivity。 我有一个活动FavouritesActivity,其布局是:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="@layout/toolbar"/>
<fragment
android:name="com.waldm.proverbica.favourites.FavouritesFragment"
android:id="@+id/fragment_favourites"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
,其代码为:
package com.waldm.proverbica.favourites;
import com.waldm.proverbica.BaseActivity;
import com.waldm.proverbica.R;
public class FavouritesActivity extends BaseActivity{
@Override
protected boolean hasParentActivity() {
return true;
}
@Override
protected int getLayoutResource() {
return R.layout.activity_favourites;
}
}
我有一个扩展ListFragment的FavouritesFragment类。 在FavouritesFragment的onResume方法中,我执行以下操作:
@Override
public void onResume() {
super.onResume();
List<String> favourites = FavouritesIO.readFavourites(getActivity());
list = Lists.newArrayList();
for (String favourite : favourites) {
list.add(ImmutableMap.of(PROVERB_KEY, favourite));
}
adapter = new SimpleAdapter(getActivity(), list, R.layout.list_item_favourites, new String[] { PROVERB_KEY },
new int[] { R.id.text });
setListAdapter(adapter);
getListView().setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE_MODAL);
getListView().setMultiChoiceModeListener(new MultiChoiceModeListener() {
@Override
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
updateShareIntent();
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_item_delete:
deleteSelectedItems();
mode.finish(); // Action picked, so close the CAB
return true;
default:
return false;
}
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.favourites, menu);
shareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(menu.findItem(R.id.menu_item_share));
return true;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
// Here you can make any necessary updates to the activity when
// the CAB is removed. By default, selected items are
// deselected/unchecked.
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// Here you can perform updates to the CAB due to
// an invalidate() request
return false;
}
});
}
片段的布局是:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="8dp"
android:paddingRight="8dp">
<ListView android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>
<TextView android:id="@android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:textSize="15sp"
android:text="@string/no_favourites"/>
</LinearLayout>
我遇到的问题是,在onCreateActionMode方法中,ShareActionProvider总是设置为null,而且我还会显示两个ActionBars / Toolbars,一个用于我的Fragment,另一个用于我的Activity。 / p>
有没有人有任何想法如何解决这个问题?
由于