android - 将 Textview 放在列表 fragment 之上

标签 android android-fragments textview android-listfragment

如何在 fragment 之上添加 TextView 。我正在使用以下适配器来填充列表 fragment ,而 xml 文件是列表 fragment 中的每一行。我需要在 ListView 的顶部添加一个 TextView ,它必须可以与列表一起滚动?

     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>

列表 fragment :

    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      
        }
    }
}

AsyncTask 类中使用以下代码来填充列表 fragment

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

最佳答案

您可以按照以下操作或将 TextView 作为标题添加到您的列表中

引用文档

ListActivity(ListFragment 类似)有一个默认布局,它由屏幕中央的单个全屏列表组成。但是,如果需要,您可以通过在 onCreate() 中使用 setContentView() 设置您自己的 View 布局来自定义屏幕布局。为此,您自己的 View 必须包含一个 ListView` 对象,其 id 为 "@android:id/list"(如果在代码中则为 list)

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>

快照

enter image description here

编辑:如果你想让textview滚动

<?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>

在调用 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);

关于android - 将 Textview 放在列表 fragment 之上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18781843/

相关文章:

android - 不同 Activity fragment 之间的共享元素转换

android - 如何将 float 操作按钮拖到 View 寻呼机上

android - 如何使表格行适合android中的屏幕宽度

android - 无法将 LocalBroadcastManager 添加到 Android Studio 中的项目

android - 如何在android中获取DialogResult?

Android fragment 选项卡示例

android - 5.1.1 通知TextView,处理白色文本的正确方式?

android - AsyncLayoutInflator 陷阱

java - 在 Android 中使用 SharedPreferences 存储数据

android - 如何在 android TextView 中显示 html 特殊字符