繁体   English   中英

列表视图中的Android进度对话框

[英]Android Progress dialog within listview

我的应用程序设计如下:

主要活动使用操作栏

第一个选项卡是一个分为3个部分的片段

| 包含列表视图的线性布局| |包含列表视图的线性布局| | 包含媒体控件和图像视图的线性布局|

我在此活动中有两个AsyncTasks,一个填充中心列表视图,另一个以媒体控件开始填充图像视图(专辑封面)。

这两个都运作良好。 List视图AsyncTask抛出一个进度对话框。 这是在屏幕的中心。 我知道我可以把这个微调器放在应用程序的右上角。 但是我可以将它放在列表视图线性布局的右上角,还是放在线性布局背面的中心? 这样,进度条适用的是不显眼但又明显的东西?

提前致谢

我知道我可以把这个微调器放在应用程序的右上角。 但是我可以将它放在列表视图线性布局的右上角,还是放在线性布局背面的中心?

如果您可以计算视图的位置,您可能可以将ProgressDialog放在您想要的位置(例如,请参阅此问题我已回答在代码中更改进度条的位置 )。 另外,请记住,这对于将看到对话框的屏幕并且对话框放置在某个奇怪位置的用户来说可能非常直观(他可能无法在ProgressDialog的位置和视图之间建立关联)数据已加载)。

另一个选项可能是修改ListView部件的当前布局,在顶部添加一个初始消失的FrameLayout (将覆盖整个ListView ),背景模拟带有Dialog的屏幕的背景(此FrameLayout将包含一个ProgressBar放置你想在哪里)。 AsyncTask启动时,此FrameLayout将变为可见(阻止下划线ListView的触摸事件可能是明智的)。 这样,用户仍然可以在您的应用中执行操作,并且可以清楚地显示正在加载其数据的View 当然,如果用户可以独立于加载ListView其他数据,这将很有效。

Luksprog的答案非常好,以为我应该在这里列出代码:绝对完美的main_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ListView
    android:id="@+id/first_list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/anchor" >
</ListView>

<View
    android:id="@id/anchor"
    android:layout_width="match_parent"
    android:layout_height="5dp"
    android:layout_centerVertical="true"
    android:background="#99cc00" />


<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/anchor" >

    <ListView
        android:id="@+id/second_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <FrameLayout
        android:id="@+id/overlay"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#33c1c1c1"
        android:visibility="gone" >

        <ProgressBar
            style="?android:attr/progressBarStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right|top"
            android:indeterminate="true" />
    </FrameLayout>
</RelativeLayout>

MainActivity.java

public class MainActivity extends Activity {

private String[] mItems = { "Item no.1", "Item no.2", "Item no.3",
        "Item no.4", "Item no.5", "Item no.6", "Item no.7", "Item no.8",
        "Item no.9", "Item no.10", "Item no.11", };

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_layout);
    // setup the two ListViews
    ListView firstList = (ListView) findViewById(R.id.first_list);
    firstList.setAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, mItems));
    firstList.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // just as an example, one you click an item in the first
            // ListView
            // a custom AsyncTask will kick in to load data in to the second
            // ListView
            new MyTask(MainActivity.this).execute((Void) null);
        }
    });
    ListView secondList = (ListView) findViewById(R.id.second_list);
    secondList.setAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, mItems));
    secondList.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // just to test that you can't click the ListView if the data is
            // loading
            Toast.makeText(getApplicationContext(), "click",
                    Toast.LENGTH_SHORT).show();
        }
    });
}

private class MyTask extends AsyncTask<Void, Void, Void> {

    private MainActivity mActivity;
    private FrameLayout mFrameOverlay;

    public MyTask(MainActivity activity) {
        mActivity = activity;
    }

    @Override
    protected void onPreExecute() {
        // the AsyncTask it's about to start so show the overlay
        mFrameOverlay = (FrameLayout) mActivity.findViewById(R.id.overlay);
        // set a touch listener and consume the event so the ListView
        // doesn't get clicked
        mFrameOverlay.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                return true;
            }
        });
        mFrameOverlay.setVisibility(View.VISIBLE);
    }

    @Override
    protected Void doInBackground(Void... params) {
        // do heavy work
        try {
            Thread.sleep(6000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        //remove the overlay
        mFrameOverlay.setVisibility(View.GONE);
        // setup the ListView with the new obtained data
        String[] obtainedData = { "D1", "D2", "D3" };
        ListView theList = (ListView) mActivity
                .findViewById(R.id.second_list);
        theList.setAdapter(new ArrayAdapter<String>(mActivity,
                android.R.layout.simple_list_item_1, obtainedData));
    }

}
}

Luksprog,希望你不介意我发布你的代码,只是不希望它从git中消失,这个答案会丢失给别人。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM