ActionMode를 만들 때 onPrepareActionMode가 호출되지 않음
방금 내 앱 중 하나를 새로운 v22.1.1 지원 및 appcompat 라이브러리에 맞게 조정했습니다 . 자세한 내용 은 여기 와 여기 를 참조하세요. 몇 가지 테스트를 수행했을 때 사용중인 ActionMode 에 문제가 있었습니다 .
호출을 사용하여 ActionMode 를 시작할 때 startSupportActionMode()-현재 사용되지 않는 ActionBarActivity 기본 클래스 를 사용하는지 또는 새 AppCompatActivity 기본 클래스 를 사용하는지 여부는 중요하지 않습니다 onPrepareActionMode().
v21.0.3 및 v22.0.0을 포함한 이전 버전에서는 onPrepareActionMode()ActionMode가 처음에 startSupportActionMode().
2.2, 4.4.2 및 5.0 장치에서 테스트 했으므로 버전에 의존하지 않는 것 같습니다.
이것이 의도 된 동작이라면 v22.1.1에 도입되었거나 버그라는 것을 아는 사람이 있습니까?
이 문제를 발견 했지만 여기에 많은 피드백이 없습니다 ...
2015 년 5 월 11 일 수정 :
Android 문제 추적기 159527 에서 언급 했듯이이 문제는 appcompat의 v22.1.x 및 지원 라이브러리뿐만 아니라 5.1 Android 구현에도 영향을 미칩니다.
현재 가능한 두 가지 임시 솔루션, 일반적인 솔루션 :
@Override
public ActionMode startSupportActionMode(final ActionMode.Callback callback) {
// Fix for bug https://code.google.com/p/android/issues/detail?id=159527
final ActionMode mode = super.startSupportActionMode(callback);
if (mode != null) {
mode.invalidate();
}
return mode;
}
그리고 '빠르고 더러운'것 (ActionMode를 인스턴스화 할 때) :
final ActionMode actionMode = startSupportActionMode(new MyActionMode());
if(actionMode != null) {
actionMode.invalidate();
}
당신이 APPCOMPAT 사용하지 않는 경우 ( ActionBarActivity/ AppCompatActivity)는 교체해야 startSupportActionMode()와 함께 startActionMode().
불행히도 이것이 의도 된 새로운 동작인지 버그인지는 아직 명확하지 않습니다. API 문서 에 따르면 버그 / 회귀이지만 누가 알겠습니까?
데모를 만들었고 제대로 작동하며, onPrepareActionMode는 작업 모드가 표시 될 때마다 호출됩니다. 항상 onCreateActionMode 이후에 호출되지만 모드가 무효화되면 여러 번 호출 될 수 있습니다. [ 누구에게나 작은 수정을 요청합니다. 툴바와 동일한 상태 바 색상이 필요하지만 동적으로 불필요한 Drawyer Layout이이 효과를 얻기 위해 사용되는 것을 볼 수 있지만, 드로어 레이아웃을 제거하면 툴바 색상에 따라 상태 바 색상이 변경되지 않습니다. 유틸리티에서 기본적으로 기본 테마 색상은 빨간색이고 툴바는 처음에는 빨간색으로 표시되지만 상태 표시 줄은 그렇지 않으며 서랍 레이아웃을 제거하는 경우에만 표시됩니다. 스타일을 사용하여이 작업을 수행해야합니다. ] 리소스 레이아웃을 만들고 이름을 => action_mode_activity로 지정합니다.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/my_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<LinearLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical"
app:insetForeground="#4000">
<include
android:id="@+id/toolbar"
layout="@layout/toolbar" />
<EditText
android:id="@+id/editTextCopy"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_marginTop="19dp"
android:ems="10"
android:inputType="textMultiLine"
android:text="Long click to share!">
<requestFocus />
</EditText>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
활동 이름을 ActionModeActivity로 작성하십시오.
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.example.deepakpawar.demolearning.R;
import com.example.deepakpawar.demolearning.demo.load.recycler.Utils;
/**
* Created by Deepak Pawar on 9/24/2015.
*/
public class ActionModeActivity extends AppCompatActivity implements View.OnLongClickListener, ActionMode.Callback {
EditText editTextCopy;
android.view.ActionMode mActionMode;
private Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Utils.onActivityCreateSetTheme(this);
setContentView(R.layout.action_mode_activity);
// 1. Get the editText
editTextCopy = (EditText) findViewById(R.id.editTextCopy);
// 2. add long-click listener
editTextCopy.setOnLongClickListener(this);
toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
actionBar.setTitle("Android Students");
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}
}
@Override
public boolean onLongClick(View view) {
// if actionmode is null "not started"
if (mActionMode != null) {
return false;
}
// Start the CAB
mActionMode = this.startActionMode(this);
view.setSelected(true);
return true;
}
// 4. Called when the action mode is created; startActionMode() was called
@Override
public boolean onCreateActionMode(android.view.ActionMode mode, Menu menu) {
// Inflate a menu resource providing context menu items
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.action_menu, menu);
return true;
}
// 5. Called when the user click share item
@Override
public boolean onActionItemClicked(android.view.ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.action_share:
Toast.makeText(this, "Shared!", Toast.LENGTH_SHORT).show();
mode.finish(); // Action picked, so close the CAB
return true;
default:
return false;
}
}
// 6. Called each time the action mode is shown. Always called after onCreateActionMode, but
// may be called multiple times if the mode is invalidated.
@Override
public boolean onPrepareActionMode(android.view.ActionMode mode, Menu menu) {
Toast.makeText(ActionModeActivity.this,"onPrepareActionMode Called ",Toast.LENGTH_SHORT).show();
return false; // Return false if nothing is done
}
// 7. Called when the user exits the action mode
@Override
public void onDestroyActionMode(android.view.ActionMode mode) {
mActionMode = null;
}
}
// 테마를 변경하는 메서드가있는 유틸리티 클래스 // 앱 테마를 동적으로 변경해야하므로 생성했습니다. import android.app.Activity;
public class Utils {
private static int sTheme;
public final static int THEME_DEFAULT = 0;
public final static int THEME_WHITE = 1;
public final static int THEME_BLUE = 2;
/**
* Set the theme of the Activity, and restart it by creating a new Activity of the same type.
*/
public static int getsTheme() {
return sTheme;
}
public static void changeToTheme(Activity activity, int theme) {
sTheme = theme;
activity.recreate();
// activity.finish();
// activity.startActivity(new Intent(activity, activity.getClass()));
}
/**
* Set the theme of the activity, according to the configuration.
*/
public static void onActivityCreateSetTheme(Activity activity) {
switch (sTheme) {
default:
case THEME_DEFAULT:
activity.setTheme(R.style.FirstTheme);
break;
case THEME_WHITE:
activity.setTheme(R.style.SecondTheme);
break;
case THEME_BLUE:
activity.setTheme(R.style.Thirdheme);
break;
}
}
}
v21-themes.xml
<resources>
<style name="AppTheme" parent="AppTheme.Base">
<item name="android:windowContentTransitions">true</item>
<item name="android:windowAllowEnterTransitionOverlap">true</item>
<item name="android:windowAllowReturnTransitionOverlap">true</item>
<item name="android:windowSharedElementEnterTransition">@android:transition/move</item>
<item name="android:windowSharedElementExitTransition">@android:transition/move</item>
<item name="android:actionOverflowButtonStyle">@style/Widget.ActionButton.Overflow</item>
<!-- <item name="android:navigationBarColor">@color/PrimaryColor</item>-->
<item name="windowActionBar">false</item>
<item name="windowActionModeOverlay">true</item>
<!-- To Make Navigation Drawer Fill Status Bar and become Transparent Too -->
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">@android:color/transparent</item>
<!--//if darker status bar needed-->
<!-- <item name="android:windowTranslucentStatus">true</item>-->
</style>
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/PrimaryColor</item>
<item name="colorPrimaryDark">@color/PrimaryDarkColor</item>
<item name="colorAccent">@color/AccentColor</item>
<item name="android:textColorPrimary">@color/TextPrimaryColor</item>
<item name="android:windowBackground">@color/WindowBackground</item>
</style>
<style name="Widget.ActionButton.Overflow" parent="@android:style/Widget.Holo.ActionButton.Overflow">
<item name="android:contentDescription">@string/accessibility_overflow</item>
</style>
<!-- style for the tool bar backgrounds -->
<style name="ToolBarStyle" parent="ToolBarStyle.Base" />
<style name="ToolBarStyle.Base" parent="">
<item name="popupTheme">@style/ThemeOverlay.AppCompat.Light</item>
<item name="theme">@style/ThemeOverlay.AppCompat.Dark.ActionBar</item>
</style>
<style name="ToolBarStyle.Event" parent="ToolBarStyle">
<item name="titleTextAppearance">@style/TextAppearance.Widget.Event.Toolbar.Title</item>
</style>
<style name="TextAppearance.Widget.Event.Toolbar.Title" parent="TextAppearance.Widget.AppCompat.Toolbar.Title">
<!--Any text styling can be done here-->
<item name="android:textStyle">normal</item>
<item name="android:textSize">18sp</item>
<item name="android:textColor">#000000</item>
</style>
<!-- Customize your theme example here. -->
<style name="FirstTheme">
<item name="android:textColor">#FF0000</item>
<item name="colorPrimary">#FF0000</item>
<item name="colorPrimaryDark">#ff0000</item>
<item name="colorAccent">#ff0087</item>
<item name="android:shadowColor">#00ccff</item>
<item name="android:shadowRadius">1.5</item>
<item name="android:shadowDy">1</item>
</style>
<style name="SecondTheme">
<item name="android:textColor">#00FF00</item>
<item name="colorPrimary">#00FF00</item>
<item name="colorPrimaryDark">#00FF00</item>
<item name="colorAccent">#00FF90</item>
<item name="android:shadowColor">#00ccff</item>
<item name="android:shadowRadius">1.5</item>
<item name="android:shadowDy">1</item>
</style>
<style name="Thirdheme">
<item name="android:textColor">#0000F0</item>
<item name="colorPrimary">#0000F0</item>
<item name="colorPrimaryDark">#0000F0</item>
<item name="colorAccent">#0090F0</item>
<item name="android:shadowColor">#00ccff</item>
<item name="android:shadowRadius">1.5</item>
<item name="android:shadowDy">1</item>
</style>
<style name="AppCompatAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="colorAccent">#FFCC00</item>
<item name="android:textColorPrimary">#FFFFFF</item>
<item name="android:background">#5fa3d0</item>
</style>
</resources>
비슷한 문제가있었습니다.
build.gradle의 "compileSdkVersion"및 "buildToolsVersion"값을 높인 후 onPrepareActionMode가 호출되지 않았 음을 알았습니다.
- compileSdkVersion : 21에서 26
- buildToolsVersion : 21.1.2 ~ 26.0.2
그래서 코드를 (A)에서 (B)로 옮겼습니다. (아래를 봐주세요)
그것이 올바른 해결책인지 확실하지 않지만 작동합니다.
다음은 내 코드의 일부입니다.
list1 = findViewById(R.id.listView1);
list1.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
//(A)
//MenuItem menuItem1 = menu.findItem(R.id.menu_item1);
//menuItem1.setVisible(false);
return false;
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_action_mode, menu);
//(B)
MenuItem menuItem1 = menu.findItem(R.id.menu_item1);
menuItem1.setVisible(false);
return true;
}
참고 URL : https://stackoverflow.com/questions/29927534/onprepareactionmode-not-called-when-creating-actionmode
'Programing' 카테고리의 다른 글
| CoreData + iCloud + Cascade Delete-처리 방법? (0) | 2020.10.13 |
|---|---|
| Heroku가 HTTP 응답을 자릅니다. (0) | 2020.10.13 |
| Hadoop 잘림 / 일관되지 않은 카운터 이름 (0) | 2020.10.13 |
| MySQL / 쓰기 파일 오류 (Errcode 28) (0) | 2020.10.12 |
| ActionSheet가 작동하지 않는 iPad (0) | 2020.10.12 |