viewpager+listview+fragment实现简单的滑动标签页

在avtivity_main.xml中实现主布局

<?xml version="1.0" encoding="utf-8"?>
<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/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="100.0dip"
        android:background="#A4D3EE" >

        <TextView
            android:id="@+id/text1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1.0"
            android:gravity="center"
            android:text="页面一"
            android:textColor="#ffffff"
            android:textSize="22.0dip" />

        <TextView
            android:id="@+id/text2"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1.0"
            android:gravity="center"
            android:text="页面二"
            android:textColor="#ffffff"
            android:textSize="22.0dip" />

        <TextView
            android:id="@+id/text3"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1.0"
            android:gravity="center"
            android:text="页面三"
            android:textColor="#ffffff"
            android:textSize="22.0dip" />
    </LinearLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/vPager"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1.0"
        android:background="#ffffff"
        android:flipInterval="30"
        android:persistentDrawingCache="animation" />

</LinearLayout>

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/lv1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></ListView>

</LinearLayout>

listitem.xml中是listview的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/img"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />

        <TextView
            android:id="@+id/tv"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:text="id"
            android:textSize="20sp"/>
        <TextView
            android:id="@+id/info"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:text="info"
            android:textSize="20sp"/>


    </LinearLayout>

</LinearLayout>

ListViewAdapter.java

package com.example.viewpager;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.Objects;

public class ListViewAdapter extends BaseAdapter{
    private LayoutInflater mInflater=null;
    public ListViewAdapter(Context menuActivity){
        mInflater=LayoutInflater.from(menuActivity);
    }
    public int getCount(){
        return 5;
    }
    public Objects getItem(int arg0){
        return null;
    }
    public long getItemId(int arg0){
        return arg0;
    }
    public View getView(int arg0,View arg1,ViewGroup arg2){
        int i=arg0+1;
        ViewHolder holder;
        if(arg1==null){
            arg1=mInflater.inflate(R.layout.listitem,null);
            holder=new ViewHolder();
            holder.img=(ImageView)arg1.findViewById(R.id.img);
            holder.title=(TextView)arg1.findViewById(R.id.tv);
            holder.info=(TextView)arg1.findViewById(R.id.info);
            arg1.setTag(holder);
        }
        else{
            holder=(ViewHolder) arg1.getTag();
        }

        holder.img.setImageResource(R.mipmap .ic_launcher);
        holder.title.setText("第"+i+"行");
        holder.info.setText("2015-10-31");
        return arg1;
    }
    public class ViewHolder{
        public ImageView img;
        public TextView title;
        public TextView info;
    }
}

ChoiceFragment.java

package com.example.viewpager;

import android.annotation.SuppressLint;
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.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;


@SuppressLint("ValidFragment")
public class ChoiceFragment extends Fragment {

    private Context context=null;
    private ListView lv;

    public ChoiceFragment (Context context){
        this.context=context;
    }

    public ChoiceFragment(){
    }

    @Override
    public View onCreateView(LayoutInflater inflater ,ViewGroup container ,Bundle savedInstanceState){
        View view =inflater .inflate(R.layout.one, container,false);
        lv =(ListView)view.findViewById(R.id.lv1);
        ListViewAdapter adapter = new ListViewAdapter(context);
        lv.setAdapter(adapter);

        lv.setOnItemClickListener(new OnItemClickListener(){

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {
                // TODO Auto-generated method stub
                TextView v=(TextView) arg1.findViewById(R.id.tv);
                Toast.makeText(context, v.getText().toString(), Toast.LENGTH_SHORT).show();
            }

        });
        return view;
    }


}

MyFragmentPagerAdapter.java

package com.example.viewpager;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import java.util.ArrayList;

public class MyFragmentPagerAdapter extends FragmentPagerAdapter {
    ArrayList<Fragment> list;
    public MyFragmentPagerAdapter(FragmentManager fm,ArrayList<Fragment> list) {
        super(fm);
        this.list = list;

    }

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

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

}

MainActivity.java

package com.example.viewpager;

import java.util.ArrayList;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends FragmentActivity {
    private ViewPager mPager;
    private ArrayList<Fragment> fragmentList;
    private TextView view1, view2, view3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        InitTextView();
        InitViewPager();
    }

    // 初始化标签名
    public void InitTextView(){
        view1 = (TextView)findViewById(R.id.text1);
        view2 = (TextView)findViewById(R.id.text2);
        view3 = (TextView)findViewById(R.id.text3);

        view1.setOnClickListener(new txListener(0));
        view2.setOnClickListener(new txListener(1));
        view3.setOnClickListener(new txListener(2));
    }

    public class txListener implements View.OnClickListener{
        private int index=0;

        public txListener(int i) {
            index =i;
        }
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            mPager.setCurrentItem(index);
        }
    }


    // 初始化ViewPager
    public void InitViewPager(){
        mPager = (ViewPager)findViewById(R.id.vPager);
        fragmentList = new ArrayList<Fragment>();
        fragmentList.add(new ChoiceFragment(this));
        fragmentList.add(new ChoiceFragment(this));
        fragmentList.add(new ChoiceFragment(this));
        view1.setTextColor(Color.BLACK);

        //给ViewPager设置适配器
        mPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), fragmentList));
        mPager.setCurrentItem(0);//设置当前显示标签页为第一页
        mPager.setOnPageChangeListener(new MyOnPageChangeListener());//页面变化时的监听器
    }

    // 页卡切换监听
    public class MyOnPageChangeListener implements OnPageChangeListener{

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

        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onPageSelected(int arg0) {
            // TODO Auto-generated method stub
            if(arg0==0){
                view1.setTextColor(Color.BLACK);
                view2.setTextColor(Color.WHITE);
                view3.setTextColor(Color.WHITE);
            }
            else if(arg0==1){
                view1.setTextColor(Color.WHITE);
                view2.setTextColor(Color.BLACK);
                view3.setTextColor(Color.WHITE);

            }
            else if(arg0==2){
                view1.setTextColor(Color.WHITE);
                view2.setTextColor(Color.WHITE);
                view3.setTextColor(Color.BLACK);
            }
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值