2016-02-14 2 views
2

Android Список фрагментов с фиксированным заголовком

<?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>

Эй, ребята!

У меня есть FragmentList с пользовательским адаптером, этот адаптер имеет некоторые текстовые элементы и изображения ... Все работает хорошо, но я пытаюсь добавить заголовок (макет с кнопкой) и нижний колонтитул (макет с некоторые Textviews и ImageView). Я бы хотел, чтобы верхний и нижний колонтитулы не прокручивались вниз со списком, но они ... Может кто-нибудь угодить мне? Ниже Отпечаток и код ...

@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); 
} 
+0

Вы получили свой ответ приятель? – Mohammad

+0

Привет, Мохаммад! Да, большое спасибо, это работает как шарм ... Ты всегда помогаешь! Надеюсь, у меня всегда найдется учитель! Еще раз спасибо! –

+0

Добро пожаловать. У меня всегда есть как приятель :) – Mohammad

ответ

0

Silva попробовать этот макет пожалуйста:

<?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> 

Этот проект Android студии можно посмотреть здесь https://github.com/m-vahidalizadeh/fixed-header-footer.git, вы можете клонировать его, чтобы увидеть результат.


Если вы хотите сделать его динамически, вы должны использовать адаптер пользовательский массив (http://developer.android.com/reference/android/widget/ArrayAdapter.html). Затем выполните следующее:

<?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> 

вы можете добавить свои элементы, как это:

<?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> 

Затем вы должны заполнить ваше содержание в ваш средний ScrollView как это:

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

    } 

} 

Я надеюсь, это помогает :)

+0

Привет, Мохаммад! Спасибо за ответ! Мой ListView динамичен, как бы раздуть этот макет в getView BaseAdapter? Во время инфляции будут завышены только предметы зрения Скроола? Заранее спасибо! –

+0

Hi Silva, вы должны использовать пользовательский адаптер массива. Я редактировал свой пост, чтобы вы могли видеть, как раздувать элементы внутри вашего ScrollView. – Mohammad

Смежные вопросы