1

アクティビティ内のフローティング アクション ボタンの動作をその子と調整する際に問題があります。

私のアプリでは、次のライブラリで構築された aと定義済みActivityを収容する があります。には柔軟な数の が含まれており、それぞれの中にがあります。ViewPagerxmlFloatingActionMenuViewPagerFragmentsFragmentRecyclerView

クリックすると、FloatingActionMenuが 2 つのボタンに展開されます。1 つは に余分なページ/フラグメントを追加し、もう 1 つはユーザーが現在いるページの にViewPager余分な行を追加します。この機能は機能します。RecyclerViewViewPager

ボタンを非表示にするために、子フラグメントでを使用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 の最後のフラグメントにスワイプしてからもう一度戻ると、フラグメントが再作成されたため、問題は解決しました。LogCathideMenuInterfacenull

あなたが提供できる洞察をありがとう!関連性があると思われるコードのみを含めました。それ以外の場合はページが存在するため、他に何か役立つと思われるかどうかを尋ねてください.

4

1 に答える 1