抽屉布局滞后于复杂的视图

时间:2014-10-09 09:33:54

标签: android android-fragments drawerlayout

我做了一个类似于卡片刷卡的视图(这种布局有点复杂)。我的抽屉布局完美地适用于除此之外的每个片段(刷卡视图)。当我在这个片段上,当我尝试打开抽屉布局时,它会滞后(并且在关闭时)。我浏览了SO上的各种链接,它只是在选择抽屉布局项目之后告诉我如何执行任务,但在我的情况下,它仅在打开时滞后。

这是我的java代码,我在运行时添加视图,使其变得复杂(我猜)

   private void addView(ArrayList<String> drawableList, final SearchUserData searchUserData) {
    windowWidth = mainBaseActivity.getWindowManager().getDefaultDisplay().getWidth();
    windowHeight = mainBaseActivity.getWindowManager().getDefaultDisplay().getHeight();

    screenCenterX = windowWidth / 2;
    screenCenterY = windowHeight/2;
    xDistance = ((windowWidth-(windowWidth-120))/2)/2;

    for (int i = 0; i < drawableList.size(); i++) {

        final View myRelView = inflater.inflate(R.layout.custom_layout, null);
        final ImageView m_image = (ImageView) myRelView.findViewById(R.id.sp_image);
        m_topLayout = (RelativeLayout) myRelView.findViewById(R.id.sp_color);
        txtViewUserNameAge = (TextView) myRelView.findViewById(R.id.txtViewUserNameAge);

        //          final FrameLayout myRelView = new FrameLayout(mainBaseActivity);
        myRelView
        .setLayoutParams(new LayoutParams((windowWidth - 120), windowWidth-120));
        myRelView.setX((windowWidth-(windowWidth-120))/2);
        myRelView.setY(windowWidth/3-90);
        myRelView.setRotation(0);
        myRelView.setTag(i);
        m_image.setTag(i);
        //          m_image.setImageDrawable(myImageList.get(i));
        ImageLoader.getInstance().displayImage(drawableList.get(i), m_image, options);
        txtViewUserNameAge.setText(""+searchUserData.getData().get(i).getName()+", "+searchUserData.getData().get(i).getAge());
        frameLayouts.add(m_image);
        viewlist.add(myRelView);
        topLayoutlist.add(m_topLayout);

        topMargin = myRelView.getTop();
        int imagePosition = drawableList.size() - 1;
        //          
        //              if (i == imagePosition) {
        //                  myRelView.setLayoutParams(new LayoutParams((windowWidth - 120), windowWidth-120));
        //                  myRelView.setX(xDistance + 40);
        //                  myRelView.setY(windowWidth/3-90);
        //
        //              } else if (i == imagePosition - 1) {
        //                  myRelView.setLayoutParams(new LayoutParams((windowWidth - 120), windowWidth-120));
        //                  myRelView.setX(xDistance + 20);
        //                  myRelView.setY(windowWidth/3-90);
        //
        //              }

        if (i == imagePosition) {
            myRelView.setLayoutParams(new LayoutParams((windowWidth - 120), windowWidth-120));
            myRelView.setX(xDistance + 40);
            myRelView.setY(windowWidth/3-90);

        } else if (i == imagePosition - 1) {
            myRelView.setLayoutParams(new LayoutParams((windowWidth - 120), windowWidth-120));
            myRelView.setX(xDistance + 20);
            myRelView.setY(windowWidth/3-90);

        } else {
            myRelView.setLayoutParams(new LayoutParams((windowWidth - 120), windowWidth-120));
            myRelView.setX(xDistance);
            myRelView.setY(windowWidth/3-90);
        }

 parentView.addView(myRelView);
}

这是我的xml文件

<RelativeLayout
    android:id="@+id/sp_color"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/blank_image"
        android:scaleType="fitXY" />

    <ImageView
        android:id="@+id/sp_image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="@dimen/common_padding"
        android:background="@drawable/baby1"
        android:scaleType="fitXY" />

    <LinearLayout
        android:id="@+id/bottomLayout"
        android:layout_width="match_parent"
        android:layout_height="@dimen/navigation_bar_height"
        android:layout_alignParentBottom="true"
        android:layout_margin="@dimen/common_padding"
        android:background="#99000000"
        android:gravity="center" >

        <TextView
            android:id="@+id/txtViewUserNameAge"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Jessica Alba, 26"
            android:textColor="@android:color/white"
            android:textSize="@dimen/normal_text_size" />
    </LinearLayout>
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

我修复了抽屉布局的这个口吃问题。我只是将我的addView(....)方法放在AsyncTask中,现在它完美地工作了。