我有一个布局inflater和一个自定义arrayAdapter,用于创建滚动列表视图。我想保留滚动列表视图,但是在顶部和底部添加一个导航栏(带有图像按钮或其他东西的表格),它们与滚动列表无关。我怎么能这样做?
这就是我想要做的事情:
+--------------------------------------+
| fixed nav bar |
+--------------------------------------+
| scroll listview item |
|--------------------------------------|
| scroll listview item |
|--------------------------------------|
| scroll listview item |
|--------------------------------------|
| scroll listview item |
+--------------------------------------+
| fixed nav bar |
+--------------------------------------+
这就是我实例化listAdapter的方法,但我希望addHeaderView()在列表滚动时保持固定:
String [] list_array = new String [mCursor.getCount ()];
View vh = getLayoutInflater ().inflate (R.layout.tabtwo_header, null);
ListView lv = getListView ();
lv.addHeaderView (vh);
setListAdapter (new dynAdap (this, android.R.layout.simple_list_item_1, list_array));
我的列表视图的xml布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical">
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:padding="10dip"
android:layout_width="fill_parent"
android:gravity="left|center"
android:textSize="22sp"
android:textStyle="bold"
android:drawableRight="@drawable/next_icon"
android:text="Name Name"
android:id="@+id/tv_ListItem"
android:background="@drawable/xml_tabtwo">
</TextView>
</LinearLayout>
ArrayAdapter:
public class dynAdap extends ArrayAdapter<String>
{
String [] list;
public dynAdap (Context context, int textViewResourceId, String [] objects)
{
super (context, textViewResourceId, objects);
list = objects;
}
@Override
public View getView (int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater = getLayoutInflater ();
View row = inflater.inflate (LAYOUT_TABTWO, parent, false);
TextView tv1 = (TextView) row.findViewById (R.id.tv_ListItem);
tv1.setText (list[position]);
return row;
}
}
答案 0 :(得分:2)
好的,终于有了这个工作。 Maximus和OcuS,感谢您的帮助。我是一个很大的帮助。还从这两个网站获得了大量信息和示例:
这是一个工作示例。希望它可以帮助那些试图做同样事情的人。
要执行的文件:
myList.java
主要活动。扩展ListActivity并包括一个用于填充列表的自定义适配器。
drawable/xml_listitem_shape.xml
它使用渐变“形状”而不是图像来控制列表项的“按下”,“选定”和“正常”状态。渐变允许更快的渲染,并不是特定于设备,因此它让你远离多图像混乱(hdpi,mdp,ldpi ...)
layout/main.xml
包含Header,Footer和Listview对象的布局。不使用任何选择器文件,但声明 android:id / list 作为listview对象的ID,这是必需的。如果你不这样做,Android会抱怨没有找到这个ID。
layout/menu_item.xml
仅包含供dynAdap类使用的TextView对象(无需布局)。此文件将 xml_listitem_shape 选择器文件声明为背景,该文件定义了listitem在各种状态下的显示方式。
values/colors.xml
整个应用程序中使用的颜色定义。你可以对你的颜色进行硬编码,但是这个文件让事情更加清晰。
<强> myList.java 强>
package com.test.listview;
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class myList extends ListActivity
{
public final String TAG = "** myList **";
String[] mNames = new String[] {
"Linux", "Plan9", "Eclipse", "Java","Ubuntu", "Next", "Android", "Xoom", "Pascal", "Assembly",
"C++", "Perl", "Bash", "Korn", "Int3", "CS:IP" };
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView (R.layout.main);
Button b1 = (Button) findViewById (R.id.button1);
Button b2 = (Button) findViewById (R.id.button2);
Button b3 = (Button) findViewById (R.id.button3);
Button b4 = (Button) findViewById (R.id.button4);
ListView listView = getListView();
setListAdapter (new dynAdap (this, android.R.layout.simple_list_item_1, mNames));
listView.setOnItemClickListener (oicl);
b1.setOnClickListener (ocl);
b2.setOnClickListener (ocl);
b3.setOnClickListener (ocl);
b4.setOnClickListener (ocl);
}
/*
* listener for buttons
*/
OnClickListener ocl = new OnClickListener()
{
@Override
public void onClick (View v)
{
String b = new String ("");
switch (v.getId ())
{
case R.id.button1:
b = "button1";
break;
case R.id.button2:
b = "button2";
break;
case R.id.button3:
b = "button3";
break;
case R.id.button4:
b = "button4";
break;
}
Toast.makeText (myList.this, b, Toast.LENGTH_SHORT).show();
}
};
/*
* listener for listview clicks - pop up toast to show what was selected
*/
OnItemClickListener oicl = new OnItemClickListener()
{
@Override
public void onItemClick (AdapterView<?> parent, View view, int index, long id)
{
Toast.makeText (myList.this, mNames[index], Toast.LENGTH_SHORT).show();
}
};
/*
* This is a custom list adapter to set the color and text content of each list item
*/
public class dynAdap extends ArrayAdapter<String>
{
String [] list;
public dynAdap (Context context, int textViewResourceId, String [] objects)
{
super (context, textViewResourceId, objects);
list = objects;
}
@Override
public View getView (int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater = getLayoutInflater ();
// return the view associated with the TextView in the menu_item.xml file
View row = inflater.inflate (R.layout.menu_item, parent, false);
TextView tv1 = (TextView) row.findViewById (R.id.tv_item);
tv1.setText (list[position]);
return row;
}
}
}
<强> xml_listitem_shape.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<!-- pressed state of item -->
<item android:state_pressed="true" >
<shape>
<gradient
android:startColor="@color/DkRed"
android:endColor="@color/Red"
android:angle="270" />
<stroke
android:width="3dp"
android:color="@color/LightGreen" />
<corners
android:radius="3dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<!-- focused state of item -->
<item android:state_selected="true" >
<shape>
<gradient
android:endColor="@color/Silver"
android:startColor="@color/Gray"
android:angle="270" />
<stroke
android:width="3dp"
android:color="@color/Red" />
<corners
android:radius="3dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<!-- normal state of item -->
<item>
<shape>
<gradient
android:endColor="@color/White"
android:startColor="@color/Silver"
android:angle="270" />
<stroke
android:width="3dp"
android:color="@color/LightBlue" />
<corners
android:radius="3dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
</selector>
<强> main.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">
<TableRow
android:id="@+id/tableRow1"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:background="@color/LightBlue"
android:gravity="center"
android:padding="5dip">
<Button
android:text="Button"
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
<Button
android:text="Button"
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
<Button
android:text="Button"
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
</TableRow>
</RelativeLayout>
<LinearLayout
android:id="@+id/bottom_control_bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@color/LightBlue"
android:padding="10dip">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Add Item"
android:id="@+id/button4" />
</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"
android:background="@color/Silver">
</ListView>
</RelativeLayout>
<强> menu_item.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:paddingTop="20dip"
android:paddingBottom="20dip"
android:layout_gravity="center"
android:gravity="center"
android:textColor="#000000"
android:background="@drawable/xml_listitem_shape"
android:text="Fooooooo"
android:textSize="22dip"
android:id="@+id/tv_item" />
<强> colors.xml 强>
<resources>
<color
name="transparent">#00000000</color>
<!-- colors used in application -->
<color
name="Black">#000000</color>
<color
name="DkRed">#660000</color>
<color
name="Red">#b70101</color>
<color
name="White">#f7f5e8</color>
<color
name="Silver">#c8c5bb</color>
<color
name="Gray">#6e6a5b</color>
<color
name="Yellow">#f6f900</color>
<color
name="Orange">#ff9000</color>
<color
name="LightGreen">#00ff00</color>
<color
name="Green">#085c00</color>
<color
name="Gold">#ccaf00</color>
<color
name="LightBlue">#0077ff</color>
<color
name="Blue">#000077</color>
<color
name="LightCyan">#00ffff</color>
<color
name="Cyan">#007777</color>
</resources>
答案 1 :(得分:1)
如果你使用一个LinearLayout,其顶部View的权重为0,ListView的权重为1,而底部View的权重为0,则它可以正常工作。
我总是忘记ListView的页眉和页脚视图,所以OcuS的帖子绝对是一种方法,特别是如果你正在扩展ListActivitiy。
无论如何,这是一个非常简单的布局示例。 TextViews可以替换为很多其他东西......例如,附加的水平LinearLayout。
请记住,这是您在主要活动中设置为内容视图的布局。 如果您的主要活动是扩展ListActivity,则不会使用此功能。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView android:id="@+id/toplabel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:gravity="center_horizontal"
android:text="Test"/>
<ListView android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView android:id="@+id/bottomtext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0" />
</LinearLayout>