从另一个DialogFragment启动后,全屏DialogFragment不可见

时间:2017-07-20 17:19:59

标签: android android-dialogfragment

我有3 DialogFragments A,B和C.我使用Fragment AActivity开始FragmentTransaction,如下所示:

FragmentA dialog = new FragmentA();
                Bundle bundle = new Bundle();
                bundle.putString("GameLevelType", "Puzzle");
                dialog.setArguments(bundle);
                FragmentTransaction transaction = ((AppCompatActivity) context).getSupportFragmentManager().beginTransaction();
                transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                transaction.add(android.R.id.content, dialog).addToBackStack(null).commit();

现在我从Fragment A的RecyclerView适配器中启动Fragment B,如下所示:

class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        TextView levelName;
        CardView gameCard;

        public MyViewHolder(View itemView) {
            super(itemView);
            levelName = (TextView) itemView.findViewById(R.id.gameText);
            gameCard = (CardView) itemView.findViewById(R.id.gameCard);
            gameCard.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            Handler handler = new Handler();
            handler.post(new Runnable() {
                @Override
                public void run() {
                    FragmentB fragment = new FragmentB();
                    FragmentTransaction gameTransaction = ((AppCompatActivity)context).getSupportFragmentManager().beginTransaction();
                    gameTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                    gameTransaction.add(android.R.id.content, fragment).addToBackStack(null).commit();
                    //fragment.show(((AppCompatActivity)context).getSupportFragmentManager(),"PuzzleDialog");
                }
            });
            //((AppCompatActivity) context).getSupportFragmentManager().popBackStack();
        }
    }

现在我从Fragment C开始Fragment B。但是Fragment C不可见。

以下是Fragment C

public class FragmentC extends DialogFragment implements OnStartDragListener {
    private ItemTouchHelper mItemTouchHelper;
    RecyclerView recyclerView;
    Integer[] str = {R.drawable.a1, R.drawable.a2, R.drawable.a3, R.drawable.a4, R.drawable.a5, R.drawable.a6, R.drawable.a7, R.drawable.a8, R.drawable.a9};
    List<Integer> itemList = new ArrayList<>(Arrays.asList(str));
    List<Integer> actualList = new ArrayList<>(Arrays.asList(str));
    RelativeLayout layout;
    PuzzleGridAdapter adapter;
    Toolbar toolbar;

    public PuzzleGameFragment() {

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.puzzle_game_dialog, container, false);
        toolbar = (Toolbar) rootView.findViewById(R.id.toolbar);
        toolbar.setTitle(R.string.puzzle);
        //toolbar.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
        ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
        ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setHomeButtonEnabled(true);
            actionBar.setHomeAsUpIndicator(android.R.drawable.ic_menu_close_clear_cancel);
        }
        setHasOptionsMenu(true);
        return rootView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        layout = (RelativeLayout) view.findViewById(R.id.puzzleLayout);
        recyclerView = (RecyclerView) view.findViewById(R.id.gridItems);
        adapter = new PuzzleGridAdapter(getActivity(), this);
        recyclerView.setHasFixedSize(true);
        Collections.shuffle(itemList);
        adapter.getItemList(itemList);
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 3));

        ItemTouchHelper.Callback callback = new SimpleItemTouchHelperCallback(adapter);
        mItemTouchHelper = new ItemTouchHelper(callback);
        mItemTouchHelper.attachToRecyclerView(recyclerView);

        recyclerView.setItemAnimator(new DefaultItemAnimator() {
            @Override
            public void onAnimationFinished(RecyclerView.ViewHolder viewHolder) {
                for (Integer s : adapter.getUpdateList()) {
                    Log.e("Final List", s + "");

                }
                int j = 0;
                for (int i = 0; i < adapter.getUpdateList().size(); i++) {
                    Log.e("ItemList", itemList.get(i) + " ");
                    if (adapter.getUpdateList().get(i).equals(actualList.get(i))) {
                        j++;
                    }
                }
                if (j == adapter.getUpdateList().size()) {
                    Log.e("Solved", "True");
                }
            }
        });
    }

    @Override
    public void onStartDrag(RecyclerView.ViewHolder viewHolder) {
        mItemTouchHelper.startDrag(viewHolder);
    }

    @Override
    public void onResume() {
        super.onResume();
        Calendar globalCalendar = Calendar.getInstance();
        int timeOfDay = globalCalendar.get(Calendar.HOUR_OF_DAY);

        if (timeOfDay >= 0 && timeOfDay < 12) {
            layout.setBackground(getResources().getDrawable(R.drawable.gradient_morning_background));
            toolbar.setBackgroundColor(getResources().getColor(R.color.colorPrimaryMorning));
        } else {
            layout.setBackground(getResources().getDrawable(R.drawable.gradient_background));
            toolbar.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
        }
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
        dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        return dialog;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        menu.clear();
        getActivity().getMenuInflater().inflate(R.menu.main_menu, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == android.R.id.home) {
            // handle close button click here
            Handler handler = new Handler();
            handler.post(new Runnable() {
                @Override
                public void run() {
                    getActivity().getSupportFragmentManager().popBackStack();
                }
            });
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

以下是fragment_c.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true"
    android:orientation="vertical">

    <RelativeLayout
        android:id="@+id/puzzleLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/toolbarLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay"
            app:elevation="0dp">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/AppTheme.PopupOverlay" />

        </android.support.design.widget.AppBarLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/toolbarLayout">

            <ImageView
                android:layout_width="200dp"
                android:layout_height="200dp"
                android:adjustViewBounds="true"
                android:alpha="0.5"
                android:scaleType="fitXY"
                android:src="@drawable/background_img" />

            <ImageView
                android:layout_width="200dp"
                android:layout_height="200dp"
                android:layout_alignParentBottom="true"
                android:layout_alignParentRight="true"
                android:adjustViewBounds="true"
                android:alpha="0.5"
                android:rotation="180"
                android:scaleType="fitXY"
                android:src="@drawable/background_img" />

            <android.support.v7.widget.RecyclerView
                android:id="@+id/gridItems"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:layout_margin="25dp" />
        </RelativeLayout>

    </RelativeLayout>

</android.support.v7.widget.CardView>

为什么Fragment C不可见?

0 个答案:

没有答案