【问题标题】:Placing views above a Listfragment将视图置于 Listfragment 之上
【发布时间】:2014-02-06 22:57:07
【问题描述】:

我有一个 listfragment,它从 arrayadapter 返回视图列表。我想知道是否有可能在活动的列表片段上方有其他视图(微调器和文本视图,它们位于单独的布局文件中)。我需要如何更改布局文件才能实现这一目标?

适配器的行布局文件 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>

这是一个数组适配器类:

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.

【问题讨论】:

    标签: android android-arrayadapter android-listfragment


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多