将视图放在Listfragment上方

时间:2014-02-06 22:57:08

标签: android android-arrayadapter android-listfragment

我有一个listfragment,它返回一个来自arrayadapter的视图列表。我想知道是否可以在activty中的listfragment上面有其他视图(一个spinner和textview,它们位于一个单独的布局文件中)。我如何更改布局文件以实现此目的?

适配器的rowlayout文件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="fill_parent"
    android:orientation="vertical" >

    <!--Rank number-->
    <TextView 
        android:id="@+id/rank"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@+id/label"
        android:textSize="40px"
        />

    <!--  -->

    <!--Device image-->
    <ImageView
        android:id="@+id/icon"
        android:layout_width="60px"
        android:layout_height="60px"
        android:layout_marginLeft="15px"
        android:layout_marginRight="10px"
        android:layout_marginTop="4px"
        android:layout_toRightOf="@+id/rank"
        android:src="@drawable/ic_launcher" />


    <!--Caption-->
    <TextView 
        android:id="@+id/caption"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@+id/label"
        android:layout_below="@+id/icon"
        android:layout_marginLeft="25px"
        />



</RelativeLayout>

布局文件topview.xml,包含要放在上面的视图:

<?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:orientation="vertical" >

    <!--Category text-->
    <TextView 
        android:id="@+id/text_chooseCategory"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

    <!--drop down-->
    <Spinner 
        android:id="@+id/list_cateory"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>

这是一个arrayadapter类:

package com.example.listfragmentexample;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

    public class SimpleArrayAdapter extends ArrayAdapter<String> {
        //arrays and context
        //call it to get information regarding another part of your program (activity, package/application)
        Context context;
        String ranks[];
        String caption[] = new String[] {"LG G2", "HTC One", "Google Nexus 5", "iPhone 5s", 
                "Sony Xperia Z1 Compact", "Samsung Galaxy S4", "Samsung Galaxy Note 3", "Motorola Moto G", "BlackBerry Q10",
                "Nokia Lumia 1020"};
        int images[] = new int[] {R.drawable.ic_launcher};

        //viewholder class
        public class ViewHolder {
            TextView rankView;
            ImageView imageView;
            TextView captionView;
        }


        public SimpleArrayAdapter(Context context, String[] ranks) {
            super(context, R.layout.rowlayout, ranks);
            this.context = context;
            this.ranks = ranks;
        }

        //return multiple views
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            //Layout inflater instantiates XML layout file
            //get system service.
            LayoutInflater inflater = (LayoutInflater) context.
                    getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            ViewHolder holder = new ViewHolder();
            //get row view. xml file and root (parent)
            //used with layout inflater

            View rootView = inflater.inflate(R.layout.rowlayout, parent, false);
            holder.rankView = (TextView) rootView.findViewById(R.id.rank);
            holder.imageView = (ImageView) rootView.findViewById(R.id.icon);
            holder.captionView = (TextView) rootView.findViewById(R.id.caption);

            //set the data 
            holder.rankView.setText(ranks[position]);
            /*holder.imageView.setImageResource(R.drawable.ic_launcher);*/
            holder.captionView.setText(caption[position]);

            return rootView;
        }


    }


The adapter is set in a listfragment class, which itself is started in another activity.

Happy to clarify and thanks in advance.

1 个答案:

答案 0 :(得分:0)

您可以将自定义布局扩展到ListFragment,如ListView documentation,类概述部分中所述。此自定义布局必须包含一个ID为#34; @android:id / list&#34;的ListView。和您需要的其他控件(如Spinner,TextView等)。