ListView添加ViewPager

引用
  ListView添加ViewPager  listView.addHeaderView(viewPager);

引用
viewpager_layout1.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.support.v4.view.ViewPager
        android:id="@+id/viewpager1"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        />

</LinearLayout>

引用
viewpager_layout2.xml
  下面布局中
android:layout_width="match_parent"
android:layout_height="300dp"
属性无效
必须用代码方式设置宽和高
viewPager.setLayoutParams(new ListView.LayoutParams(
ListView.LayoutParams.MATCH_PARENT, 300)); 设置宽高

引用
viewpager_layout2.xml

<?xml version="1.0" encoding="utf-8"?>

    <android.support.v4.view.ViewPager
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/viewpager1"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        />

import java.util.ArrayList;
import java.util.ArrayList;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.widget.ArrayAdapter;
import android.widget.ListView;
//因为import android.support.v4.app.Fragment;

//所以MainActivity继承自FragmentActivity
public class MainActivity extends FragmentActivity {
	private ArrayList<Fragment> fragmentList;
	private Fragment1 mfragment1;
	private Fragment2 mfragment2;
	private Fragment3 mfragment3;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		// 找到ListView
		ListView listView = (ListView) findViewById(R.id.listView1);
		ArrayList<String> list1 = new ArrayList<String>();
		list1.add("pfg");
		list1.add("124");
		list1.add("234");
		list1.add("456");
		list1.add("567");
		list1.add("567");
		// Fragment数组,为FragmentPagerAdapter准备
		mfragment1 = new Fragment1();
		mfragment2 = new Fragment2();
		mfragment3 = new Fragment3();
		fragmentList = new ArrayList<Fragment>();
		fragmentList.add(mfragment1);
		fragmentList.add(mfragment2);
		fragmentList.add(mfragment3);
		// 找到ViewPager
		ViewPager viewPager = (ViewPager) getLayoutInflater().inflate(
				R.layout.viewpager_layout2, null);
		// 设置 ViewPager的宽和高(重要)
		viewPager.setLayoutParams(new ListView.LayoutParams(
				ListView.LayoutParams.MATCH_PARENT, 300));
		// 设置ViewPager适配器
		viewPager
				.setAdapter(new MyViewPagerAdapter(getSupportFragmentManager()));

		// 布局viewpager_layout1 LinearLayout 中ViewPager
		// View view = getLayoutInflater().inflate(R.layout.viewpager_layout1,
		// null);
		// ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewpager1);

		// viewPager.setLayoutParams(new LinearLayout.LayoutParams(
		// LinearLayout.LayoutParams.MATCH_PARENT, 300));

		viewPager
				.setAdapter(new MyViewPagerAdapter(getSupportFragmentManager()));
		// listView.addHeaderView(view);
		listView.addHeaderView(viewPager);
		listView.setAdapter(new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, list1));

	}

	// ViewPager适配器
	class MyViewPagerAdapter extends FragmentPagerAdapter {

		public MyViewPagerAdapter(FragmentManager fm) {
			super(fm);
			// TODO Auto-generated constructor stub
		}

		@Override
		public Fragment getItem(int arg0) {
			// TODO Auto-generated method stub
			return fragmentList.get(arg0);
		}

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

	}

}

引用
源码地址http://pan.baidu.com/share/link?shareid=1731088090&uk=2099615464

你可能感兴趣的:(ListView,viewpager,addHeaderView)