2011-04-21 102 views
0

我有一个布局inflater和一个自定义arrayAdapter创建一个滚动列表视图。我想保持滚动列表视图,但添加一个导航栏(带有图像按钮或其他东西的表格)到独立于滚动列表的顶部和底部。我怎么能这样做?如何在layoutInflater中实现固定&滚动视图?

这就是我想做的事:

+--------------------------------------+ 
| 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)); 

为ListView我的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; 
    } 
} 

回答

2

好吧终于得到了这个工作。 Maximus和OcuS,感谢您的全力帮助。我是一个很大的帮助。也得到了大量的信息和例子来自这两个网站:

这里是一个工作示例。希望它能帮助任何试图做同样事情的人。

scrolling listview with fixed header/footer

的文件来做到这一点:


  • myList.java

主要活动。扩展ListActivity并包含用于填充列表的自定义适配器。


  • drawable/xml_listitem_shape.xml

这种控制采用梯度“形状”而不是图像列表项的追问下,选择和正常状态。梯度允许更快的渲染,而不是特定的设备,所以它可以让你从多个图像乱走(华电国际,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

如果您使用LinearLayout的顶部视图的权重为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> 
+0

如果你不介意,一个例子是非常有帮助的。我试图按照你的建议添加权重,但我认为我是误解。谢谢。 – wufoo 2011-04-21 18:18:36

+0

添加了一个示例。 – Maximus 2011-04-21 19:23:36

+0

我的解决方案的问题是,页眉和页脚将不会被修复,它们将移动,因为这是一个不错的解决方案...所以这是一个不好的解决方案... – OcuS 2011-04-21 19:59:05

相关问题