ImageView在Sliding up面板中消失

时间:2015-02-03 16:54:31

标签: java android android-activity android-fragments android-viewpager

我有问题。我有ViewPager 3片段(页面)。在第一个片段我有向上滑动面板。我用它来自GitHub这个库。点击here。在该滑动面板的TitleBar中,我有ImageView,如下图所示。当用户打开或关闭Sliding面板时,ImageView会发生变化。

enter image description here

我的问题:当我更改片段(移动到另一页)并返回第一页时ImageVIew消失。是什么原因?!

Fragment1.java :( setPanelSlideListener在onViewCreated方法中,在创建视图时立即调用...)

mSlidingUpPanelLayout.setPanelSlideListener(new SlidingUpPanelLayout.PanelSlideListener() {
            @Override
            public void onPanelSlide(View panel, float slideOffset) {
                Log.i(TAG, "onPanelSlide, offset " + slideOffset);
            }

            @Override
            public void onPanelCollapsed(View panel) {
                Log.i(TAG, "onPanelCollapsed");
                mImageView.setBackgroundResource(R.drawable.icon_up);
            }

            @Override
            public void onPanelExpanded(View panel) {
                Log.i(TAG, "onPanelExpanded");
                mImageView.setBackgroundResource(R.drawable.icon_down);
                fab.setColorPressed(getResources().getColor(R.color.fab_up_pressed));
            }

            @Override
            public void onPanelAnchored(View panel) {
                Log.i(TAG, "onPanelAnchored");
            }

            @Override
            public void onPanelHidden(View panel) {
                Log.i(TAG, "onPanelHidden");
            }

            @Override
            public void onPanelHiddenExecuted(View panel, Interpolator interpolator, int duration) {
                Log.i(TAG, "onPanelHiddenExecuted");
            }

            @Override
            public void onPanelShownExecuted(View panel, Interpolator interpolator, int duration) {
                Log.i(TAG, "onPanelShownExecuted");
                mImageView.setBackgroundResource(R.drawable.icon_up);
            }

            @Override
            public void onPanelExpandedStateY(View panel, boolean reached) {
                Log.i(TAG, "onPanelExpandedStateY" + (reached ? "reached" : "left"));
            }

            @Override
            public void onPanelCollapsedStateY(View panel, boolean reached) {
                Log.i(TAG, "onPanelCollapsedStateY" + (reached ? "reached" : "left"));
                if (reached) {
          fab.setColorPressed(getResources().getColor(R.color.fab_pressed));
                    mTextView.setTextColor(Color.parseColor("#FFFFFF"));
                }else{
                                        fab.setColorPressed(getResources().getColor(R.color.fab_up_pressed));
                    mTextView.setTextColor(Color.parseColor("#FFFFFF"));
                }
            }

            @Override
            public void onPanelLayout(View panel, SlidingUpPanelLayout.PanelState state) {
                if(state == SlidingUpPanelLayout.PanelState.COLLAPSED){
                    titleBar.setBackgroundColor(Color.parseColor("#03A9F4"));
                    fab.setColorNormal(getResources().getColor(R.color.fab_primary));
                    fab.setColorPressed(getResources().getColor(R.color.fab_pressed));
                    mTextView.setTextColor(Color.parseColor("#FFFFFF"));
                    mImageView.setBackgroundResource(R.drawable.icon_up);
                }
                else if (state == SlidingUpPanelLayout.PanelState.EXPANDED || state == SlidingUpPanelLayout.PanelState.ANCHORED){
                    titleBar.setBackgroundColor(Color.parseColor("#ffff9431"));
                    fab.setColorNormal(getResources().getColor(R.color.fab_up_primary));
                    fab.setColorPressed(getResources().getColor(R.color.fab_up_pressed));
                    mTextView.setTextColor(Color.parseColor("#FFFFFF"));
                    mImageView.setBackgroundResource(R.drawable.icon_down);
                }
            }
        });

感谢您的帮助!需要任何帮助!

2 个答案:

答案 0 :(得分:1)

我在很多应用程序中都使用过umano库。你试着保存它的状态吗?

请添加以下代码,以便在您从另一个片段返回时保存您的状态。

@Override
protected void onSaveInstanceState(Bundle outState) {
     super.onSaveInstanceState(outState);
     outState.putBoolean(SAVED_STATE_ACTION_BAR_HIDDEN,
     mLayout.isExpanded());
}

<强>更新

public static final String SAVED_STATE_ACTION_BAR_HIDDEN = "saved_state_action_bar_hidden";

而不是isExpanded(),根据您使用的库,它可能是isPanelExpanded()isPaneExpanded()

请更新相关方法并告知我们。

如果没有解决您的问题,请告诉我。

我想帮助你。 享受编码...... :)

答案 1 :(得分:0)

使用addPanelSlideListener代替setPanelSlideListener