一、Fragment
注意:
1、要使用V4包
2、Fragment类中
inflater.inflate(R.layout.tab01,container,false)
* @param inflater The LayoutInflater object that can be used to inflate * any views in the fragment, * @param container If non-null, this is the parent view that the fragment's * UI should be attached to. The fragment should not add the view itself, * but this can be used to generate the LayoutParams of the view. * @param savedInstanceState If non-null, this fragment is being re-constructed * from a previous saved state as given here. *
如果最后没有False,会出错
The specified child already has a parent. You must call removeView() on the child's parent first.
MainActivity
package com.example.mysecond;import android.os.Bundle;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentManager;import android.support.v4.app.FragmentTransaction;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.ImageButton;public class MainActivity extends AppCompatActivity { protected Fragment weixin,friend,address,setting; protected ImageButton weixinButton1,friendButton2,addressButton3,settingButton4; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } protected void init(){ //为每个按钮添加监听器 weixinButton1= (ImageButton) findViewById(R.id.id_tab_weixin_img); friendButton2= (ImageButton) findViewById(R.id.id_tab_frd_img); addressButton3= (ImageButton) findViewById(R.id.id_tab_address_img); settingButton4= (ImageButton) findViewById(R.id.id_tab_settings_img); weixinButton1.setOnClickListener(new MyButtonListener()); friendButton2.setOnClickListener(new MyButtonListener()); addressButton3.setOnClickListener(new MyButtonListener()); settingButton4.setOnClickListener(new MyButtonListener()); weixin=new weixin(); friend=new friend(); address=new address(); setting=new Setting(); FragmentManager fragmentManager=getSupportFragmentManager(); FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction(); fragmentTransaction.add(R.id.MyFragment,weixin); fragmentTransaction.add(R.id.MyFragment,friend); fragmentTransaction.add(R.id.MyFragment,address); fragmentTransaction.add(R.id.MyFragment,setting); hideFragment(fragmentTransaction); fragmentTransaction.show(weixin); fragmentTransaction.commit(); weixinButton1.setImageResource(R.drawable.tab_weixin_pressed); } //重新设置ImageButton为不亮 protected void resetImg(){ weixinButton1.setImageResource(R.drawable.tab_weixin_normal); friendButton2.setImageResource(R.drawable.tab_find_frd_normal); addressButton3.setImageResource(R.drawable.tab_address_normal); settingButton4.setImageResource(R.drawable.tab_settings_normal); } //隐藏所有Fragment protected void hideFragment( FragmentTransaction fragmentTransaction){ if(weixin!=null){ fragmentTransaction.hide(weixin); } if(friend!=null){ fragmentTransaction.hide(friend); } if(address!=null){ fragmentTransaction.hide(address); } if(setting!=null){ fragmentTransaction.hide(setting); } } class MyButtonListener implements View.OnClickListener{ @Override public void onClick(View v) { resetImg(); FragmentManager fragmentManager=getSupportFragmentManager(); FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction(); hideFragment(fragmentTransaction); switch (v.getId()) { case R.id.id_tab_weixin_img: { if(weixin==null){ weixin=new weixin(); fragmentTransaction.add(R.id.MyFragment,weixin); } fragmentTransaction.show(weixin); weixinButton1.setImageResource(R.drawable.tab_weixin_pressed); break; } case R.id.id_tab_frd_img: { if(friend==null){ friend=new friend(); fragmentTransaction.add(R.id.MyFragment,friend); } fragmentTransaction.show(friend); friendButton2.setImageResource(R.drawable.tab_find_frd_pressed); break; } case R.id.id_tab_address_img: { if(address==null){ address=new address(); fragmentTransaction.add(R.id.MyFragment,address); } fragmentTransaction.show(address); addressButton3.setImageResource(R.drawable.tab_address_pressed); break; } case R.id.id_tab_settings_img: { if(setting==null){ setting=new Setting(); fragmentTransaction.add(R.id.MyFragment,setting); } fragmentTransaction.show(setting); settingButton4.setImageResource(R.drawable.tab_settings_pressed); break; } } fragmentTransaction.commit(); } }}
Activity_main
Fragment
public class weixin extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.tab01,container,false); } }
二、viewpager
private void initGuide() { vp_guide = (ViewPager) findViewById(R.id.vp_guide); //初始化数据 listView = new ArrayList(); int imageViewId[]=new int[]{R.mipmap.guide_1,R.mipmap.guide_2,R.mipmap.guide_3}; for (int ivId:imageViewId){ ImageView imageView=new ImageView(this); imageView.setBackgroundResource(ivId); listView.add(imageView); } vp_guide.setAdapter(new PagerAdapter() { @Override public Object instantiateItem(ViewGroup container, int position) { container.addView(listView.get(position)); return listView.get(position); } @Override public void destroyItem(ViewGroup container, int position, Object object) { //container.removeView(listView.get(position)); container.removeView((View)object); } @Override public int getCount() { return listView.size(); } @Override public boolean isViewFromObject(View view, Object object) { return view==object; } }); }
//container.removeView(listView.get(position)); container.removeView((View)object);