Android中fragment+viewpager实现布局
本文实例为大家分享了Android九宫格图片展示的具体代码,供大家参考,具体内容如下
永定ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为成都创新互联公司的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:028-86922220(备注:SSL证书合作)期待与您的合作!
1.先布局实现mian.xml
<?xml version="1.0" encoding="utf-8"?>
2.创建3个fragment 要继承Fragment类v4包下的
public class FragmentThree extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { //引入布局文件 View view = inflater.inflate(R.layout.fragmentthree, null); return view; } }
3.创建fragment 相对应的布局文件
<?xml version="1.0" encoding="utf-8"?>
4.创建适配器继承FragmentPagerAdapter
package com.bwei.fragment; import android.content.Context; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import java.util.List; public class MyAdapter extends FragmentPagerAdapter { private Listfragments; private Context context; //构造方法 public MyAdapter(FragmentManager fm, List fragments, Context context) { super(fm); this.fragments = fragments; this.context = context; } //得到item条目 @Override public Fragment getItem(int position) { return fragments.get(position); } //得到数量 @Override public int getCount() { return fragments.size(); } }
5.在mainActivity实现效果
package com.bwei.fragment; import android.graphics.Color; import android.os.Bundle; import android.support.annotation.IdRes; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.Toast; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener,ViewPager.OnPageChangeListener { private ViewPager vPager; private Listfragments; private FragmentManager fm; private RadioGroup mRadioGroup; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化控件 initView(); initDate(); } private void initView() { vPager=(ViewPager) findViewById(R.id.pager); vPager.setOnPageChangeListener(this); mRadioGroup=(RadioGroup) findViewById(R.id.radioGroup1); mRadioGroup.setOnCheckedChangeListener(this); } private void initDate() { fragments=new ArrayList (); //实例化Fragment FragmentOne fragmentOne = new FragmentOne(); FragmentTwo fragmentTwo = new FragmentTwo(); FragmentThree fragmentThree = new FragmentThree(); //添加到集合 fragments.add(fragmentOne); fragments.add(fragmentTwo); fragments.add(fragmentThree); //得到getSupportFragmentManager()的管理器 fm = getSupportFragmentManager(); //得到适配器 MyAdapter myAdapter = new MyAdapter(fm, fragments, this); //设置适配器 vPager.setAdapter(myAdapter); } //ViewPager.OnPageChangeListener监听事件 @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { for (int i = 0; i
6.最后的效果图
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。
分享文章:Android中fragment+viewpager实现布局
标题URL:http://lswzjz.com/article/ppogie.html