Button+Fragment+Viewpager

来源:互联网 发布:网络新闻报道的特点 编辑:程序博客网 时间:2024/04/27 20:01

主界面===========================================================================

public class MainActivity extends FragmentActivity implements OnClickListener{


    private Button btn1,btn2,btn3;
    private F1 f1;
    private F2 f2;
    private F3 f3;
    private FragmentManager fm;
    private FragmentTransaction btc;
    
    private ViewPager vp;
    List<Fragment> list;    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        initView();
        
        vp=(ViewPager) findViewById(R.id.vp);
        
        fm=getSupportFragmentManager();
        btc=fm.beginTransaction();
        
        f1=new F1();
        f2=new F2();
        f3=new F3();
        
        btc.add(R.id.fly, f1).add(R.id.fly, f2).add(R.id.fly, f3).hide(f2).hide(f3);
        btc.commit();
        
        
       // vp.setAdapter(new MyFragMentPagetAdapter(fm,getFragment()));
        
       
       }

    
   /* private List<Fragment> getFragment() {
        // TODO Auto-generated method stub
        list=new ArrayList<Fragment>();
        
        list.add(new F1());
        list.add(new F2());
        list.add(new F3());
        
        
        return list;
    }*/
    
       private void initView() {
        // TODO Auto-generated method stub
        btn1=(Button) findViewById(R.id.btn1);
        btn2=(Button) findViewById(R.id.btn2);
        btn3=(Button) findViewById(R.id.btn3);
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        
        
    }

    

    @Override
    public void onClick(View v) {
        switch(v.getId()){
        case R.id.btn1:
            btc=fm.beginTransaction();
            btc.hide(f2).hide(f3).show(f1).commit();
            break;
        case R.id.btn2:
            btc=fm.beginTransaction();
            btc.hide(f1).hide(f3).show(f2).commit();
            break;
        case R.id.btn3:
            btc=fm.beginTransaction();
            btc.hide(f1).hide(f2).show(f3).commit();
            break;
        }
    }
    


   

}


主界面布局=======================================================

<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"
    tools:context="com.bwie.test.MainActivity" >

    <FrameLayout
        android:id="@+id/fly"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        ></FrameLayout>
    <android.support.v4.view.ViewPager
        android:id="@+id/vp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
    
    
    <LinearLayout
        android:id="@+id/lin"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"
        >
        <Button
            android:id="@+id/btn1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/selecter_text_color"
            android:text="动弹"
            />
        <Button
            android:id="@+id/btn2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
          android:background="@drawable/selecter_text_color"
            android:text="博客"
            />
        <Button
            android:id="@+id/btn3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            
         android:background="@drawable/selecter_text_color"
            android:text="推荐"
            />
    </LinearLayout>
    

</RelativeLayout>

PagerAdapter适配界面=================================================

package com.bwie.test;

import java.util.List;

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

public class MyFragMentPagetAdapter extends FragmentPagerAdapter {

    List<Fragment>list;
    public MyFragMentPagetAdapter(FragmentManager fm, List<Fragment> list) {
        super(fm);
        this.list=list;
    }

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

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

}

背景选择器====================================================

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false">
    <shape>
        <solid android:color="#787878"></solid>
    </shape>
</item>
<item android:state_checked="true">
    <shape>
        <solid android:color="#ff00"></solid>
    </shape>
</item>

</selector>


0 0