博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
微信实例_Fragment and viewPager
阅读量:6819 次
发布时间:2019-06-26

本文共 7175 字,大约阅读时间需要 23 分钟。

一、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);

转载于:https://www.cnblogs.com/324sige/p/5712205.html

你可能感兴趣的文章
iOS正则表达式
查看>>
关于javascript的this指向问题
查看>>
Promise的理解和用法
查看>>
java B2B2C Springboot电子商城系统-高可用的服务注册中心
查看>>
Dubbo的总体架构
查看>>
Spring Cloud微服务架构代码结构详细讲解
查看>>
以太经典硬分叉:矿工欢喜、投资者欢庆、社区高兴的“三赢”之举
查看>>
我的友情链接
查看>>
LVS启(禁)用成员
查看>>
innobackupex 备份报错
查看>>
2016 IT 运维工作计划及学习
查看>>
将一个数的二进制位模式从左到右翻转并输出
查看>>
jQuery学习之jQuery Ajax用法详解
查看>>
关于JEPLUS软件介绍——JEPLUS软件快速开发平台
查看>>
动态增加UIView到当前视图中
查看>>
怎么能看透信封
查看>>
css正方形照片墙
查看>>
找工作的程序员必懂的Linux
查看>>
shell脚本实现杨辉三角形
查看>>
ComponentOne 2019V1火热来袭!全面支持 Visual Studio 2019
查看>>