全屏碎片不可见

时间:2017-07-19 19:13:42

标签: android android-fragments

我有Activity,其中包含RecyclerView。点击一个项目,我打开一个Full Screen Fragment。在Fragment里面我还有一个RecyclerView。点击Fragment中的一个项目后,我又开了一个新的Full Screen Fragment。但问题是新的Full Screen Fragment不可见。片段即将出现在屏幕上但不可见。

以下是活动的RecyclerView的适配器:

public class GameAdapter extends RecyclerView.Adapter<GameAdapter.MyViewHolder> {
    private LayoutInflater inflater;
    private Context context;
    private List<String> gameList;

    GameAdapter(Context context) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        gameList = new ArrayList<>();
    }

    public void getList(List<String> list) {
        int currentSize = gameList.size();
        gameList.clear();
        gameList.addAll(list);
        notifyItemRangeRemoved(0, currentSize);

        notifyItemRangeInserted(0, list.size());
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.custom_row, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.text.setText(gameList.get(position));
    }

    @Override
    public int getItemCount() {
        return gameList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        TextView text;
        CardView gameCard;

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

        @Override
        public void onClick(View view) {
            LevelDialog dialog = new LevelDialog();
            FragmentTransaction transaction = ((AppCompatActivity)context).getSupportFragmentManager().beginTransaction();
            transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
            transaction.add(android.R.id.content, dialog).addToBackStack(null).commit();
        }
    }
}

这是Fragment的RecyclerView的适配器:

public class LearningGameAdapter extends RecyclerView.Adapter<LearningGameAdapter.MyViewHolder> {
    private LayoutInflater inflater;
    private Context context;
    private List<String> learnGameList;
    Dialog dialog;

    LearningGameAdapter(Context context, Dialog dialog) {
        this.dialog = dialog;
        this.context = context;
        inflater = LayoutInflater.from(context);
        learnGameList = new ArrayList<>();
    }

    void getGameList(List<String> gameList) {
        int currentSize = learnGameList.size();
        learnGameList.clear();
        learnGameList.addAll(gameList);
        notifyItemRangeRemoved(0, currentSize);
        notifyItemRangeInserted(0, gameList.size());
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.custom_row, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.levelName.setText(learnGameList.get(position));
    }

    @Override
    public int getItemCount() {
        return learnGameList.size();
    }

    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) {
            FirstFragment fragment = new FirstFragment();
            Bundle bundle = new Bundle();
            FragmentTransaction transaction = ((AppCompatActivity) context).getSupportFragmentManager().beginTransaction();
            bundle.putInt("CurrentPosition", getAdapterPosition());
            bundle.putString("GameType", "Learn");
            fragment.setArguments(bundle);
            transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
            transaction.add(android.R.id.content, fragment).addToBackStack(null).commit();
            //((AppCompatActivity) context).getSupportFragmentManager().popBackStack();
        }
    }
}

以下是Full Screen Fragment即使在屏幕上显示后仍然无法显示:

public class FirstFragment extends DialogFragment {

    ConnectDotsView connectDotsView;
    ImageView refreshCanvas, shareTrace, helpTrace;
    int position = 0;
    String gameType = "";
    Toolbar toolbar;
    TracingGames games;

    KonfettiView konfettiView;
    boolean isDrawn = false;
    List<Point> points;

    View.OnClickListener myListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.refreshCanvas:
                    isDrawn = false;
                    connectDotsView.clear();
                    connectDotsView.setPoints(points);
                    break;
                case R.id.helpTrace:
                    HelpDialog dialog = new HelpDialog();
                    Bundle bundle = new Bundle();
                    bundle.putInt("Position", position);
                    bundle.putString("GameType",gameType);
                    dialog.setArguments(bundle);
                    dialog.show(getActivity().getSupportFragmentManager(), "HelpDialog");
                    break;
                case R.id.shareTrace:

                    break;
            }
        }
    };

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.first_fragment, container, false);
        toolbar = (Toolbar) rootView.findViewById(R.id.toolbar);
        ((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);
        position = getArguments().getInt("CurrentPosition");
        Log.e("Position",position+"");
        gameType = getArguments().getString("GameType");
        Log.e("GameType",gameType);
        connectDotsView = (ConnectDotsView) view.findViewById(R.id.connect_dots_view);
        refreshCanvas = (ImageView) view.findViewById(R.id.refreshCanvas);
        shareTrace = (ImageView) view.findViewById(R.id.shareTrace);
        helpTrace = (ImageView) view.findViewById(R.id.helpTrace);
        games = new TracingGames();

        konfettiView = (KonfettiView) view.findViewById(R.id.viewKonfetti);

        refreshCanvas.setOnClickListener(myListener);
        shareTrace.setOnClickListener(myListener);
        helpTrace.setOnClickListener(myListener);

        connectDotsView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                Rect rect = new Rect();

                int[] x = null;
                int[] y = null;
                if (gameType.equalsIgnoreCase("Learn")) {
                    switch (position) {
                        case 0:
                            x = games.getLevel1X();
                            y = games.getLevel1Y();
                            break;
                        case 1:
                            x = games.getLevel2X();
                            y = games.getLevel2Y();
                            break;
                        case 2:
                            x = games.getLevel3X();
                            y = games.getLevel3Y();
                            break;
                        case 3:
                            x = games.getLevel4X();
                            y = games.getLevel4Y();
                            break;
                        case 4:
                            x = games.getLevel5X();
                            y = games.getLevel5Y();
                            break;
                        case 5:
                            x = games.getLevel6X();
                            y = games.getLevel6Y();
                            break;
                        case 6:
                            x = games.getLevel7X();
                            y = games.getLevel7Y();
                            break;
                        case 7:
                            x = games.getLevel8X();
                            y = games.getLevel8Y();
                            break;
                    }
                }else if(gameType.equalsIgnoreCase("Game")){

                }
                // Fist find the min and max value for x axis
                int minX = x[0];
                int maxX = x[0];

                for (int i = 1; i <= x.length - 1; i++) {
                    if (maxX < x[i]) {
                        maxX = x[i];
                    }

                    if (minX > x[i]) {
                        minX = x[i];
                    }
                }

                // Find min and max vlaue for Y axis
                int minY = y[0];
                int maxY = y[0];

                for (int i = 1; i <= y.length - 1; i++) {
                    if (maxY < y[i]) {
                        maxY = y[i];
                    }

                    if (minY > y[i]) {
                        minY = y[i];
                    }
                }

                connectDotsView.getLocalVisibleRect(rect);
                Log.e("Width", rect.width() + "");
                Log.e("Height", rect.height() + "");
                Log.e("left", rect.left + "");
                Log.e("right", rect.right + "");
                Log.e("top", rect.top + "");
                Log.e("bottom", rect.bottom + "");

                // Find the scale factor based on the view you allocated in the screen
                float scaleX = ((float) ((float) rect.width() / (float) maxX));
                float scaleY = ((float) ((float) rect.height() / (float) maxY));

                final float scale;

                // Take the lowest scale factor
                if (scaleX > scaleY) {
                    scale = scaleY;
                } else {
                    scale = scaleX;
                }

                // find the left and top
                int left = (rect.width() - ((int) ((float) maxX * scale)) - ((int) ((float) minX * scale))) / 2;
                int top = (rect.height() - ((int) ((float) maxY * scale)) - ((int) ((float) minY * scale))) / 2;

                // base on the above calculation draw in a view
                List<Point> points = new ArrayList<>();
                for (int i = 0, j = 0; i < x.length && j < y.length; i++, j++) {
                    Point p = new Point(((int) ((float) x[i] * scale)) + left, (int) ((float) y[j] * scale) + top);
                    points.add(p);
                }
                connectDotsView.clear();
                FirstFragment.this.points = points;
                connectDotsView.setPoints(points);
            }
        });

        connectDotsView.setOnCompleteListener(new ConnectDotsView.CompleteListener() {
            @Override
            public void onCompleteListener() {
                if (!isDrawn) {
                    isDrawn = true;
                    konfettiView.build()
                            .addColors(Color.parseColor("#764ba2"), Color.parseColor("#f5576c"), Color.parseColor("#43e97b"))
                            .setDirection(0.0, 359.0)
                            .setSpeed(0.5f, 3f)
                            .setFadeOutEnabled(true)
                            .setTimeToLive(2000L)
                            .addShapes(Shape.RECT, Shape.CIRCLE)
                            .addSizes(new Size(12, 5f))
                            .setPosition(-50f, konfettiView.getWidth() + 50f, -50f, -50f)
                            .stream(300, 2200L);
                    Log.e("Clear", "cleared");
                }
            }
        });

    }

    @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
            getActivity().getSupportFragmentManager().popBackStack();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

}

2 个答案:

答案 0 :(得分:0)

<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"
    app:cardElevation="3dp"
    app:cardCornerRadius="3dp"
    android:clickable="true">

<!-- Content -->
</android.support.v7.widget.CardView>

添加CardView作为您的父布局将起到作用。即使我曾经遇到过这个问题。

答案 1 :(得分:0)

简单... 保持片段布局在父布局中是“相对布局”