アクティビティ内のフローティング アクション ボタンの動作をその子と調整する際に問題があります。
私のアプリでは、次のライブラリで構築された aと定義済みActivity
を収容する があります。には柔軟な数の が含まれており、それぞれの中にがあります。ViewPager
xml
FloatingActionMenu
ViewPager
Fragments
Fragment
RecyclerView
クリックすると、FloatingActionMenu
が 2 つのボタンに展開されます。1 つは に余分なページ/フラグメントを追加し、もう 1 つはユーザーが現在いるページの にViewPager
余分な行を追加します。この機能は機能します。RecyclerView
ViewPager
ボタンを非表示にするために、子フラグメントでを使用addOnItemTouchListener
します。RecyclerView
次に、 でOnInterceptTouchEvent
、スワイプ ジェスチャが十分に大きい場合、カスタムを使用しInterface
てイベントをホスティング に中継し、ホスティングActivity
はアニメーションを実行してボタンを表示または非表示にします。これが私のコードの一部です:
public class Categories extends ActionBarActivity implements
View.OnClickListener,
FloatingButtonInterface{
//...
// Final parameter is for the custom Interface
mAdapter = new CategoriesViewPagerAdapter(mFragmentManager, allCategoriesCursor, this);
mViewPager = (ViewPager) findViewById(R.id.categories_view_pager_pager);
mViewPager.setAdapter(mAdapter);
menuOriginalXPos = mFloatingActionsMenu.getX();
menuOriginalYPos = mFloatingActionsMenu.getY();
//...
// onTouchEvent is the imaginative name for my custom Interface method.
@Override
public void onTouchEvent(int callBackID) {
if (callBackID == FloatingButtonInterface.MOTION_DOWN){
if (mFloatingActionsMenu.getVisibility() == View.GONE) {
Animation translate = new TranslateAnimation(
menuOriginalXPos, menuOriginalXPos, mViewPager.getHeight(), menuOriginalYPos);
translate.setDuration(250);
translate.setInterpolator(new LinearInterpolator());
mFloatingActionsMenu.startAnimation(translate);
mFloatingActionsMenu.setEnabled(true);
mFloatingActionsMenu.setVisibility(View.VISIBLE);
}
} else if (callBackID == FloatingButtonInterface.MOTION_UP) {
if (mFloatingActionsMenu.getVisibility() == View.VISIBLE) {
Animation translate = new TranslateAnimation(
menuOriginalXPos, menuOriginalXPos, menuOriginalYPos, mViewPager.getHeight());
translate.setDuration(250);
translate.setInterpolator(new LinearInterpolator());
mFloatingActionsMenu.startAnimation(translate);
mFloatingActionsMenu.setEnabled(false);
mFloatingActionsMenu.setVisibility(View.GONE);
}
}
}
これが私の非常に簡単なインターフェースです:
public interface FloatingButtonInterface {
public static final int MOTION_UP = 4;
public static final int MOTION_DOWN = MOTION_UP+1;
public static final int MOTION_NONE = 0;
public void onTouchEvent(int callBackID);
}
私の中でViewPager
:
public class CategoriesViewPagerAdapter extends FragmentStatePagerAdapter {
private static final String LOG_TAG = "CategoriesViewPagerAd";
private Cursor mCursor;
private FloatingButtonInterface hideMenuInterface;
private FragmentManager mFragmentManager;
private boolean refreshItem = false;
public CategoriesViewPagerAdapter(FragmentManager fm, Cursor data, FloatingButtonInterface hideMenuInterface) {
super(fm);
mCursor = data;
mFragmentManager = fm;
this.hideMenuInterface = hideMenuInterface;
}
@Override
public Fragment getItem(int i) {
mCursor.moveToPosition(i);
int categoryFragmentIdentifier =
mCursor.getInt(mCursor.getColumnIndex(FilesDatabaseHelper.KEY_ID));
CategoryFragment frag = CategoryFragment.newInstance(categoryFragmentIdentifier);
frag.setOnTouchEvent(hideMenuInterface);
return frag;
}
そして最後に、ViewPager
Fragment
次のいずれか:
public class CategoryFragment extends Fragment implements
RecyclerView.OnItemTouchListener,
View.OnClickListener{
//...
private static final int TOUCH_SLOP=200;
//...
static CategoryFragment newInstance(int categoryIdentifier){
CategoryFragment frag = new CategoryFragment();
Bundle bundle = new Bundle();
bundle.putInt(CATEGORY_ID, categoryIdentifier);
frag.setArguments(bundle);
return frag;
}
//...
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mRecyclerView.addOnItemTouchListener(this);
//...
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
switch (e.getAction()){
case MotionEvent.ACTION_DOWN:
originalFingerPos = e.getY();
break;
case MotionEvent.ACTION_MOVE:
if (e.getY() - originalFingerPos > TOUCH_SLOP){
if (hideMenuInterface != null) {
hideMenuInterface.onTouchEvent(FloatingButtonInterface.MOTION_DOWN);
} else {
Log.e(LOG_TAG, "hideMenuInterface is null");
}
} else if (e.getY() + TOUCH_SLOP < originalFingerPos ){
if (hideMenuInterface != null) {
hideMenuInterface.onTouchEvent(FloatingButtonInterface.MOTION_UP);
} else {
Log.e(LOG_TAG, "hideMenuInterface is null");
}
}
}
return false;
}
私が抱えている主な問題は、構成の変更後にユーザーが画面を上下にスワイプしたときにボタンを自動的に非表示にするために使用するコードに関連しています。テストデバイスで画面の向きを変更すると、スワイプでボタンを非表示にしようとすると、一部のボタンが反応しFragments
ません。これに対処するにはどうすればよいですか? すべてのフラグメントではなく一部のフラグメントのみに影響するのはなぜですか? ViewPager の最後のフラグメントにスワイプしてからもう一度戻ると、フラグメントが再作成されたため、問題は解決しました。LogCat
hideMenuInterface
null
あなたが提供できる洞察をありがとう!関連性があると思われるコードのみを含めました。それ以外の場合はページが存在するため、他に何か役立つと思われるかどうかを尋ねてください.