简体   繁体   English

是否可以向Android listactivity添加另一个视图?

[英]Is it possible to add another view to an Android listactivity?

i want to add a waiting circle to my listactivity. 我想在我的列表活动中添加一个等待圈。 I used the answer in this post: Using the "animated circle" in an ImageView while loading stuff 我在这篇文章中使用了答案: 加载东西时在ImageView中使用“动画圆”

However i think i have to add setContentView of the waiting circle layout in order to be able to use findViewById. 但是我认为我必须添加等待圈布局的setContentView才能使用findViewById。 The problem is i don't know if i can use setContentView twice in the same activity. 问题是我不知道我是否可以在同一活动中两次使用setContentView。

Right now when i try it, i get an error that i most use setContentView with the id of the listview layout i want to use. 现在,当我尝试它时,出现一个错误,我最常使用setContentView和要使用的listview布局的ID。

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listview);}


    public void showCallList() // show call list on screen
{
    getListView().setVisibility(View.GONE);
    findViewById(R.loadingPanel).setVisibility(View.VISIBLE);
    CallListArrayAdapter adapter = new CallListArrayAdapter(this,
            arrayListCalls);
    setListAdapter(adapter);
    findViewById(R.id.loadingPanel).setVisibility(View.GONE);
    getListView().setVisibility(View.VISIBLE);
}

the exception i get is : 我得到的例外是:

E/AndroidRuntime(4320): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list' E / AndroidRuntime(4320):由以下原因引起:java.lang.RuntimeException:您的内容必须具有ID属性为“ android.R.id.list”的ListView

you can't call setContentView again. 您不能再次调用setContentView。 instead, you should add your additional layout into your main XML that also contains your ListView. 相反,您应该将其他布局添加到还包含ListView的主XML中。 you can then find it there. 您可以在那里找到它。

Actually you don't need a ListActivity to have a ListView. 实际上,您不需要ListActivity即可拥有ListView。 You can just as well use a normal Activity with a normal layout.xml and just put a <ListView> in there and all the other views you want. 您也可以将普通的Activity与普通的layout.xml一起使用,并在其中放置<ListView>以及所需的所有其他视图。

Instead of getListView() , which is defined by ListActivity use the normal findViewById() with the id you gave your ListView in your layout.xml. 代替由getListView()定义的getListView() ,使用普通的findViewById()以及您在layout.xml中提供ListView的ID。

If you're trying to render a custom view of your own(The animated circle), and add it to an activity you have to use addContentView(View view, ViewGroup.LayoutParams params), wich adds an additional content view to the activity. 如果您要呈现自己的自定义视图(动画圆),然后将其添加到活动中,则必须使用addContentView(View view,ViewGroup.LayoutParams params),从而向该活动添加一个附加的内容视图。 Added after any existing ones in the activity -- existing views are NOT removed. 在活动中任何现有视图之后添加-不删除现有视图。 These are the parameters: view The desired content to display. 这些是参数:view要显示的所需内容。 params Layout parameters for the view. params视图的布局参数。

But the right way to do this(depending of course in the amount of data you're going to load) is using AsyncTask wich enables proper and easy use of the UI thread. 但是执行此操作的正确方法(当然取决于要加载的数据量)是使用AsyncTask wich,它可以正确轻松地使用UI线程。 This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers. 此类允许执行后台操作并在UI线程上发布结果,而无需操纵线程和/或处理程序。 An asynchronous task is defined by a computation that runs on a background thread and whose result is published on the UI thread. 异步任务由在后台线程上运行的计算定义,并且其结果发布在UI线程上。 An asynchronous task is defined by 3 generic types, called Params, Progress and Result, and 4 steps, called onPreExecute, doInBackground, onProgressUpdate and onPostExecute. 异步任务由3个通用类型(称为Params,Progress和Result)以及4个步骤(称为onPreExecute,doInBackground,onProgressUpdate和onPostExecute)定义。 For example,you can call a progress dialog(Function as your waiting circle) on the onPreExecute method and list your files in doInBackground method. 例如,您可以在onPreExecute方法上调用一个进度对话框(用作等待圈),并在doInBackground方法中列出文件。 Good Luck!!!! 祝好运!!!!

Yes, it can be done easily. 是的,这很容易做到。

Create a layout xml like you would for a normal activity, and inside this activity create an empty listview with android:id=@android:id/list . 创建一个布局xml,就像执行普通活动一样,并在此活动中使用android:id=@android:id/list创建一个空的listview。

When you start the ListActivity, setContentView to your custom layout. 启动ListActivity时,将ContentView设置为自定义布局。 The ListActivity will automatically find the listView with the id android:id/list , and will use that listview for all of the list-related functionality. ListActivity将自动找到ID为android:id/list的listView,并将该listview用于所有与列表相关的功能。

Relevant text from ListActivity: 来自ListActivity的相关文本:

ListActivity has a default layout that consists of a single, full-screen list in the center of the screen. ListActivity具有默认布局,该布局由位于屏幕中央的单个全屏列表组成。 However, if you desire, you can customize the screen layout by setting your own view layout with setContentView() in onCreate(). 但是,如果需要,可以通过使用onCreate()中的setContentView()设置自己的视图布局来自定义屏幕布局。 To do this, your own view MUST contain a ListView object with the id "@android:id/list" (or list if it's in code) 为此,您自己的视图必须包含ID为“ @android:id / list”(或列表,如果在代码中,则为List)的ListView对象

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

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