DialogFragment中的ScrollView非常慢

时间:2016-07-28 12:37:38

标签: java android xml android-layout layout

我创建了一个DialogFragment,现在我在其中遇到了ScrollView的问题。滚动速度非常慢,我找不到原因。 Android工作室建议添加此行:

android:baselineAligned="false"

但它什么也没做。

然后我删除了两个孩子中的一个(ScrollView - > Relative - > Linear)并且对性能没有影响,但是它将所有东西都捣碎成一个小空间(当一切都清楚时包裹内容设置为匹配父级)。所以RelativeLayout显然保持了一切。但是,删除它对性能没有影响

这是我的XML文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" 
    android:layout_width="match_parent"
    android:background="@drawable/main_bc"
    android:weightSum="1"
    android:layout_gravity="center"
    android:layout_height="match_parent">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:baselineAligned="false"
        android:layout_marginTop="50dp"
        android:layout_marginBottom="45dp"

        android:id="@+id/scrollView3" >
        <RelativeLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
            <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:baselineAligned="false"
                android:weightSum="1"
                android:layout_height="match_parent" >

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/back"

                    android:id="@+id/bShopBack" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="113dp"
                    android:background="#800011FF"
                    android:layout_marginTop="5dp"
                    android:text="@string/bettsoft"

                    android:id="@+id/bbTap" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="113dp"
                    android:background="#800011FF"
                    android:layout_marginTop="5dp"
                    android:text="@string/friends"
                    android:id="@+id/bb1" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="113dp"
                    android:background="#800011FF"
                    android:layout_marginTop="5dp"
                    android:text="@string/advert"
                    android:id="@+id/bb2" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="113dp"
                    android:background="#800011FF"
                    android:layout_marginTop="5dp"
                    android:text="@string/lottery"
                    android:id="@+id/bb3" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="113dp"
                    android:background="#800011FF"
                    android:layout_marginTop="5dp"
                    android:text="@string/elcar"
                    android:id="@+id/bb4" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="113dp"
                    android:background="#800011FF"
                    android:layout_marginTop="5dp"
                    android:text="@string/scomp"
                    android:id="@+id/bb5" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="113dp"
                    android:background="#800011FF"
                    android:layout_marginTop="5dp"
                    android:text="@string/mcomp"
                    android:id="@+id/bb6" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="113dp"
                    android:background="#800011FF"
                    android:layout_marginTop="5dp"
                    android:text="@string/lcomp"
                    android:id="@+id/bb7" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="113dp"
                    android:background="#800011FF"
                    android:layout_marginTop="5dp"
                    android:text="@string/trains"
                    android:id="@+id/bb8" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="113dp"
                    android:background="#800011FF"
                    android:layout_marginTop="5dp"
                    android:text="@string/electronics"
                    android:id="@+id/bb9" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="113dp"
                    android:background="#800011FF"
                    android:layout_marginTop="5dp"
                    android:text="@string/medres"
                    android:id="@+id/bb10" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="113dp"
                    android:background="#800011FF"
                    android:layout_marginTop="5dp"
                    android:text="@string/himp"
                    android:id="@+id/bb11" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="113dp"
                    android:background="#800011FF"
                    android:layout_marginTop="5dp"
                    android:text="@string/tvs"
                    android:id="@+id/bb12" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="113dp"
                    android:background="#800011FF"
                    android:layout_marginTop="5dp"
                    android:text="@string/shouse"
                    android:id="@+id/bb13" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="113dp"
                    android:background="#800011FF"
                    android:layout_marginTop="5dp"
                    android:text="@string/mobosdev"
                    android:id="@+id/bb14" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="113dp"
                    android:background="#800011FF"
                    android:layout_marginTop="5dp"
                    android:text="@string/envprot"
                    android:id="@+id/bb15" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="113dp"
                    android:background="#800011FF"
                    android:layout_marginTop="5dp"
                    android:text="@string/digcurr"
                    android:id="@+id/bb16" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="113dp"
                    android:background="#800011FF"
                    android:layout_marginTop="5dp"
                    android:text="@string/nano"
                    android:id="@+id/bb17" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="113dp"
                    android:background="#800011FF"
                    android:layout_marginTop="5dp"
                    android:text="@string/holopho"
                    android:id="@+id/bb18" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="113dp"
                    android:background="#800011FF"
                    android:layout_marginTop="5dp"
                    android:text="@string/timetrav"
                    android:id="@+id/bb19" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="113dp"
                    android:background="#800011FF"
                    android:layout_marginTop="5dp"
                    android:text="@string/comspatra"
                    android:id="@+id/bb20" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="113dp"
                    android:background="#800011FF"
                    android:layout_marginTop="5dp"
                    android:text="@string/spaele"
                    android:id="@+id/bb21" />

            </LinearLayout>
        </RelativeLayout>
    </ScrollView>

</LinearLayout>

此布局中有大量按钮。但实际上,这不是问题。我有第二种布局,其中几乎没有什么可以滚动,但速度仍然很慢,因为地狱......这不是我的设备,它是一个非常新的模型。在发射前测试中,每个设备也检测到它。

和相应的DialogFragment类;

public class ShopDialog extends DialogFragment implements View.OnClickListener {
    Context c;
    Clicker cl;
    /**
     * Activity class reference to call more dialogs from this
     * DialogFragment
     */
    Game game;

    public static ShopDialog newInstance() {
        ShopDialog f = new ShopDialog();

        // Supply num input as an argument.
        Bundle args = new Bundle();

        f.setArguments(args);

        return f;
    }

    public void params(Context c, Clicker cl, Game game){
        this.c = c;
        this.cl = cl;
        this.game = game;

    }




    @Override
    public void onCreate(Bundle sis){
        super.onCreate(sis);
        //      setContentView(null);

        int style, theme;

        style = DialogFragment.STYLE_NO_FRAME;
        theme = android.R.style.Theme_Holo_Dialog;


        setStyle(style, theme);

    }

    private View v;



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.shop, container, false);
        this.v = v;
        setButtons();
        return v;
    }





    private Button tap, back;
    private Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17,b18,b19,b20,b21;

    private void setButtons(){
        tap = (Button) v.findViewById(R.id.bbTap);
        back = (Button) v.findViewById(R.id.bShopBack);

        b1 = (Button) v.findViewById(R.id.bb1);
        b2 = (Button) v.findViewById(R.id.bb2);
        b3 = (Button) v.findViewById(R.id.bb3);
        b4 = (Button) v.findViewById(R.id.bb4);
        b5 = (Button) v.findViewById(R.id.bb5);
        b6 = (Button) v.findViewById(R.id.bb6);
        b7 = (Button) v.findViewById(R.id.bb7);
        b8 = (Button) v.findViewById(R.id.bb8);
        b9 = (Button) v.findViewById(R.id.bb9);
        b10 = (Button) v.findViewById(R.id.bb10);
        b11 = (Button) v.findViewById(R.id.bb11);
        b12 = (Button) v.findViewById(R.id.bb12);
        b13 = (Button) v.findViewById(R.id.bb13);
        b14 = (Button) v.findViewById(R.id.bb14);
        b15 = (Button) v.findViewById(R.id.bb15);
        b16 = (Button) v.findViewById(R.id.bb16);
        b17 = (Button) v.findViewById(R.id.bb17);
        b18 = (Button) v.findViewById(R.id.bb18);
        b19 = (Button) v.findViewById(R.id.bb19);
        b20 = (Button) v.findViewById(R.id.bb20);
        b21 = (Button) v.findViewById(R.id.bb21);

        tap.setOnClickListener(this);
        back.setOnClickListener(this);

        b1.setOnClickListener(this);
        b2.setOnClickListener(this);
        b3.setOnClickListener(this);
        b4.setOnClickListener(this);
        b5.setOnClickListener(this);
        b6.setOnClickListener(this);
        b7.setOnClickListener(this);
        b8.setOnClickListener(this);
        b9.setOnClickListener(this);
        b10.setOnClickListener(this);
        b11.setOnClickListener(this);
        b12.setOnClickListener(this);
        b13.setOnClickListener(this);
        b14.setOnClickListener(this);
        b15.setOnClickListener(this);
        b16.setOnClickListener(this);
        b17.setOnClickListener(this);
        b18.setOnClickListener(this);
        b19.setOnClickListener(this);
        b20.setOnClickListener(this);
        b21.setOnClickListener(this);

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bShopBack:
                dismiss();
                break;
            case R.id.bbTap:
                FragmentTransaction ft99 = getFragmentManager().beginTransaction();
                Fragment prev99 = getFragmentManager().findFragmentByTag("sdialog99");
                if (prev99 != null) {
                    ft99.remove(prev99);
                }
                ft99.addToBackStack(null);

                // Create and show the dialog.
                TapUpDialog newFragment99 = TapUpDialog.newInstance();
                newFragment99.params(c,cl, game, cl.tap);
                newFragment99.show(ft99, "sdialog99");
                break;
            case R.id.bb1:
                showItemDialog(cl.items[0], 0);
                break;
            case R.id.bb2:
                showItemDialog(cl.items[1], 1);
                break;
            case R.id.bb3:
                showItemDialog(cl.items[2], 2);
                break;
            case R.id.bb4:
                showItemDialog(cl.items[3], 3);
                break;
            case R.id.bb5:
                showItemDialog(cl.items[4], 4);
                break;
            case R.id.bb6:
                showItemDialog(cl.items[5], 5);
                break;
            case R.id.bb7:
                showItemDialog(cl.items[6], 6);
                break;
            case R.id.bb8:
                showItemDialog(cl.items[7], 7);
                break;
            case R.id.bb9:
                showItemDialog(cl.items[8], 8);
                break;
            case R.id.bb10:
                showItemDialog(cl.items[9], 9);
                break;
            case R.id.bb11:
                showItemDialog(cl.items[10], 10);
                break;
            case R.id.bb12:
                showItemDialog(cl.items[11], 11);
                break;
            case R.id.bb13:
                showItemDialog(cl.items[12], 12);
                break;
            case R.id.bb14:
                showItemDialog(cl.items[13], 13);
                break;
            case R.id.bb15:
                showItemDialog(cl.items[14], 14);
                break;
            case R.id.bb16:
                showItemDialog(cl.items[15], 15);
                break;
            case R.id.bb17:
                showItemDialog(cl.items[16], 16);
                break;
            case R.id.bb18:
                showItemDialog(cl.items[17], 17);
                break;
            case R.id.bb19:
                showItemDialog(cl.items[18], 18);
                break;
            case R.id.bb20:
                showItemDialog(cl.items[19], 19);
                break;
            case R.id.bb21:
                showItemDialog(cl.items[20], 20);
                break;



        }

    }

    private void showItemDialog(Item item, int id){
        FragmentTransaction ft2 = getFragmentManager().beginTransaction();
        Fragment prev2 = getFragmentManager().findFragmentByTag("sdialog"+id);
        if (prev2 != null) {
            ft2.remove(prev2);
        }
        ft2.addToBackStack(null);

        // Create and show the dialog.
        ItemUpDialog newFragment2 = ItemUpDialog.newInstance();
        newFragment2.params(c,cl, game, item);
        newFragment2.show(ft2, "sdialog"+id);


    }

}

我无法弄清楚这一点,而缓慢的表现有点破坏了体验。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

在尝试调整一些不相关的样式问题时,我最终暂时删除了背景作为样式实验并解决了它。现在表现再次非常好。我删除了这一行:

android:background="@drawable/main_bc"

它解决了它。我不确定它为什么解决它,但滚动现在非常正常。没有更多的滞后。我将背景设置为原始颜色并解决了它。