2016-01-23 65 views
0

嗨我想在我的碎片之一有一個listView。我試圖addHeaderView和addFooterView但滾動與列​​表一起頁眉和頁腳,所以我嘗試了不同的方法,但標頭和被重複用於每個元件爲ListView中的每個元素重複的頁眉和頁腳?

我的片段佈局

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 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" 
    android:orientation="vertical" 
    tools:context="com.solutionnest.fragment.RemoteListFragment"> 
    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:layout_centerHorizontal="true" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:text="Device List" 
     android:id="@+id/listHeader" 
     android:singleLine="true" 
     android:background="#f4438fd3" 
     android:textColor="#ffffffff" 
     android:layout_marginBottom="10dp" /> 
<RelativeLayout android:layout_width="match_parent" android:id="@+id/listContainer" 
    android:layout_height="0dp" 
    android:layout_weight="1"> 
    <ListView android:id="@android:id/list" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 
    <TextView android:id="@+id/deviceName" android:layout_width="match_parent" 
     android:textSize="@dimen/listElementSize" 
     android:layout_height="@dimen/ListBoxSize" android:gravity="center" /> 
</RelativeLayout> 
    <LinearLayout 
     android:id="@+id/footer" 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent" 
     android:layout_centerHorizontal="true" 
     android:orientation="horizontal"> 
     <Button 
      android:layout_width="0dp" 
      android:layout_weight="0.50" 
      android:text="Add" 
      android:onClick="addDevice" 
      android:id="@+id/addDevice" 
      android:layout_height="match_parent" 
      android:background="#f4438fd3" 
      android:textSize="15dp" 
      android:textColor="#ffffffff" 
      android:layout_marginLeft="5dp" 
      /> 

     <Button 
      android:layout_width="0dp" 
      android:layout_weight="0.50" 
      android:text="Delete" 
      android:onClick="deleteDevice" 
      android:id="@+id/deleteDevice" 
      android:layout_height="match_parent" 
      android:background="#f4438fd3" 
      android:textSize="15dp" 
      android:textColor="#ffffffff" 
      android:layout_marginLeft="5dp"/> 
    </LinearLayout> 
</LinearLayout> 

我getView方法在頁腳適配器看起來像

public View getView(int position, View convertView, ViewGroup parent) { 
     ViewHolder holder = null; 
     Device device = (Device)getItem(position); 
     if (convertView == null) { 
      convertView = inflater.inflate(R.layout.fragment_remote_list,parent ,false); 
      holder = new ViewHolder(); 
      holder.name = (TextView) convertView.findViewById(R.id.deviceName); 
      convertView.setTag(holder); 
     } else { 
      holder = (ViewHolder) convertView.getTag(); 
     } holder.name.setText(device.getDeviceName()); 


     return convertView; 

    } 
+2

你的目標是什麼?如果你想爲整個列表添加一個頁眉和頁腳,它們不會隨列表一起滾動,將頁眉,ListView和頁腳放到垂直的LinearLayout中。 – CommonsWare

回答

0

後喜有點鬥爭得到這個固定.Posting爲me..It什麼工作似乎列表的內容必須是單獨的佈局文件的一部分,以達到上述要求,我的新佈局和視圖配置是`

public View getView(int position, View convertView, ViewGroup parent) { 
     ViewHolder holder = null; 
     Device device = (Device)getItem(position); 
     if (convertView == null) { 
      convertView = inflater.inflate(R.layout.fragment_remote_list_content,parent ,false); 
      holder = new ViewHolder(); 
      holder.name = (TextView) convertView.findViewById(R.id.deviceName); 
      convertView.setTag(holder); 
     } else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 
     if(deviceList.isEmpty()) 
     { 
      holder.name.setText("No Devices"); 
     } 
     else { 
      holder.name.setText(device.getDeviceName()); 
      convertView.setOnClickListener(new OnItemClickListener(position)); 
     } 

     return convertView; 

    } 

`的fragment_remote_list.xml佈局被分成2個文件

fragment_remote_list.xml

<?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="fill_parent" 
    android:layout_height="fill_parent" 
    tools:context="com.solutionnest.fragment.RemoteListFragment"> 

    <ListView android:id="@android:id/list" 
     android:layout_width="match_parent" 
     android:layout_height="fill_parent" 
     android:layout_above="@+id/footer" 
     android:layout_below="@+id/listHeader"/> 


    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:text="Device List" 
     android:id="@+id/listHeader" 
     android:singleLine="true" 
     android:background="#f4438fd3" 
     android:textColor="#ffffffff" 
     android:layout_marginBottom="10dp" /> 
    <LinearLayout 
     android:id="@+id/footer" 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent" 
     android:gravity="center" 
     android:layout_alignParentBottom="true" 
     android:orientation="horizontal"> 
     <Button 
      android:layout_width="0dp" 
      android:layout_weight="0.50" 
      android:text="Add" 
      android:onClick="addDevice" 
      android:id="@+id/addDevice" 
      android:layout_height="match_parent" 
      android:background="#f4438fd3" 
      android:textSize="15dp" 
      android:textColor="#ffffffff" 
      android:layout_marginLeft="5dp" 
      /> 

     <Button 
      android:layout_width="0dp" 
      android:layout_weight="0.50" 
      android:text="Delete" 
      android:onClick="deleteDevice" 
      android:id="@+id/deleteDevice" 
      android:layout_height="match_parent" 
      android:background="#f4438fd3" 
      android:textSize="15dp" 
      android:textColor="#ffffffff" 
      android:layout_marginLeft="5dp"/> 
    </LinearLayout> 



</RelativeLayout> 

fragment_remote_list_content.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <TextView android:id="@+id/deviceName" android:layout_width="match_parent" 
     android:textSize="@dimen/listElementSize" 
     android:layout_height="@dimen/ListBoxSize" android:gravity="center" /> 
</LinearLayout> 
0

一個簡單的方法來創建頁眉和頁腳。

在主要活動:

public class MainActivity extends Activity { 
    ListView listView; 
    TextView tv; 

    static final String[] numbers = new String[]{"one", "two", "three", 
     "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", 
     "twelve", "thirteen", "fourteen", "fifteen", "sixteen", 
     "seventeen", "eighteen", "nineteen", "twenty", "twenty one", 
     "twenty two"}; 
    View header; 
    ViewGroup footer; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     listView = (ListView) findViewById(R.id.list_id); 

     //code to add header and footer to listview 
     LayoutInflater inflater = getLayoutInflater(); 
     ViewGroup header = (ViewGroup) inflater.inflate(R.layout.header,  listView, 
      false); 
     ViewGroup footer = (ViewGroup) inflater.inflate(R.layout.footer, listView, 
      false); 
     listView.addHeaderView(header, null, false); 
     listView.addFooterView(footer, null, false); 
     ArrayAdapter adapter = new ArrayAdapter(this, 
      android.R.layout.simple_list_item_1, numbers); 
     listView.setAdapter(adapter); 
    } 

在活動XML。

<ListView 
    android:id="@+id/list_id" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:columnWidth="80dp" 
    android:gravity="center" 
    android:numColumns="auto_fit" 
    android:stretchMode="columnWidth" > 
</ListView> 

在資源/佈局創建兩個XML文件

header.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical" > 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center_horizontal" 
     android:text="HEADER" 
     android:background="#cf9f99" /> 

</LinearLayout> 

footer.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical" > 

    <TextView 
     android:id="@+id/tv" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center_horizontal" 
     android:text="FOOTER" 
     android:background="#cf9f99" /> 

</LinearLayout> 

enter image description hereenter image description here

+0

嗨@Ms Yvette感謝您的快速響應只是想檢查這將不會使頁眉和頁腳粘到列表中,而不會滾動滾動 –

+0

這會使頁眉和頁腳附加到列表中,因此您只能在列表中看到它們列表的頂部和底部,因此只能在列表頂部看到標題,而在底部看到頁眉。 –

+0

是的,所以這是不需要的:)。我需要將頁眉和頁腳固定到屏幕上,當列表滾動時,它應該保持固定在屏幕上(浮動) –