Android的碎片列出固定头 [英] Android List Fragment with fixed Header

查看:238
本文介绍了Android的碎片列出固定头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

\r
\r

<?XML版本=1.0编码=UTF-8? >\r
<的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android\r
    机器人:方向=横向\r
    机器人:layout_width =FILL_PARENT\r
    机器人:layout_height =100dip\r
    机器人:填充=6DIP>\r
\r
    < ImageView的\r
        机器人:ID =@ + ID / img_icon\r
        机器人:layout_width =WRAP_CONTENT\r
        机器人:layout_height =FILL_PARENT\r
        机器人:layout_alignParentBottom =真\r
        机器人:layout_alignParentTop =真\r
        机器人:layout_marginRight =6DIP\r
        机器人:contentDescription =图片/>\r
\r
    <的TextView\r
        机器人:ID =@ + ID / txtNome\r
        机器人:layout_width =FILL_PARENT\r
        机器人:layout_height =WRAP_CONTENT\r
        机器人:layout_alignParentRight =真\r
        机器人:layout_alignParentTop =真\r
        机器人:layout_toRightOf =@ + ID / img_icon\r
        机器人:TEXTSIZE =12SP\r
        机器人:重力=顶\r
        机器人:文字=诺姆/>\r
\r
    <的TextView\r
        机器人:ID =@ + ID / TXT preCO\r
        机器人:layout_width =FILL_PARENT\r
        机器人:layout_height =WRAP_CONTENT\r
        机器人:ellipsize =金字招牌\r
        机器人:layout_toRightOf =@ + ID / img_icon\r
        机器人:layout_below =@ + ID / txtNome\r
        机器人:TEXTSIZE =12SP\r
        机器人:文字=preCO/>\r
\r
    <的TextView\r
        机器人:ID =@ + ID / txtQtde\r
        机器人:layout_width =FILL_PARENT\r
        机器人:layout_height =WRAP_CONTENT\r
        机器人:TEXTSIZE =12SP\r
        机器人:文字=Qtde\r
        机器人:layout_below =@ + ID / TXT preCO\r
        机器人:layout_toRightOf =@ + ID / img_icon/>\r
\r
    <的TextView\r
        机器人:ID =@ + ID / txtTotal\r
        机器人:layout_width =FILL_PARENT\r
        机器人:layout_height =26dip\r
        机器人:TEXTSIZE =12SP\r
        机器人:文字=总计\r
        机器人:layout_alignParentBottom =真\r
        机器人:layout_toRightOf =@ + ID / img_icon\r
        机器人:layout_below =@ + ID / txtQtde/>\r
\r
< / RelativeLayout的>

\r

\r
\r

嗨!

我有一个自定义适配器FragmentList,此适配器具有一定的TextViews和Imageviews ......一切运作良好,但我试图添加一个头(带有一个按钮布局)和页脚(有一些布局Textviews和ImageView的)。
我想页眉和页脚不下来的滚动列表,但他们这样做...
可有人请我helpe?下面是一个打印和code ...

  @覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){    mAdapter =新PedidoAdapter(的getContext(),(Pedido)((MainActivity)getActivity())pedido);    setListAdapter(mAdapter);    //充气的布局该片段
    返回super.onCreateView(充气器,容器,savedInstanceState);
}@覆盖
公共无效onViewCreated(查看视图,捆绑savedInstance){
    super.onViewCreated(查看,savedInstance);    StyleSpan bssLabel =新StyleSpan(Typeface.BOLD);
    StyleSpan bssText =新StyleSpan(Typeface.NORMAL);    SimpleDateFormat的SDF =新的SimpleDateFormat(DD / MM / YYYY HH:MM:SS);
    串数据=将String.valueOf(sdf.format(((MainActivity)getActivity())pedido.getDataPedido())。);    字符串_dataPedido =数据Pedido:+数据;
    SpannableStringBuilder sbDataPedido =新SpannableStringBuilder(_dataPedido);
    sbDataPedido.setSpan(bssLabel,0,14,Spanned.SPAN_INCLUSIVE_INCLUSIVE);
    sbDataPedido.setSpan(bssText,14,_dataPedido.length(),Spanned.SPAN_INCLUSIVE_INCLUSIVE);    串_status =状态:+将String.valueOf(。((MainActivity)getActivity())pedido.getStatusPedido());
    SpannableStringBuilder sbStatus =新SpannableStringBuilder(_status);
    sbStatus.setSpan(bssLabel,0,9,Spanned.SPAN_INCLUSIVE_INCLUSIVE);
    sbStatus.setSpan(bssText,8,_status.length(),Spanned.SPAN_INCLUSIVE_INCLUSIVE);    总浮= 0;    对(INT I = 0; I&下;((MainActivity)getActivity())pedido.getItens()大小();我++){
        总+ =((MainActivity)getActivity())。pedido.getItens()。得到(ⅰ).getQtde()*((MainActivity)getActivity())。pedido.getItens()。得到(ⅰ).getProduto() .getValor();
    }    对(INT I = 0; I&下;((MainActivity)getActivity())pedido.getPizzas()大小();我++){
        总+ =((MainActivity)getActivity())。pedido.getPizzas()。得到(ⅰ).getQtde()*((MainActivity)getActivity())。pedido.getPizzas()。得到(ⅰ).getPizza() .getValor();
    }    字符串_total =总计:R $+将String.valueOf(总);
    SpannableStringBuilder sbTotal =新SpannableStringBuilder(_Total);
    sbTotal.setSpan(bssLabel,0,7,Spanned.SPAN_INCLUSIVE_INCLUSIVE);
    sbTotal.setSpan(bssText,8,_total.length(),Spanned.SPAN_INCLUSIVE_INCLUSIVE);    查看页脚=​​ LayoutInflater.from(的getContext())膨胀(R.layout.listview_pedido_footer_layout,空,假的)。    ((的TextView)footer.findViewById(R.id.txtDataPedido))的setText(sbDataPedido)。
    ((的TextView)footer.findViewById(R.id.txtStatusPedido))的setText(sbStatus)。
    ((的TextView)footer.findViewById(R.id.txtTotalPedido))的setText(sbTotal)。    查看标题= LayoutInflater.from(的getContext())膨胀(R.layout.listview_pedido_header_layout,空,假的)。    。getListView()addHeaderView(头);
    。getListView()addFooterView(页脚);
}


解决方案

席尔瓦试试这个布局请:

 <?XML版本=1.0编码=UTF-8&GT?;
<的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent>    <! - 放置在顶部的标题 - >
    < RelativeLayout的
        机器人:ID =@ + ID /头
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentTop =真
        机器人:背景=#FC9
        机器人:比重=中心>        <的TextView
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_margin =5DP
            机器人:文字=固定头
            机器人:文字颜色=#000
            机器人:TEXTSIZE =20SP/>
    < / RelativeLayout的>    &所述;! - 页脚对齐的底部 - >
    < RelativeLayout的
        机器人:ID =@ + ID /页脚
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentBottom =真
        机器人:背景=#FC0
        机器人:比重=中心>        <的TextView
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_margin =5DP
            机器人:文字=固定页脚
            机器人:文字颜色=#000
            机器人:TEXTSIZE =20SP/>
    < / RelativeLayout的>    <! - 页眉下方及以上页脚滚动项目 - >
    <滚动型
        机器人:ID =@ + ID / scrollableContents
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =FILL_PARENT
        机器人:layout_above =@ ID /页脚
        机器人:背景=#005
        机器人:layout_below =@ ID /头>        <的LinearLayout
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =WRAP_CONTENT
            机器人:重力=CENTER_HORIZONTAL
            机器人:方向=垂直>            <的TextView
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:layout_marginBottom =22dp
                机器人:layout_marginTop =22dp
                机器人:文字=项目1
                机器人:文字颜色=#CCCCCC
                机器人:TEXTSIZE =19sp/>            <的TextView
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:layout_marginBottom =22dp
                机器人:layout_marginTop =22dp
                机器人:文字=项目2
                机器人:文字颜色=#CCCCCC
                机器人:TEXTSIZE =19sp/>            <的TextView
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:layout_marginBottom =22dp
                机器人:layout_marginTop =22dp
                机器人:文字=3项
                机器人:文字颜色=#CCCCCC
                机器人:TEXTSIZE =19sp/>            <的TextView
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:layout_marginBottom =22dp
                机器人:layout_marginTop =22dp
                机器人:文字=项目4
                机器人:文字颜色=#CCCCCC
                机器人:TEXTSIZE =19sp/>            <的TextView
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:layout_marginBottom =22dp
                机器人:layout_marginTop =22dp
                机器人:文字=项目5
                机器人:文字颜色=#CCCCCC
                机器人:TEXTSIZE =19sp/>            <的TextView
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:layout_marginBottom =22dp
                机器人:layout_marginTop =22dp
                机器人:文字=项目6
                机器人:文字颜色=#CCCCCC
                机器人:TEXTSIZE =19sp/>            <的TextView
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:layout_marginBottom =22dp
                机器人:layout_marginTop =22dp
                机器人:文字=项目7
                机器人:文字颜色=#CCCCCC
                机器人:TEXTSIZE =19sp/>        < / LinearLayout中>    < /滚动型>< / RelativeLayout的>

本的Andr​​oid Studio项目可以在这里找到 https://github.com/m -vahidalizadeh /固定头,footer.git ,则可以克隆它看到的结果。


如果要动态做到这一点,你应该使用自定义阵列适配器( HTTP ://developer.android.com/reference/android/widget/ArrayAdapter.html )。然后,按照这样的:

 <?XML版本=1.0编码=UTF-8&GT?;
<的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
  的xmlns:工具=htt​​p://schemas.android.com/tool​​s
  机器人:layout_width =match_parent
  机器人:layout_height =match_parent>  <! - 头对齐顶部 - >
  < RelativeLayout的
    机器人:ID =@ + ID /头
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_alignParentTop =真
    机器人:背景=#FC9
    机器人:比重=中心>    <的TextView
      机器人:layout_width =WRAP_CONTENT
      机器人:layout_height =WRAP_CONTENT
      机器人:layout_margin =5DP
      机器人:文字=固定头
      机器人:文字颜色=#000
      机器人:TEXTSIZE =20SP/>
  < / RelativeLayout的>  <! - 页脚对准底 - >
  < RelativeLayout的
    机器人:ID =@ + ID /页脚
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_alignParentBottom =真
    机器人:背景=#FC0
    机器人:比重=中心>    <的TextView
      机器人:layout_width =WRAP_CONTENT
      机器人:layout_height =WRAP_CONTENT
      机器人:layout_margin =5DP
      机器人:文字=固定页脚
      机器人:文字颜色=#000
      机器人:TEXTSIZE =20SP/>
  < / RelativeLayout的>  <! - 页眉下方及以上页脚滚动项目 - >
  <滚动型
    机器人:ID =@ + ID / scrollableContents
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:layout_above =@ ID /页脚
    机器人:背景=#005
    机器人:layout_below =@ ID /头>     <! - 充气的滚动型的内容dynamicaly - >  < /滚动型>< / RelativeLayout的>

您可以将您的项目是这样的:

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:重力=CENTER_HORIZONTAL
    机器人:方向=垂直>    <的TextView
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_marginBottom =22dp
        机器人:layout_marginTop =22dp
        机器人:文字=项目1
        机器人:文字颜色=#CCCCCC
        机器人:TEXTSIZE =19sp/>    <的TextView
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_marginBottom =22dp
        机器人:layout_marginTop =22dp
        机器人:文字=项目2
        机器人:文字颜色=#CCCCCC
        机器人:TEXTSIZE =19sp/>    <的TextView
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_marginBottom =22dp
        机器人:layout_marginTop =22dp
        机器人:文字=3项
        机器人:文字颜色=#CCCCCC
        机器人:TEXTSIZE =19sp/>    <的TextView
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_marginBottom =22dp
        机器人:layout_marginTop =22dp
        机器人:文字=项目4
        机器人:文字颜色=#CCCCCC
        机器人:TEXTSIZE =19sp/>    <的TextView
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_marginBottom =22dp
        机器人:layout_marginTop =22dp
        机器人:文字=项目5
        机器人:文字颜色=#CCCCCC
        机器人:TEXTSIZE =19sp/>    <的TextView
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_marginBottom =22dp
        机器人:layout_marginTop =22dp
        机器人:文字=项目6
        机器人:文字颜色=#CCCCCC
        机器人:TEXTSIZE =19sp/>    <的TextView
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_marginBottom =22dp
        机器人:layout_marginTop =22dp
        机器人:文字=项目7
        机器人:文字颜色=#CCCCCC
        机器人:TEXTSIZE =19sp/>< / LinearLayout中>

然后,您应该填充内容到中间滚动型是这样的:

 进口android.app.Activity;
进口android.os.Bundle;
进口android.widget.ScrollView;公共类MainActivity延伸活动{    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.scrollable_contents);        滚动型scrollable_contents =(滚动型)findViewById(R.id.scrollableContents);
        。getLayoutInflater()膨胀(R.layout.contents,scrollable_contents);    }}

我希望它能帮助:)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="100dip"
    android:padding="6dip" >

    <ImageView
        android:id="@+id/img_icon"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="6dip"
        android:contentDescription="Image" />

    <TextView
        android:id="@+id/txtNome"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/img_icon"
        android:textSize="12sp"
        android:gravity="top"
        android:text="Nome" />

    <TextView
        android:id="@+id/txtPreco"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:ellipsize="marquee"
        android:layout_toRightOf="@+id/img_icon"
        android:layout_below="@+id/txtNome"
        android:textSize="12sp"
        android:text="Preço" />

    <TextView
        android:id="@+id/txtQtde"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="12sp"
        android:text="Qtde"
        android:layout_below="@+id/txtPreco"
        android:layout_toRightOf="@+id/img_icon" />

    <TextView
        android:id="@+id/txtTotal"
        android:layout_width="fill_parent"
        android:layout_height="26dip"
        android:textSize="12sp"
        android:text="Total"
        android:layout_alignParentBottom="true"
        android:layout_toRightOf="@+id/img_icon"
        android:layout_below="@+id/txtQtde" />

</RelativeLayout>

Hey guys!

I have a FragmentList with a custom Adapter, this adapter has some TextViews and Imageviews... Everything works well but, i'm trying to add a Header (A layout with a button) and a Footer (A layout with some Textviews and an ImageView). I'd like the Header and Footer not to scroll down with the list, but they do... Can someone please helpe me? Below is A print and the code...

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    mAdapter = new PedidoAdapter(getContext(), (Pedido)((MainActivity) getActivity()).pedido);

    setListAdapter(mAdapter);

    // Inflate the layout for this fragment
    return super.onCreateView(inflater, container, savedInstanceState);
}

@Override
public void onViewCreated(View view, Bundle savedInstance){
    super.onViewCreated(view, savedInstance);

    StyleSpan bssLabel = new StyleSpan(Typeface.BOLD);
    StyleSpan bssText = new StyleSpan(Typeface.NORMAL);

    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
    String Data = String.valueOf(sdf.format(((MainActivity) getActivity()).pedido.getDataPedido()));

    String _dataPedido = "Data Pedido.: " + Data;
    SpannableStringBuilder sbDataPedido = new SpannableStringBuilder(_dataPedido);
    sbDataPedido.setSpan(bssLabel, 0, 14, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
    sbDataPedido.setSpan(bssText, 14, _dataPedido.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);

    String _status = "Status.: " + String.valueOf(((MainActivity) getActivity()).pedido.getStatusPedido());
    SpannableStringBuilder sbStatus = new SpannableStringBuilder(_status);
    sbStatus.setSpan(bssLabel, 0, 9, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
    sbStatus.setSpan(bssText, 8, _status.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);

    float total = 0;

    for(int i = 0; i < ((MainActivity) getActivity()).pedido.getItens().size(); i++){
        total += ((MainActivity) getActivity()).pedido.getItens().get(i).getQtde() * ((MainActivity) getActivity()).pedido.getItens().get(i).getProduto().getValor();
    }

    for(int i = 0; i < ((MainActivity) getActivity()).pedido.getPizzas().size(); i++){
        total += ((MainActivity) getActivity()).pedido.getPizzas().get(i).getQtde() * ((MainActivity) getActivity()).pedido.getPizzas().get(i).getPizza().getValor();
    }

    String _total = "Total.: R$ " + String.valueOf(total);
    SpannableStringBuilder sbTotal = new SpannableStringBuilder(_total);
    sbTotal.setSpan(bssLabel, 0, 7, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
    sbTotal.setSpan(bssText, 8, _total.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);

    View footer = LayoutInflater.from(getContext()).inflate(R.layout.listview_pedido_footer_layout, null, false);

    ((TextView) footer.findViewById(R.id.txtDataPedido)).setText(sbDataPedido);
    ((TextView)footer.findViewById(R.id.txtStatusPedido)).setText(sbStatus);
    ((TextView)footer.findViewById(R.id.txtTotalPedido)).setText(sbTotal);

    View header = LayoutInflater.from(getContext()).inflate(R.layout.listview_pedido_header_layout, null, false);

    getListView().addHeaderView(header);
    getListView().addFooterView(footer);
}

解决方案

Silva try this layout please:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <!-- Header aligned to the top -->
    <RelativeLayout
        android:id="@+id/header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:background="#FC9"
        android:gravity="center" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:text="Fixed Header"
            android:textColor="#000"
            android:textSize="20sp" />
    </RelativeLayout>

    <!-- Footer aligned to the bottom -->
    <RelativeLayout
        android:id="@+id/footer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="#FC0"
        android:gravity="center" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:text="Fixed Footer"
            android:textColor="#000"
            android:textSize="20sp" />
    </RelativeLayout>

    <!-- Scrollable Item below header and above footer -->
    <ScrollView
        android:id="@+id/scrollableContents"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@id/footer"
        android:background="#005"
        android:layout_below="@id/header" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:orientation="vertical" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="22dp"
                android:layout_marginTop="22dp"
                android:text="Item 1"
                android:textColor="#CCCCCC"
                android:textSize="19sp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="22dp"
                android:layout_marginTop="22dp"
                android:text="Item 2"
                android:textColor="#CCCCCC"
                android:textSize="19sp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="22dp"
                android:layout_marginTop="22dp"
                android:text="Item 3"
                android:textColor="#CCCCCC"
                android:textSize="19sp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="22dp"
                android:layout_marginTop="22dp"
                android:text="Item 4"
                android:textColor="#CCCCCC"
                android:textSize="19sp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="22dp"
                android:layout_marginTop="22dp"
                android:text="Item 5"
                android:textColor="#CCCCCC"
                android:textSize="19sp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="22dp"
                android:layout_marginTop="22dp"
                android:text="Item 6"
                android:textColor="#CCCCCC"
                android:textSize="19sp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="22dp"
                android:layout_marginTop="22dp"
                android:text="Item 7"
                android:textColor="#CCCCCC"
                android:textSize="19sp" />

        </LinearLayout>

    </ScrollView>

</RelativeLayout>

This Android studio project is available here https://github.com/m-vahidalizadeh/fixed-header-footer.git, you can clone it to see the result.


If you want to do it dynamically, you should use custom array adapter (http://developer.android.com/reference/android/widget/ArrayAdapter.html). Then, follow this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent" >

  <!-- Header aligned to top -->
  <RelativeLayout
    android:id="@+id/header"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:background="#FC9"
    android:gravity="center" >

    <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_margin="5dp"
      android:text="Fixed Header"
      android:textColor="#000"
      android:textSize="20sp" />
  </RelativeLayout>

  <!-- Footer aligned to bottom -->
  <RelativeLayout
    android:id="@+id/footer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:background="#FC0"
    android:gravity="center" >

    <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_margin="5dp"
      android:text="Fixed Footer"
      android:textColor="#000"
      android:textSize="20sp" />
  </RelativeLayout>

  <!-- Scrollable Item below header and above footer -->
  <ScrollView
    android:id="@+id/scrollableContents"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_above="@id/footer"
    android:background="#005"
    android:layout_below="@id/header" >

     <!-- Inflate the contents of the ScrollView dynamicaly -->

  </ScrollView>

</RelativeLayout>

you can add your items like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="22dp"
        android:layout_marginTop="22dp"
        android:text="Item 1"
        android:textColor="#CCCCCC"
        android:textSize="19sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="22dp"
        android:layout_marginTop="22dp"
        android:text="Item 2"
        android:textColor="#CCCCCC"
        android:textSize="19sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="22dp"
        android:layout_marginTop="22dp"
        android:text="Item 3"
        android:textColor="#CCCCCC"
        android:textSize="19sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="22dp"
        android:layout_marginTop="22dp"
        android:text="Item 4"
        android:textColor="#CCCCCC"
        android:textSize="19sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="22dp"
        android:layout_marginTop="22dp"
        android:text="Item 5"
        android:textColor="#CCCCCC"
        android:textSize="19sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="22dp"
        android:layout_marginTop="22dp"
        android:text="Item 6"
        android:textColor="#CCCCCC"
        android:textSize="19sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="22dp"
        android:layout_marginTop="22dp"
        android:text="Item 7"
        android:textColor="#CCCCCC"
        android:textSize="19sp" />

</LinearLayout>

Then, you should populate your content into your middle ScrollView like this:

import android.app.Activity;
import android.os.Bundle;
import android.widget.ScrollView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.scrollable_contents);

        ScrollView scrollable_contents = (ScrollView) findViewById(R.id.scrollableContents);
        getLayoutInflater().inflate(R.layout.contents, scrollable_contents);

    }

}

I hope it helps :)

这篇关于Android的碎片列出固定头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆