在ListView中为每个元素重复页眉和页脚?

时间:2016-01-23 14:51:50

标签: android listview

您好我正试图在我的一个片段中有一个listView。我尝试了addHeaderView和addFooterView,但是滚动页眉和页脚以及列表,所以我尝试了不同的方法,但页眉和页脚正在为每个元素重复

我的片段布局

    <?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 个答案:

答案 0 :(得分: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>

在resources / layout

中创建两个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 here enter image description here

答案 1 :(得分:0)

嗨,经过一番挣扎得到了这个修复。发布对我有用的东西..似乎列表内容必须是单独布局文件的一部分才能达到上述要求我的新布局和视图配置是`

 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>