Android:向下滚动/向上列表视图时,使用动画隐藏RelativeLayout

时间:2014-07-03 17:43:18

标签: android android-layout android-listview android-animation

我尝试实现一个动画来隐藏一个在listview上方的relativeLayout。我想有这种行为:

  • on Scroll DOWN:Hide RelativeLayout
  • on Scroll UP:仅当我在顶部时显示RelativeLayout

enter image description here

我创建的以下代码有效,但有两个问题:

  1. 当我向下滚动或向上滚动时,如果我在列表视图的顶部,则会有一个相对布局的闪烁。我必须执行快速滚动才能隐藏此攀爬。 =>我的代码中有问题,可能是listIsAtTop()函数?
  2. 第二个问题,我没有动画。我使用setVisivility(VISIBLE或GONE)来显示或隐藏relativeLayout。这不是用户友好的,不是吗? :)我不知道如何添加一些翻译动画来隐藏和显示这个relativeLayout。
  3. 在Account_Activity类的onCreate()中

    list = (com.app.frisbeee.account_view.CustomListView)findViewById(R.id.accountview_listview);
    adapter = new Account_view_custom_adapter(Account_view_act.this, annoncesList);
                list.setAdapter(adapter);
                adapter.notifyDataSetChanged();
                registerForContextMenu(list);
    
                list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        list.showContextMenuForChild(view);   
                    }
                });
    
                list.setOnDetectScrollListener(new OnDetectScrollListener() {
                    @Override
                    public void onUpScrolling() {
                        /* do something */
                        Log.d("INFO", "UPPPPPPPPPPP");
                        if (listIsAtTop()) {
                            RelativeLayout relative1 = (RelativeLayout) findViewById(R.id.relative1);
                            relative1.setVisibility(View.VISIBLE);
                        }
                    }
    
                    @Override
                    public void onDownScrolling() {
                        /* do something */
                        Log.d("INFO", "DOWNNNNNNNN");
                        if (!listIsAtTop()) {
                            RelativeLayout relative1 = (RelativeLayout) findViewById(R.id.relative1);
                            relative1.setVisibility(View.GONE);
                        }
    
                    }
                });
    
            }
    

    listIsAtTop函数代码:

    private boolean listIsAtTop()   {   
            if (list.getChildCount() == 0) {
                return true;
            }
            return list.getFirstVisiblePosition() == 0 && (list.getChildCount() == 0 || list.getChildAt(0).getTop() == 0);
        }
    
    用于检测向上和向下滚动的

    CustomListView

    import android.content.Context;
    import android.util.AttributeSet;
    import android.view.View;
    import android.widget.AbsListView;
    
    public class CustomListView extends android.widget.ListView {
    
        private OnScrollListener onScrollListener;
        private OnDetectScrollListener onDetectScrollListener;
    
        public CustomListView(Context context) {
            super(context);
            onCreate(context, null, null);
        }
    
        public CustomListView(Context context, AttributeSet attrs) {
            super(context, attrs);
            onCreate(context, attrs, null);
        }
    
        public CustomListView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            onCreate(context, attrs, defStyle);
        }
    
        private void onCreate(Context context, AttributeSet attrs, Integer defStyle) {
            setListeners();
        }
    
        private void setListeners() {
            super.setOnScrollListener(new OnScrollListener() {
    
                private int oldTop;
                private int oldFirstVisibleItem;
    
                @Override
                public void onScrollStateChanged(AbsListView view, int scrollState) {
                    if (onScrollListener != null) {
                        onScrollListener.onScrollStateChanged(view, scrollState);
                    }
                }
    
                @Override
                public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                    if (onScrollListener != null) {
                        onScrollListener.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount);
                    }
    
                    if (onDetectScrollListener != null) {
                        onDetectedListScroll(view, firstVisibleItem);
                    }
                }
    
                private void onDetectedListScroll(AbsListView absListView, int firstVisibleItem) {
                    View view = absListView.getChildAt(0);
                    int top = (view == null) ? 0 : view.getTop();
    
                    if (firstVisibleItem == oldFirstVisibleItem) {
                        if (top > oldTop) {
                            onDetectScrollListener.onUpScrolling();
                        } else if (top < oldTop) {
                            onDetectScrollListener.onDownScrolling();
                        }
                    } else {
                        if (firstVisibleItem < oldFirstVisibleItem) {
                            onDetectScrollListener.onUpScrolling();
                        } else {
                            onDetectScrollListener.onDownScrolling();
                        }
                    }
    
                    oldTop = top;
                    oldFirstVisibleItem = firstVisibleItem;
                }
            });
        }
    
        @Override
        public void setOnScrollListener(OnScrollListener onScrollListener) {
            this.onScrollListener = onScrollListener;
        }
    
        public void setOnDetectScrollListener(OnDetectScrollListener onDetectScrollListener) {
            this.onDetectScrollListener = onDetectScrollListener;
        }
    }
    

1 个答案:

答案 0 :(得分:0)

尝试使用listIsAtTop()函数:

private boolean listIsAtTop()   {   
    if(listView.getChildCount() == 0) return true;
    return listView.getChildAt(0).getTop() == 0;
}

至于滑动动画:

slide_down.xml的代码:

<set xmlns:android="http://schemas.android.com/apk/res/android">  
    <translate android:fromXDelta="0" android:fromYDelta="-1000" android:duration="1500"/>
</set>

slide_up.xml的代码:

<set xmlns:android="http://schemas.android.com/apk/res/android">  
    <translate android:fromXDelta="0" android:fromYDelta="1000" android:duration="1500"/>
</set>

然后在onCreate方法中加载动画:

Animation slideUp = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_up);

然后将它附加到您的RelativeLayout:

relative1.startAnimation(slideUp);

希望这有助于:)

相关问题