从帖子

时间:2015-06-02 00:37:18

标签: android multithreading listview

我正在运行带有选项的listView,当用户点击listView中的元素时,他们将重定向到新活动,当前的活动将关闭。此代码运行但每次跳过大约100帧。我想知道是否有更有效的方法来做到这一点,如果我正确地这样做。谢谢!

@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
    drawerLayout.closeDrawers();
    switch(position){
        case 0:
            // Sets listView element selected.
            selectListViewItemLeft(position);
            new Thread() {
                public void run() {
                    try {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Intent intent = new Intent(Home_Page.this, Home_Page.class);
                                startActivity(intent);
                                finish();
                            }
                        });
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }.start();
            break;
        case 1:

            break;
    }
}

1 个答案:

答案 0 :(得分:0)

除非我误解了这个问题,否则你不需要在一个线程中开始新的活动,你可以开始一个新的活动:

public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
    drawerLayout.closeDrawers();
    switch(position){
        case 0:
            Intent intent = new Intent(Home_Page.this, Home_Page.class);
            startActivity(intent);
            finish();

            break;
        case 1:

            break;
    }
}