2

在我的场景中,如果我单击扩展卡,card_1那么所有其他卡都应该是不可见的。另外,我可以更改卡片运行时的位置,以便它可以将第二张卡片移动到第一张卡片的位置,并且它将扩展到全屏,因此无需隐藏其他卡片。

请检查所附图像。

在此处输入图像描述

public class DashboardMarketAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int VIEW_TYPE_TOP_MENU = 0;
    private static final int VIEW_TYPE_MARKET_MOVERS = 1;
    private static final int VIEW_TYPE_DERIVATIVES = 2;
    private static final int VIEW_TYPE_NEWS = 3;
    private final Context context;
    private final String[] menuItems;

    public DashboardMarketAdapter(Context context, String[] menuItems) {
        super();
        this.context = context;
        this.menuItems = menuItems;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v;
        switch (viewType) {
            case VIEW_TYPE_TOP_MENU:
                v = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_top_view, parent, false);
                return new TopMenuViewHolder(v, parent.getContext());

            case VIEW_TYPE_MARKET_MOVERS:
                v = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_market_movers, parent, false);
                return new MarketMoversViewHolder(v, parent.getContext());

            case VIEW_TYPE_DERIVATIVES:
                v = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_market_derivatives, parent, false);
                return new MarketDerivativesViewHolder(v);

            case VIEW_TYPE_NEWS:
                v = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_top_news, parent, false);
                return new TopNewsViewHolder(v);

            default:
                throw new RuntimeException("there is no type that matches the type " + viewType + " + make sure your using types correctly");

        }


    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

    }

    /**
     * The length of the string array will tell recycler view the count of the card views.
     */
    @Override
    public int getItemCount() {
        return menuItems.length;
    }

    /**
     * @param position this will return the view of the card according to the position of the card.
     */
    @Override
    public int getItemViewType(int position) {
        switch (position) {
            case VIEW_TYPE_TOP_MENU:
                return VIEW_TYPE_TOP_MENU;
            case VIEW_TYPE_MARKET_MOVERS:
                return VIEW_TYPE_MARKET_MOVERS;
            case VIEW_TYPE_DERIVATIVES:
                return VIEW_TYPE_DERIVATIVES;
            case VIEW_TYPE_NEWS:
                return VIEW_TYPE_NEWS;
            default:
                return 0;

        }

    }

    /**
     * This view holder is holding the view of topmost card,
     * which contains tab and viewpager.
     */
    static class TopMenuViewHolder extends RecyclerView.ViewHolder {
        @InjectView(R.id.pager)
        ViewPager top_view_pager;
        @InjectView(R.id.tabs)
        DashboardSlidingTabLayout top_view_tabs;
        @InjectView(R.id.expand)
        Button btnExpand;
        @InjectView(R.id.card_view)
        CardView cardView;

        private DashboardTabAdapter adapter;
        private CharSequence Titles[];
        private int NumberOfTabs;
        int pos;
        static int setView = 0;
        int minHeight;

        public static final int TOP_VIEW = 0;
        public static final int INDICES = 1;
        public static final int COMMODITY = 2;
        public static final int CURRENCY = 3;

        public TopMenuViewHolder(View itemView, final Context context) {

            super(itemView);
            ButterKnife.inject(this, itemView);
            Titles = context.getResources().getStringArray(R.array.string_array_dashboard_tab_menu);
            Bundle bundle = new Bundle();
            bundle.putInt("setView", setView);
            NumberOfTabs = Titles.length;
            adapter = new DashboardTabAdapter(((FragmentActivity) context).getSupportFragmentManager(), Titles, NumberOfTabs, bundle);
            top_view_pager.setAdapter(adapter);
            top_view_tabs.setViewPager(top_view_pager);
            /**
             * Here visibility of Expand button is decided upon the tab selected.
             */
            top_view_pager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                @Override
                public void onPageSelected(int position) {
                    pos = position;
                    if (position > 0) {
                        btnExpand.setVisibility(View.VISIBLE);
                        initializeTab();
                    } else if (position == 0) {
                        btnExpand.setVisibility(View.GONE);
                        collapseView();
                    }
                }

            });

            /**
             * This will be used to give initial height for the top card.
             */
            cardView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {

                @Override
                public boolean onPreDraw() {
                    cardView.getViewTreeObserver().removeOnPreDrawListener(this);
                    minHeight = cardView.getHeight();

                    ViewGroup.LayoutParams layoutParams = cardView.getLayoutParams();
                    layoutParams.height = minHeight;
                    cardView.setLayoutParams(layoutParams);

                    return true;
                }
            });
            /**
             * Here you can get the height of the screen
             */
            WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            DisplayMetrics dimension = new DisplayMetrics();
            wm.getDefaultDisplay().getMetrics(dimension);
            final int height = dimension.heightPixels;

            /**
             * Here you can add logic for expanding a card.
             */
            btnExpand.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.i("Dashboard", "Current Fragment" + top_view_pager.getCurrentItem());
                    new ChildRecyclerView(context, true);
                    new ParentRecyclerView(context, false);
                    toggleCardViewnHeight(height);


                }
            });
        }

        /**
         * This method will toggle expand and collapse of card view.
         */

        private void toggleCardViewnHeight(int height) {
            int ViewMinHeight = minHeight;
            if (cardView.getHeight() == ViewMinHeight) {
                // expand

                expandView(height);

            } else {
                // collapse
                collapseView();

            }
        }

        /**
         * This method will initialize tab adapter depending upon the page selected.
         */

        public void initializeTab() {
            switch (top_view_pager.getCurrentItem()) {
                case INDICES:
                    ((IndicesFragment) adapter.getRegisteredFragment(INDICES)).initUI();
                    break;
                case COMMODITY:
                    ((CommodityDashboardFragment) adapter.getRegisteredFragment(COMMODITY)).initUI();
                    break;
                case CURRENCY:
                    ((CurrencyDashboardFragment) adapter.getRegisteredFragment(CURRENCY)).initUI();
                    break;
            }
        }

        /**
         * Collapse View Animation
         */
        public void collapseView() {
            BaseFragment.isExpanded = 0;
            ValueAnimator anim = ValueAnimator.ofInt(cardView.getMeasuredHeightAndState(),
                    minHeight);
            anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator valueAnimator) {
                    int val = (Integer) valueAnimator.getAnimatedValue();
                    ViewGroup.LayoutParams layoutParams = cardView.getLayoutParams();
                    layoutParams.height = val;
                    cardView.setLayoutParams(layoutParams);
                }
            });
            anim.addListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animation) {

                }

                @Override
                public void onAnimationEnd(Animator animation) {
                    initializeTab();
                }

                @Override
                public void onAnimationCancel(Animator animation) {

                }

                @Override
                public void onAnimationRepeat(Animator animation) {

                }
            });

            anim.start();
        }

        /**
         * Expand View Animation
         */
        public void expandView(int height) {
            BaseFragment.isExpanded = 1;

            ValueAnimator anim = ValueAnimator.ofInt(cardView.getMeasuredHeightAndState(),
                    height);
            anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator valueAnimator) {
                    int val = (Integer) valueAnimator.getAnimatedValue();
                    ViewGroup.LayoutParams layoutParams = cardView.getLayoutParams();
                    layoutParams.height = val;
                    cardView.setLayoutParams(layoutParams);
                }
            });
            anim.addListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animation) {

                }

                @Override
                public void onAnimationEnd(Animator animation) {
                    initializeTab();
                }

                @Override
                public void onAnimationCancel(Animator animation) {

                }

                @Override
                public void onAnimationRepeat(Animator animation) {

                }
            });
            anim.start();

        }


    }

    /**
     * This view holder is holding the view of MarketMovers card,
     */
    static class MarketMoversViewHolder extends RecyclerView.ViewHolder {
        @InjectView(R.id.btnExpand)
        Button btnExpand;
        @InjectView(R.id.card_view)
        CardView cardView;
        @InjectView(R.id.relativeLayout)
        RelativeLayout relativeLayout;
        int height;
        String []menuItem={"MARKET MOVERS","DERIVATIVES","TOP NEWS"};
        public MarketMoversViewHolder(View itemView, final Context context) {
            super(itemView);
            ButterKnife.inject(this, itemView);

            relativeLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

                @Override
                public void onGlobalLayout() {
                    relativeLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                    height = relativeLayout.getHeight();
                    ViewGroup.LayoutParams layoutParams = cardView.getLayoutParams();
                    layoutParams.height = 400;
                    cardView.setLayoutParams(layoutParams);
                }

            });
            WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            DisplayMetrics dimension = new DisplayMetrics();
            wm.getDefaultDisplay().getMetrics(dimension);
            int width = dimension.widthPixels;
            final int screenHeight = dimension.heightPixels;
            final int fullHeight = screenHeight - 400;
            btnExpand.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    toggleCardViewnHeight(fullHeight);
                    DashboardMarketAdapter DM =new DashboardMarketAdapter(context,menuItem);
                    DM.notifyDataSetChanged();

                }
            });


        }

        private void toggleCardViewnHeight(int height) {
            int ViewMinHeight = 400;
            if (cardView.getHeight() == ViewMinHeight) {
                // expand
                ValueAnimator anim = ValueAnimator.ofInt(cardView.getMeasuredHeightAndState(),
                        height);
                anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator valueAnimator) {
                        int val = (Integer) valueAnimator.getAnimatedValue();
                        ViewGroup.LayoutParams layoutParams = cardView.getLayoutParams();
                        layoutParams.height = val;
                        cardView.setLayoutParams(layoutParams);
                    }
                });
                anim.start();
            } else {
                // collapse
                ValueAnimator anim = ValueAnimator.ofInt(cardView.getMeasuredHeightAndState(),
                        ViewMinHeight);
                anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator valueAnimator) {
                        int val = (Integer) valueAnimator.getAnimatedValue();
                        ViewGroup.LayoutParams layoutParams = cardView.getLayoutParams();
                        layoutParams.height = val;
                        cardView.setLayoutParams(layoutParams);
                    }
                });
                anim.start();
            }
        }
    }

    /**
     * This view holder is holding the view of Derivatives card,
     */
    private class MarketDerivativesViewHolder extends RecyclerView.ViewHolder {
        public MarketDerivativesViewHolder(View v) {
            super(v);
        }
    }

    /**
     * This view holder is holding the view of NEWS card,
     */
    private class TopNewsViewHolder extends RecyclerView.ViewHolder {
        public TopNewsViewHolder(View v) {
            super(v);
        }
    }
}
4

0 回答 0