FragmentTabHost与fragment的使用(设置标题)

有别与前面的是这个标题不会滚动

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.widget.TabHost.TabSpec;

public class MainActivity extends FragmentActivity {

    private FragmentTabHost tabHost;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 获得tabhost
        tabHost = (FragmentTabHost) findViewById(R.id.fth_tabHost);
        // 关联fragment的容器id和fragmentmanger
        // Context context,
        // FragmentManager manager,//管理fragment的fragmentmanger
        // int containerId //存放fragment的容器
        tabHost.setup(MainActivity.this, getSupportFragmentManager(),R.id.fl_fragment);
        for (int i = 0; i < 3; i++) {
            TabSpec tabSpec = tabHost.newTabSpec("收藏" + i).setIndicator("收藏" + i);
            // 将要显示的fragment与tabhost关联
            // TabSpec tabSpec,
            // Class<?> clss, 传入Fragment的class对象
            // Bundle args 传入fragment中的bundle参数, 相当于 setArguments();
            Bundle bundle = new Bundle();
            bundle.putInt("INDEX", i);
            tabHost.addTab(tabSpec, FragmentOne.class, bundle);
        }
    }
}

碎片类 FragmentOne.java

import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class FragmentOne extends Fragment {

    private List<List<Person>> dataList;
    private List<Person> list;
    private Context context;
    private LayoutInflater inflater;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initData();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        this.context = getActivity();
        this.inflater = inflater;

        View view = inflater.inflate(R.layout.fragment_one, null);

        ListView lvItems = (ListView) view.findViewById(R.id.lv_items);
        // 获得activity传入的参数,根据参数再请求网络数据

        Bundle bundle = getArguments();
        int pagerIndex = bundle.getInt("INDEX");
        list = dataList.get(pagerIndex);

        lvItems.setAdapter(new ItemBaseAdapter());

        return view;

    }
    //模拟数据
    private void initData() {
        dataList = new ArrayList<List<Person>>();
        for (int i = 0; i < 3; i++) {
            ArrayList<Person> arrayList = new ArrayList<Person>();
            for (int j = 0; j < 30; j++) {
                arrayList.add(new Person(i + "王宝强" + j, "1378273" + j));
            }
            dataList.add(arrayList);
        }
    }

    class ItemBaseAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = inflater.inflate(R.layout.listview_item, null);
            TextView tvName = (TextView) view.findViewById(R.id.tv_name);
            TextView tvNumber = (TextView) view.findViewById(R.id.tv_number);
            Person person = list.get(position);
            tvName.setText(person.getName());
            tvNumber.setText(person.getNumber());
            return view;
        }
    }
}

Person.java为listview的item数据实体类

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.support.v4.app.FragmentTabHost
        android:id="@+id/fth_tabHost"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </android.support.v4.app.FragmentTabHost>

    <FrameLayout
        android:id="@+id/fl_fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </FrameLayout>

</RelativeLayout>

碎片显示的布局文件fragment_one.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" >

    <ListView
        android:id="@+id/lv_items"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </ListView>

</LinearLayout>

listview_item.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" >

    <TextView
        android:id="@+id/tv_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/tv_number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#ff3333" />

</LinearLayout>

效果:

 

posted @ 2016-08-18 15:52  ts-android  阅读(847)  评论(0编辑  收藏  举报