如何解决Android列表视图问题
| 我在“列表”视图中遇到问题-我正在尝试添加页眉视图和页脚视图,但它们似乎都出现在列表的顶部和底部。另外,当我滚动时,该应用程序冻结了几秒钟。 这是我的代码: LayoutInflater inflater = getLayoutInflater();
View header = inflater.inflate(R.layout.header_row,(ViewGroup) findViewById(R.id.header_layout_root));
header.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent();
i.setClassName(\"com.visualdenim.schooltraq\",\"com.visualdenim.schooltraq.Add_Class\");
startActivity(i);
}});
getListView().addHeaderView(header,null,true);
LayoutInflater footerinflater = getLayoutInflater();
View footer = footerinflater.inflate(R.layout.footer_row,(ViewGroup) findViewById(R.id.header_layout_root));
getListView().addFooterView(footer,false);
classes = new ArrayList<Course>();
this.cla = new CLA(this,R.layout.row,classes);
setListAdapter(this.cla);
cla.notifyDataSetChanged();
最佳答案将被打勾!
解决方法
查看LayoutInflater.inflate()的文档:
返回的根视图
膨胀的等级制度。如果root是
提供,这是根视图;
否则,这是
膨胀的XML文件。
然后查看代码中的以下几行:
View header = inflater.inflate(R.layout.header_row,(ViewGroup) findViewById(R.id.header_layout_root));
...
View footer = footerinflater.inflate(R.layout.footer_row,(ViewGroup) findViewById(R.id.header_layout_root));
这两种情况下返回的视图都是相同的; header_layout_root
视图同时包含页眉和页脚,因此您将同时在顶部和底部看到。用空根扩大视图并添加它们将有望解决您的问题。
页眉和页脚也不需要使用\'separate \'布局填充器。
, 这是我使用列表视图所做的类似操作,在底部有一个按钮,在顶部有一个微调器,您可能可以对其进行修改以适合您的应用程序。 XML布局:
<?xml version=\"1.0\" encoding=\"utf-8\"?>
<RelativeLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"
android:layout_width=\"wrap_content\" android:layout_height=\"wrap_content\"
android:orientation=\"vertical\">
<RelativeLayout android:layout_width=\"fill_parent\"
android:layout_height=\"wrap_content\" android:id=\"@+id/top_control_bar\">
<Spinner android:id=\"@+id/sort_by\" android:layout_width=\"fill_parent\"
android:layout_height=\"wrap_content\" android:entries=\"@array/default_sorts\" />
</RelativeLayout>
<LinearLayout android:id=\"@+id/bottom_control_bar\"
android:layout_width=\"fill_parent\" android:layout_height=\"wrap_content\"
android:layout_alignParentBottom=\"true\">
<Button android:layout_width=\"fill_parent\"
android:layout_height=\"wrap_content\" android:text=\"Add Item\" />
</LinearLayout>
<ListView android:id=\"@android:id/list\" android:layout_width=\"fill_parent\"
android:layout_height=\"0dip\" android:choiceMode=\"multipleChoice\"
android:layout_below=\"@id/top_control_bar\" android:layout_above=\"@id/bottom_control_bar\"></ListView>
<TextView android:id=\"@android:id/empty\" android:layout_width=\"wrap_content\"
android:layout_height=\"wrap_content\" android:text=\"@string/main_empty_list\"
android:layout_below=\"@id/top_control_bar\"android:layout_above=\"@id/bottom_control_bar\" />
</RelativeLayout>
Java代码:
// myList.java
package com.test.listview;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class myList extends ListActivity
{
/** Called when the activity is first created. */
public void onCreate(Bundle bundle)
{
super.onCreate(bundle);
// Create an array of Strings,that will be put to our ListActivity
String[] names = new String[] { \"Linux\",\"Windows7\",\"Eclipse\",\"Suse\",\"Ubuntu\",\"Solaris\",\"Android\",\"iPhone\",\"Linux\",\"iPhone\" };
setContentView (R.layout.main);
ListView listView = getListView();
ArrayAdapter a = new ArrayAdapter <String>(this,android.R.layout.simple_list_item_single_choice,names);
setListAdapter(a);
}
}
Strings.xml:
// strings.xml
<?xml version=\"1.0\" encoding=\"utf-8\"?>
<resources>
<string name=\"hello\">Hello World,myList!</string>
<string name=\"app_name\">listview</string>
<string-array name=\"default_sorts\">
<item>fooboo</item>
<item>asdfgh</item>
<item>qwerty</item>
<item>346346</item>
<item>hjkgaf</item>
<item>asdfas</item>
<item>vbncvn</item>
<item>dfgrdf</item>
<item>hjkkmb</item>
<item>fdghgv</item>
</string-array>
<string name=\"main_empty_list\">foo</string>
</resources>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。