我的应用程序在两个重叠布局上的平滑度方面存在问题。我建立了某种幻灯片菜单(如 Facebook 或 YouTube)。此菜单包含两种布局:显示数据的主布局并包括其下方的菜单布局。然后是菜单布局本身,一个带有自定义行布局的普通 ListView。
主要的:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
<include
        android:id="@+id/menu_list"
        layout="@layout/menu_list"/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/news_content"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:background="#FFF"
              android:orientation="vertical">
    <ListView
            android:id="@android:id/list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
 </LinearLayout>
 <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    <ProgressBar
            android:id="@+id/NewsProgressBar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:visibility="gone"
            style="@android:style/Widget.Holo.Light.ProgressBar.Large"/>
 </LinearLayout>
</RelativeLayout>
菜单:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="250dip"
          android:layout_height="fill_parent"
          android:orientation="vertical"
          android:background="@drawable/ic_sidebar_bg">
<RelativeLayout
        android:layout_height="wrap_content"
        android:layout_width="wrap_content">
    <ImageView
            android:alpha="0.8"
            android:layout_height="40dp"
            android:layout_width="match_parent"
            android:background="#222222"/>
    <TextView
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_marginLeft="10dip"
            android:layout_marginTop="12dip"
            android:textSize="14dp"
            android:textColor="#666"
            android:text="@string/menu_cat_main"/>
    <ImageView
            android:layout_height="1dp"
            android:layout_marginTop="40dp"
            android:layout_width="match_parent"
            android:background="#666666"/>
</RelativeLayout>
<ListView
        android:id="@+id/mainMenuList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>
我已经认识到这种重叠会导致 ListView 严重滞后。我所做的就是让菜单包含“visibility:gone”,这会导致 ListViews 工作得非常好。但是,如果我尝试将内容侧向滑动,则包含(菜单)会再次可见,并且幻灯片不会平滑。但除了这个外观问题之外,菜单的工作方式与 YouTube 上的完全一样。
为了清楚地了解以下代码: viewContent (View) 包含带有其内容的 LinearLayout viewMenu (View) 包含菜单布局 include canSlide (bool) 只要菜单可以通过触摸滑动(屏幕的某些区域为真) touch) isMenuScrolled (bool) 每当菜单滑出或不滑出时
这里是来自触摸事件的代码:
public boolean dispatchTouchEvent(MotionEvent touchevent) {
    switch (touchevent.getAction()) {
        case MotionEvent.ACTION_MOVE:
            if (canSlide) {
                if (touchevent.getX() < viewMenu.getWidth()) {
                    viewContent.setX(touchevent.getX());
                } else if (touchevent.getX() < 0) {
                    viewContent.setX(0);
                } else if (touchevent.getX() > viewMenu.getWidth()) {
                    viewContent.setX(viewMenu.getWidth());
                }
                viewContent.setEnabled(false);
            }
            return true;
        case MotionEvent.ACTION_DOWN:
            if (touchevent.getY() > 200 && touchevent.getX() < 50 && !isMenuScrolled) {
                viewMenu.setVisibility(View.VISIBLE);
                touchStart = touchevent.getX();
                canSlide = true;
            } else if (touchevent.getY() > 200 && touchevent.getX() > viewMenu.getWidth() && isMenuScrolled) {
                viewMenu.setVisibility(View.VISIBLE);
                touchStart = touchevent.getX();
                canSlide = true;
            } else {
                canSlide = false;
            }
            return true;
        case MotionEvent.ACTION_UP:
            if (viewContent.getX() != 0 || viewContent.getX() != viewMenu.getWidth()) {
                if (viewContent.getX() < touchStart) {
                    ObjectAnimator transAnimation = ObjectAnimator.ofFloat(viewContent, "x", viewContent.getX(), 0);
                    transAnimation.addListener(new Animator.AnimatorListener() {
                        @Override
                        public void onAnimationStart(Animator animator) {
                            viewContent.setEnabled(false);
                            isMenuScrolled = false;
                        }
                        @Override
                        public void onAnimationEnd(Animator animation) {
                            viewMenu.setVisibility(View.GONE);
                            viewContent.setEnabled(true);
                        }
                        @Override
                        public void onAnimationCancel(Animator animator) {
                            // nothing
                        }
                        @Override
                        public void onAnimationRepeat(Animator animator) {
                            // nothing
                        }
                    });
                    transAnimation.setDuration(300);
                    transAnimation.start();
                } else if (viewContent.getX() > touchStart) {
                    ObjectAnimator transAnimation = ObjectAnimator.ofFloat(viewContent, "x", viewContent.getX(), viewMenu.getWidth());
                    transAnimation.addListener(new Animator.AnimatorListener() {
                        @Override
                        public void onAnimationStart(Animator animator) {
                            viewContent.setEnabled(false);
                            isMenuScrolled = true;
                        }
                        @Override
                        public void onAnimationEnd(Animator animation) {
                            viewContent.setEnabled(true);
                        }
                        @Override
                        public void onAnimationCancel(Animator animator) {
                            // nothing
                        }
                        @Override
                        public void onAnimationRepeat(Animator animator) {
                            // nothing
                        }
                    });
                    transAnimation.setDuration(300);
                    transAnimation.start();
                }
            }
            return true;
    }
    return true;
}
如何解决此问题,使其滚动更流畅?最后一件事,在滚动菜单时禁用内容的最佳方法是什么?viewContent.setEnabled 无法正常工作。
非常感谢提前!