2010-11-24 57 views
84

我正在开发一个应用程序,在我的应用程序中,我使用ListView来显示使用dom解析的数据,我想在listview中添加页脚,当我单击页脚添加更多数据添加到列表视图时,我附加图像设计过程中,请参阅此搜索和imgae2.I提页脚红色长方形如何在ListView中添加页脚?

FIG1页脚像 “更多新闻”
​​

alt text

Fig2-添加额外的10条中增加listview

回答

199

创建一个由文字要设置为页脚,然后一个页脚视图布局尝试

View footerView = ((LayoutInflater) ActivityContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer_layout, null, false); 
ListView.addFooterView(footerView); 

的页脚布局可能是这样的:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:paddingTop="7dip" 
    android:paddingBottom="7dip" 
    android:orientation="horizontal" 
    android:gravity="center"> 

    <LinearLayout 
     android:id="@+id/footer_layout" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:gravity="center" 
     android:layout_gravity="center"> 

    <TextView 
     android:text="@string/footer_text_1" 
     android:id="@+id/footer_1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textSize="14dip" 
     android:textStyle="bold" 
     android:layout_marginRight="5dip" /> 
    </LinearLayout> 
</LinearLayout> 

Activity类可能是:

public class MyListActivty extends ListActivity { 
    private Context context = null; 
    private ListView list = null; 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     list = (ListView)findViewById(android.R.id.list); 

     //code to set adapter to populate list 
     View footerView = ((LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer_layout, null, false); 
     list.addFooterView(footerView); 
    } 
} 
+6

这是好事,但如果你想在屏幕上固定的页脚,而不是只在滚动的底部可见?而且,如果您希望即使列表为“空”,也可以看到页脚?请参阅http://stackoverflow.com/questions/12353701/fixed-and-always-visible-footer-below-listfragment上的完整问题。 – gcl1 2012-09-10 14:30:07

9

我知道这是一个很老的问题,但我在这里搜索了一下,发现了答案pr并非100%满意,因为正如gcl1提到的那样 - 这种方式页脚并不是真正的屏幕页脚 - 它只是列表中的“附加”。

底线 - 为别人谁可以在这里谷歌自己的方式 - 我发现了以下建议位置:Fixed and always visible footer below ListFragment

尝试做如下,其中的重点是在列出的第一个按钮(或页脚元素) XML - 然后在列表中添加“layout_above”:

<RelativeLayout> 

<Button android:id="@+id/footer" android:layout_alignParentBottom="true"/> 
<ListView android:id="@android:id/list" **android:layout_above**="@id/footer"> <!-- the list --> 

</RelativeLayout> 
2

要在其中添加列表视图页脚和我也有生成列表视图上点击页脚的事件活动。

public class MainActivity extends Activity 
{ 

     @Override 
     protected void onCreate(Bundle savedInstanceState) 
     { 

      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 

      ListView list_of_f = (ListView) findViewById(R.id.list_of_f); 

      LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

      View view = inflater.inflate(R.layout.web_view, null); // i have open a webview on the listview footer 

      RelativeLayout layoutFooter = (RelativeLayout) view.findViewById(R.id.layoutFooter); 

      list_of_f.addFooterView(view); 

     } 

} 

activity_main.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:background="@drawable/bg" > 

    <ImageView 
     android:id="@+id/dept_nav" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@drawable/dept_nav" /> 

    <ListView 
     android:id="@+id/list_of_f" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/dept_nav" 
     android:layout_margin="5dp" 
     android:layout_marginTop="10dp" 
     android:divider="@null" 
     android:dividerHeight="0dp" 
     android:listSelector="@android:color/transparent" > 
    </ListView> 

</RelativeLayout> 
5

如果ListView控件是ListActivity的孩子:

getListView().addFooterView(
    getLayoutInflater().inflate(R.layout.footer_view, null) 
); 

(里面的onCreate())

8

此题答案有点过时。虽然代码保持不变,但行为有一些变化。

public class MyListActivity extends ListActivity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     TextView footerView = (TextView) ((LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer_view, null, false); 
     getListView().addFooterView(footerView); 
     setListAdapter(new ArrayAdapter<String>(this, getResources().getStringArray(R.array.news))); 
    } 
} 

信息关于addFooterView()方法

添加一个固定的视图显示在列表的底部。如果多次调用addFooterView(),视图将按照它们添加的顺序显示。如果他们想要,使用此调用添加的视图可以关注焦点。

最上方的压力非常重要的一点问题的答案 -

addFooterView()必须在调用setAdapter()。这之前,被称为是这样的ListView可以换一个附带的光标也将占页眉和页脚的意见。

从Kitkat这已经改变。

注意:首次引入时,只能在使用setAdapter(ListAdapter)设置适配器之前调用此方法。从KITKAT开始,可以随时调用此方法。如果ListView的适配器不扩展HeaderViewListAdapter,它将被WrapperListAdapter的支持实例包装。

Documentation