把一个TextView在列表片段的顶部 [英] Putting a Textview on top of a List Fragment

查看:106
本文介绍了把一个TextView在列表片段的顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何添加一个TextView上的片段上。我使用下面的适配器来填充listfragment和XML文件在列表中的片段的每一行。我需要添加一个TextView ontop列表视图中必须是滚动伴随着名单?

 公共类适配器扩展了BaseAdapter {
    上下文语境;

    公众的TextView txtName的;
    公众的TextView txtTitle;


    私人LayoutInflater mInflater;
    私人存储的存储;
    FontManager fontManager;
    字体字样;

    公共适配器(上下文_context,存储_storage){
        上下文= _context;

        mInflater = LayoutInflater.from(上下文);
        this.storage = _storage;

        fontManager =新FontManager(_context);
        字体= fontManager.getTypeFace();

    }

    @覆盖
    公众诠释getCount将(){
        // TODO自动生成方法存根
        返回_storage.size();
    }

    @覆盖
    公共对象的getItem(INT为arg0){
        // TODO自动生成方法存根
        返回null;
    }

    @覆盖
    众长getItemId(INT为arg0){
        // TODO自动生成方法存根
        返回0;
    }

    @覆盖
    公共查看getView(INT位置,查看convertView,ViewGroup中父){
        // TODO自动生成方法存根

        convertView = mInflater.inflate(R.layout.row3,NULL);

        txtName的=(的TextView)convertView.findViewById(R.id.tvName);
        txtName.setTypeface(字体);
        txtName.setText(storage.getName(位置));

        txtTitle =(TextView中)convertView.findViewById(R.id.tvTitle);
        txtTitle.setTypeface(字体);
        txtTitle.setText(storage.getTitle(位置));

        返回convertView;
    }
}
 

XML:

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT
    机器人:重力=center_vertical
    机器人:方向=垂直
    机器人:填充=20dp
    机器人:滚动条=无
    机器人:ID =@ + ID / LL1>

    <的TextView
        机器人:ID =@ + ID / tvName
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:文本=TextView的
        机器人:文字颜色=@机器人:彩色/黑白
        机器人:TEXTSIZE =20SP
        机器人:TEXTSTYLE =黑体/>

    <的TextView
        机器人:ID =@ + ID / tvTitle
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:文本=TextView的
        机器人:文字颜色=@机器人:彩色/黑白/>

< / LinearLayout中>
 

ListFragment:

 公共类SampleListFragment扩展ListFragment {

    INT编号;

    @覆盖
    公共无效onActivityCreated(包savedInstanceState){
        // TODO自动生成方法存根
        super.onActivityCreated(savedInstanceState);
    }

    @覆盖
    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
            捆绑savedInstanceState){
        数= getArguments()调用getInt(
                NUM,0);
        新SampleAsyncTask(这一点,getActivity(),数量).execute();
        返回super.onCreateView(充气,容器,savedInstanceState);
    }

    @覆盖
    公共无效onListItemClick(ListView的L,视图V,INT位置,长的id){
        // TODO自动生成方法存根
        }
    }
}
 

随着code采用的是AsyncTask的类来填充列表片段

 转接适配器=新的适配器(背景下,储存);
listFragment.setListAdapter(适配器);
 

解决方案

您可以做如下或作为头添加一个TextView到您的listivew

从文档报价

ListActivity(ListFragment类似)具有默认布局由单个,全屏列表在屏幕的中心。但是,如果你的愿望,你可以用的setContentView设定自己的视图布局自定义屏幕布局()中的onCreate()。要做到这一点,你自己的看法必须包含的ID @机器人:ID /列表一 ListView`对象(或列表,如果它在code)

activity_main.xml

 < RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:paddingBottom会=@扪/ activity_vertical_margin
    机器人:以下属性来=@扪/ activity_horizo​​ntal_margin
    机器人:paddingRight =@扪/ activity_horizo​​ntal_margin
    机器人:paddingTop =@扪/ activity_vertical_margin
    工具:上下文=MainActivity。>

     <片段机器人:名称=com.example.listfragment.MyFragment
            机器人:ID =@ + ID / FRAG
            机器人:layout_above =@ + ID /按钮1
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =FILL_PARENT/>


< / RelativeLayout的>
 

MyFragment.java

 公共类MyFragment扩展ListFragment {

    字符串名称[] = {A,B,C};
    @覆盖
    公共无效onActivityCreated(包savedInstanceState){
        // TODO自动生成方法存根
        super.onActivityCreated(savedInstanceState);
    }
     @覆盖
     公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
       捆绑savedInstanceState){
      查看myFragmentView = inflater.inflate(R.layout.list_frag,集装箱,假);
      TextView的电视=(TextView中)myFragmentView.findViewById(R.id.textView1);
      tv.setText(我的头);
      setListAdapter(新ArrayAdapter<字符串>(getActivity(),android.R.layout.simple_list_item_1,名称));
      返回myFragmentView;
     }
     @覆盖
     公共无效onListItemClick(ListView的L,视图V,INT位置,长的id){
         //在点击显示在敬酒的项目
          Toast.makeText(getActivity(),(串)l.getItemAtPosition(位置),Toast.LENGTH_SHORT).show();
         }
}
 

list_frag.xml

 < XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent>

    <的TextView
        机器人:ID =@ + ID / textView1
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentTop =真
        机器人:layout_centerHorizo​​ntal =真
        机器人:文本=TextView的/>

    <的ListView
        机器人:ID =@机器人:ID /列表
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_below =@ + ID / textView1
        机器人:layout_centerHorizo​​ntal =真正的>

    < / ListView控件>

< / RelativeLayout的>
 

快照

编辑:如果您希望的TextView滚动

 < XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent>

    <的TextView
        机器人:ID =@ + ID / textView1
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentTop =真
        机器人:layout_centerHorizo​​ntal =真
        机器人:layout_marginTop =19dp
        机器人:文本=TextView的/>

< / RelativeLayout的>
 

在调用setListAdapter

 查看视图= inflater.inflate(R.layout.text,NULL);
  TextView的textinlfated =(TextView中)view.findViewById(R.id.textView1);
  ListView的LV = getListView();
  textinlfated.setText(TextView中滚动);
  lv.addHeaderView(视图);
 

How Can I add a textview on top of a fragment. I'm using the following adapter to populate the listfragment and the xml file is each row in the list fragment. I need to add a textview ontop of listview which must be scrollable along with the list?

     public class Adapter extends BaseAdapter {
    Context context;

    public TextView txtName;
    public TextView txtTitle;


    private LayoutInflater mInflater;
    private Storage storage;
    FontManager fontManager;
    Typeface typeface;

    public Adapter(Context _context,Storage _storage) {
        context = _context;

        mInflater = LayoutInflater.from(context);
        this.storage = _storage;

        fontManager = new FontManager(_context);
        typeface = fontManager.getTypeFace();

    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return _storage.size();
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        convertView = mInflater.inflate(R.layout.row3, null);

        txtName = (TextView) convertView.findViewById(R.id.tvName);
        txtName.setTypeface(typeface);
        txtName.setText(storage.getName(position));

        txtTitle = (TextView) convertView.findViewById(R.id.tvTitle);
        txtTitle.setTypeface(typeface);
        txtTitle.setText(storage.getTitle(position));

        return convertView;
    }
}

xml:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:orientation="vertical"
    android:padding="20dp"
    android:scrollbars="none" 
    android:id="@+id/ll1">

    <TextView
        android:id="@+id/tvName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textColor="@android:color/black"
        android:textSize="20sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/tvTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textColor="@android:color/black" />

</LinearLayout>

ListFragment:

    public class SampleListFragment extends ListFragment {

    int number;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        number = getArguments().getInt(
                "num", 0);
        new SampleAsyncTask(this, getActivity(), number).execute();
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub      
        }
    }
}

Following code is used in AsyncTask class to populate the list fragment

Adapter adapter = new Adapter(context, storage);
listFragment.setListAdapter(adapter);

解决方案

You can do as below or add a textview as a header to your listivew

Quoting from the docs

ListActivity(ListFragment is similar) has a default layout that consists of a single, full-screen list in the center of the screen. However, if you desire, you can customize the screen layout by setting your own view layout with setContentView() in onCreate(). To do this, your own view MUST contain aListView` object with the id "@android:id/list" (or list if it's in code)

activity_main.xml

 <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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

     <fragment android:name="com.example.listfragment.MyFragment"
            android:id="@+id/frag"
            android:layout_above="@+id/button1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />


</RelativeLayout>

MyFragment.java

public class MyFragment extends ListFragment {

    String names[] ={"A","B","C"};
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
    }
     @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container,
       Bundle savedInstanceState) {
      View myFragmentView = inflater.inflate(R.layout.list_frag, container, false);
      TextView tv = (TextView) myFragmentView.findViewById(R.id.textView1);
      tv.setText("My Header");
      setListAdapter(new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,names));
      return myFragmentView;
     }
     @Override
     public void onListItemClick(ListView l, View v, int position, long id) {
         // on click display the item in toast
          Toast.makeText(getActivity(), (String)l.getItemAtPosition(position), Toast.LENGTH_SHORT).show();
         }
}

list_frag.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="match_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="TextView" />

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true" >

    </ListView>

</RelativeLayout>

snap shot

Edit: if you want the textview to scroll

<?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="match_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="19dp"
        android:text="TextView" />

</RelativeLayout>

Before calling setListAdapter

  View view = inflater.inflate(R.layout.text, null);
  TextView textinlfated = (TextView) view.findViewById(R.id.textView1);
  ListView lv = getListView();
  textinlfated.setText("TextView scrolls");
  lv.addHeaderView(view);

这篇关于把一个TextView在列表片段的顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆