为了账号安全,请及时绑定邮箱和手机立即绑定

viewpager和Fragment实现tab


按照视频写,最后报错了,找不出bug,求大神解答。


package com.example.bighead_tab3;

import java.util.ArrayList;

import java.util.List;

import android.os.Bundle;

import android.support.v4.app.Fragment;

import android.support.v4.app.FragmentActivity;

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.view.Window;

import android.widget.ImageButton;

import android.widget.LinearLayout;


public class MainActivity extends FragmentActivity implements OnClickListener{

    private ViewPager mViewPager;

    private LinearLayout mTabxuexi;

    private LinearLayout mTabfan;

    private LinearLayout mTabfeed;

    private LinearLayout mTabge;

   

    private ImageButton mxuexiImg;

    private ImageButton mfanImg;

    private ImageButton mfeedImg;

    private ImageButton mgeImg;

    

    private FragmentPagerAdapter mAdadpter;

    private List<Fragment>mFragments;




    @Override

    

    protected void onCreate(Bundle savedInstanceState) {

    requestWindowFeature(Window.FEATURE_NO_TITLE);

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        initView();

        initEvents();

        setSelect(0);

    }




private void initEvents() {

mTabxuexi.setOnClickListener(this);

   mTabfan.setOnClickListener(this);

   mTabfeed.setOnClickListener(this);

   mTabge.setOnClickListener(this);

}




private void initView() {

mViewPager=(ViewPager) findViewById(R.id.id_viewpager);

mTabxuexi=(LinearLayout) findViewById(R.id.id_tab_xuexi);

mTabfan=(LinearLayout) findViewById(R.id.id_tab_fan);

mTabfeed=(LinearLayout) findViewById(R.id.id_tab_feed);

mTabge=(LinearLayout) findViewById(R.id.id_tab_ge);

mxuexiImg=(ImageButton) findViewById(R.id.id_tab_xuexi_img);

mfanImg=(ImageButton) findViewById(R.id.id_tab_fan_img);

mfeedImg=(ImageButton) findViewById(R.id.id_tab_feed_img);

mgeImg=(ImageButton) findViewById(R.id.id_tab_ge_img);

mFragments=new ArrayList<Fragment>();

Fragment mTab01=new XuexiFragment();

Fragment mTab02=new FanFragment();

Fragment mTab03=new FeedFragment();

Fragment mTab04=new GeFragment();

mFragments.add(mTab01);

mFragments.add(mTab02);

mFragments.add(mTab03);

mFragments.add(mTab04);

mAdadpter=new FragmentPagerAdapter(getSupportFragmentManager()) {

@Override

public int getCount() {

return mFragments.size();

}

@Override

public Fragment getItem(int arg0) {

return mFragments.get(arg0=0);

}

};

mViewPager.setAdapter(mAdadpter);

   mViewPager.setOnPageChangeListener(new OnPageChangeListener(){


@Override

public void onPageScrollStateChanged(int arg0) {

}


@Override

public void onPageScrolled(int arg0, float arg1, int arg2) {

}


@Override

public void onPageSelected(int arg0) {

int currentItem =mViewPager.getCurrentItem();

setTab(currentItem);

}

});



}



@Override

public void onClick(View v) {

switch (v.getId()) {

case R.id.id_tab_xuexi:

setSelect(0);

break;

        case R.id.id_tab_fan:

        setSelect(1);

break;

        case R.id.id_tab_feed:

        setSelect(2);

break;

        case R.id.id_tab_ge:

        setSelect(3);

break;

default:

break;

}

}

private void setSelect(int i) {

setTab(i);

mViewPager.setCurrentItem(i);

}






private void setTab(int i) {

resetImg();

switch (i){

case 0:

mxuexiImg.setImageResource(R.drawable.tab_address_pressed);

break;

   case 1:

    mfanImg.setImageResource(R.drawable.tab_find_frd_pressed);

break;

  case 2:

  mfeedImg.setImageResource(R.drawable.tab_settings_pressed);

break;

       case 3:

    mgeImg.setImageResource(R.drawable.ic_launcher);

break;


}

}



private void resetImg() {

mxuexiImg.setImageResource(R.drawable.tab_address_normal);

mfanImg.setImageResource(R.drawable.tab_find_frd_normal);

mfeedImg.setImageResource(R.drawable.tab_settings_normal);

mgeImg.setImageResource(R.drawable.ic_menu_emoticons);

}

}

package com.example.bighead_tab3;



import java.util.ArrayList;

import java.util.List;


import android.os.Bundle;

import android.support.v4.app.Fragment;

import android.support.v4.app.FragmentActivity;

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.view.Window;

import android.widget.ImageButton;

import android.widget.LinearLayout;


public class MainActivity extends FragmentActivity implements OnClickListener{

    private ViewPager mViewPager;

    private LinearLayout mTabxuexi;

    private LinearLayout mTabfan;

    private LinearLayout mTabfeed;

    private LinearLayout mTabge;

   

    private ImageButton mxuexiImg;

    private ImageButton mfanImg;

    private ImageButton mfeedImg;

    private ImageButton mgeImg;

    

    private FragmentPagerAdapter mAdadpter;

    private List<Fragment>mFragments;




    @Override

    

    protected void onCreate(Bundle savedInstanceState) {

    requestWindowFeature(Window.FEATURE_NO_TITLE);

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        initView();

        initEvents();

        setSelect(0);

    }




private void initEvents() {

mTabxuexi.setOnClickListener(this);

   mTabfan.setOnClickListener(this);

   mTabfeed.setOnClickListener(this);

   mTabge.setOnClickListener(this);

}




private void initView() {

mViewPager=(ViewPager) findViewById(R.id.id_viewpager);

mTabxuexi=(LinearLayout) findViewById(R.id.id_tab_xuexi);

mTabfan=(LinearLayout) findViewById(R.id.id_tab_fan);

mTabfeed=(LinearLayout) findViewById(R.id.id_tab_feed);

mTabge=(LinearLayout) findViewById(R.id.id_tab_ge);

mxuexiImg=(ImageButton) findViewById(R.id.id_tab_xuexi_img);

mfanImg=(ImageButton) findViewById(R.id.id_tab_fan_img);

mfeedImg=(ImageButton) findViewById(R.id.id_tab_feed_img);

mgeImg=(ImageButton) findViewById(R.id.id_tab_ge_img);

mFragments=new ArrayList<Fragment>();

Fragment mTab01=new XuexiFragment();

Fragment mTab02=new FanFragment();

Fragment mTab03=new FeedFragment();

Fragment mTab04=new GeFragment();

mFragments.add(mTab01);

mFragments.add(mTab02);

mFragments.add(mTab03);

mFragments.add(mTab04);

mAdadpter=new FragmentPagerAdapter(getSupportFragmentManager()) {

@Override

public int getCount() {

return mFragments.size();

}

@Override

public Fragment getItem(int arg0) {

return mFragments.get(arg0=0);

}

};

mViewPager.setAdapter(mAdadpter);

   mViewPager.setOnPageChangeListener(new OnPageChangeListener(){


@Override

public void onPageScrollStateChanged(int arg0) {

}


@Override

public void onPageScrolled(int arg0, float arg1, int arg2) {

}


@Override

public void onPageSelected(int arg0) {

int currentItem =mViewPager.getCurrentItem();

setTab(currentItem);

}

});



}



@Override

public void onClick(View v) {

switch (v.getId()) {

case R.id.id_tab_xuexi:

setSelect(0);

break;

        case R.id.id_tab_fan:

        setSelect(1);

break;

        case R.id.id_tab_feed:

        setSelect(2);

break;

        case R.id.id_tab_ge:

        setSelect(3);

break;

default:

break;

}

}

private void setSelect(int i) {

setTab(i);

mViewPager.setCurrentItem(i);

}






private void setTab(int i) {

resetImg();

switch (i){

case 0:

mxuexiImg.setImageResource(R.drawable.tab_address_pressed);

break;

   case 1:

    mfanImg.setImageResource(R.drawable.tab_find_frd_pressed);

break;

  case 2:

  mfeedImg.setImageResource(R.drawable.tab_settings_pressed);

break;

       case 3:

    mgeImg.setImageResource(R.drawable.ic_launcher);

break;


}

}



private void resetImg() {

mxuexiImg.setImageResource(R.drawable.tab_address_normal);

mfanImg.setImageResource(R.drawable.tab_find_frd_normal);

mfeedImg.setImageResource(R.drawable.tab_settings_normal);

mgeImg.setImageResource(R.drawable.ic_menu_emoticons);

}

}


正在回答

0 回答

举报

0/150
提交
取消
多种多样的App主界面Tab实现方法
  • 参与学习       48925    人
  • 解答问题       179    个

本课程就将讲解迄今为止最流行的4种实现Tab的方法

进入课程

viewpager和Fragment实现tab

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信