仿QQ界面切换简单实现-ViewPager、Fragment

效果图:


(1)activity_main.xml文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/LinearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#297DC6"
        android:orientation="horizontal"
        android:paddingBottom="8dp"
        android:paddingTop="8dp" >

        <TextView
            android:id="@+id/tv_1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:onClick="click"
            android:text="动态"
            android:textColor="#fff"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/tv_2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:onClick="click"
            android:text="群组"
            android:textColor="#fff"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/tv_3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:onClick="click"
            android:text="好友"
            android:textColor="#fff"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/tv_4"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:onClick="click"
            android:text="会话"
            android:textColor="#fff"
            android:textSize="18sp" />
    </LinearLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/ViewPager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>
(2)fragment_a.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/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

(3)MainActivity.java文件

</pre><pre name="code" class="java">package com.example.android21_viewpagerfragment2;

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

import android.graphics.Color;
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.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends FragmentActivity {
	private final static int LIST_NUM = 4;

	private ViewPager viewPager;
	private LinearLayout linearLayout;
	private TextView[] array_view = new TextView[LIST_NUM];
	private List<Fragment> list_fragment = new ArrayList<Fragment>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		viewPager = (ViewPager) findViewById(R.id.ViewPager);
		linearLayout = (LinearLayout) findViewById(R.id.LinearLayout);
		init();

		FragmentManager fm = getSupportFragmentManager();
		viewPager.setAdapter(new MyAdapter(fm));
		viewPager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int arg0) {
				for (int i = 0; i < array_view.length; i++) {
					array_view[i].setTextColor(Color.WHITE);
				}
				array_view[arg0].setTextColor(Color.RED);
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
				switch (arg0) {
				case ViewPager.SCROLL_STATE_SETTLING:

					break;

				default:
					break;
				}

			}
		});
	}

	public class MyAdapter extends FragmentPagerAdapter {

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

		@Override
		public Fragment getItem(int arg0) {
			return list_fragment.get(arg0);
		}

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

	private void init() {
		for (int i = 0; i < LIST_NUM; i++) {
			TextView tv = (TextView) linearLayout.getChildAt(i);
			tv.setTag(i);
			tv.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					viewPager.setCurrentItem((Integer) v.getTag());
				}
			});
			array_view[i] = tv;
		}
<span style="white-space:pre">		</span>array_view[0].setTextColor(Color.RED);
		String[] data = new String[] { "动态", "群组", "好友", "会话" };
		for (int i = 0; i < LIST_NUM; i++) {
			Fragment_A fA = new Fragment_A();
			Bundle bundle = new Bundle();
			bundle.putString("name", data[i]);
			fA.setArguments(bundle);
			list_fragment.add(fA);
		}

	}
}
(4)Fragment_A.java文件

package com.example.android21_viewpagerfragment2;

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

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.ArrayAdapter;
import android.widget.ListView;

public class Fragment_A extends Fragment {
	private ListView listView;

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

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_a, null);
		listView = (ListView) view.findViewById(R.id.listView1);
		Bundle bundle = getArguments();
		String name = bundle.getString("name");
		List<String> list = new ArrayList<String>();
		for (int i = 0; i < 20; i++) {
			list.add(name + " : " + i);
		}

		ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
				android.R.layout.simple_list_item_1, list);
		listView.setAdapter(adapter);
		return view;
	}
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值