布局页面
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:layout_weight="1"
android:id="@+id/viewpager"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:text="TextView" />
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/radio0"
android:layout_width="0dp"
android:layout_height="match_parent"
android:checked="true"
android:layout_weight="1"
android:gravity="center"
android:button="@null"
android:text="首页" />
<RadioButton
android:id="@+id/radio1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:button="@null"
android:text="分类" />
<RadioButton
android:id="@+id/radio2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:button="@null"
android:text="我的" />
</RadioGroup>
</LinearLayout>
MainActivity界面
public class MainActivity extends FragmentActivity {
private ViewPager vPager;
private RadioGroup rGroup;
private RadioButton rdio0;
private RadioButton rdio1;
private RadioButton rdio2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取控件
rGroup = (RadioGroup) findViewById(R.id.radioGroup1);
rdio0 = (RadioButton) findViewById(R.id.radio0);
rdio1 = (RadioButton) findViewById(R.id.radio1);
rdio2 = (RadioButton) findViewById(R.id.radio2);
vPager = (ViewPager) findViewById(R.id.viewpager);
MyfPAdapter adapter= new MyfPAdapter(getSupportFragmentManager());
vPager.setAdapter(adapter);
// 默认背景颜色
rdio0.setBackgroundColor(Color.RED);
rdio1.setBackgroundColor(Color.BLUE);
rdio2.setBackgroundColor(Color.BLUE);
// 默认选择颜色
vPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
switch (arg0) {
case 0:
rdio0.setBackgroundColor(Color.RED);
rdio1.setBackgroundColor(Color.BLUE);
rdio2.setBackgroundColor(Color.BLUE);
break;
case 1:
rdio1.setBackgroundColor(Color.RED);
rdio0.setBackgroundColor(Color.BLUE);
rdio2.setBackgroundColor(Color.BLUE);
break;
case 2:
rdio2.setBackgroundColor(Color.RED);
rdio1.setBackgroundColor(Color.BLUE);
rdio0.setBackgroundColor(Color.BLUE);
break;
default:
break;
}
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
// 点击按钮切换fragment页面
rGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
switch (checkedId) {
case R.id.radio0:
vPager.setCurrentItem(0);
break;
case R.id.radio1:
vPager.setCurrentItem(1);
break;
case R.id.radio2:
vPager.setCurrentItem(2);
break;
default:
break;
}
}
});
}
}
fragment传值:
fragment1:
lv.setOnItemClickListener(new OnItemClickListener() {
private TextView text;
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
// 调用公共方法传值
Fragment_textview fragment_t = (Fragment_textview) getActivity()
.getSupportFragmentManager().findFragmentById(
R.id.framelayout2);
fragment_t.setmsg(list.get(position));
// bytag传值
// FragmentActivity activity = getActivity();
// text
// =(TextView)activity.getSupportFragmentManager().findFragmentByTag("2").getView().findViewById(R.id.f2_textView1);
// text.setText(list.get(position));
}
});
fragment2:
// 修改值方法
public void setmsg(String msg) {
text.setText(msg);
}
轮播页面:
public class Fragment3 extends Fragment {
private View view;
private ViewPager vpager;
// 轮播自动
Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
if (msg.what == 1) {
// 获取当前页面
int item = vpager.getCurrentItem();
item++;
// 设置页面
vpager.setCurrentItem(item);
handler.sendEmptyMessageDelayed(1, 1500);
}
};
};
private LinearLayout linearlyout;
private ImageView imageView;
private LayoutParams params;
private List<Integer> list;
private List<ImageView> list_imag;
/*
* (non-Javadoc)
*
* @see
* android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater,
* android.view.ViewGroup, android.os.Bundle)
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_3, container, false);
// 图片集合、不能放全局
list = new ArrayList<Integer>();
// 小点图像集合
list_imag = new ArrayList<ImageView>();
vpager = (ViewPager) view.findViewById(R.id.f3_viewpager);
// 线性布局
linearlyout = (LinearLayout) view.findViewById(R.id.f3_linearlyout);
return view;
}
/*
* (non-Javadoc)
*
* @see android.support.v4.app.Fragment#onActivityCreated(android.os.Bundle)
*/
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
// 添加数据
list.add(R.drawable.mm);
list.add(R.drawable.mm1);
list.add(R.drawable.mm2);
list.add(R.drawable.mm3);
MypagerAdapter adapter = new MypagerAdapter(list, getActivity());
vpager.setAdapter(adapter);
//设置最大值
vpager.setCurrentItem(list.size() * 1000);
// 监听vpager
vpager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
// 改变小点颜色
for (int i = 0; i < list_imag.size(); i++) {
if (arg0 % list_imag.size() == i) {
list_imag.get(i).setImageResource(R.drawable.shape_yes);
} else {
list_imag.get(i).setImageResource(R.drawable.shape_no);
}
}
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
handler.sendEmptyMessageDelayed(1, 1500);
// 置空圆点集合 必须
if (list_imag != null) {
list_imag.clear();
// 小圆点动态添加
for (int i = 0; i < list.size(); i++) {
imageView = new ImageView(getActivity());
// 设置颜色
if (i == 0) {
imageView.setImageResource(R.drawable.shape_yes);
} else {
imageView.setImageResource(R.drawable.shape_no);
}
// 小点的宽高
params = new LayoutParams(15, 15);
// 小点间距
params.setMargins(10, 0, 10, 20);
list_imag.add(imageView);
// 加入到线性布局
linearlyout.addView(imageView, params);
}
}
}
/*
* (non-Javadoc)
*
* @see android.support.v4.app.Fragment#onPause() 失去焦点时调用
*/
@Override
public void onPause() {
// TODO Auto-generated method stub
// 把handler里面的消息清空
handler.removeCallbacksAndMessages(null);
super.onPause();
}
}
适配器:
public class MypagerAdapter extends PagerAdapter {
List<Integer> list;
Context ctx;
public MypagerAdapter(List<Integer> list, Context ctx) {
super();
this.list = list;
this.ctx = ctx;
}
/* (non-Javadoc)
* @see android.support.v4.view.PagerAdapter#getCount()
*/
@Override
public int getCount() {
// TODO Auto-generated method stub
// 设置最大长度
return Integer.MAX_VALUE;
}
/* (non-Javadoc)
* @see android.support.v4.view.PagerAdapter#isViewFromObject(android.view.View, java.lang.Object)
*/
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0==arg1;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// TODO Auto-generated method stub
container.removeView((View) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
// TODO Auto-generated method stub
ImageView imageView = new ImageView(ctx);
// 磨除集合长度
imageView.setImageResource(list.get(position%list.size()));
// 图片填充拉伸
imageView.setScaleType(ScaleType.FIT_XY);
container.addView(imageView);
return imageView;
}
}
轮播图片加载网络图片
List<String> imag_list = new ArrayList<>();
String rul1="http://pic8.nipic.com/20100701/5290458_114840036316_2.jpg";
String rul2="http://pic2.nipic.com/20090424/1468853_230119053_2.jpg";
String rul3="http://img3.3lian.com/2013/s1/20/d/57.jpg";
imag_list.add(rul1);
imag_list.add(rul2);
imag_list.add(rul3);
MyPagerAdapter vpadapter = new MyPagerAdapter(imag_list,this);
vper.setAdapter(vpadapter);
vper.setCurrentItem(10000);
// 发送handler延迟空消息
sendMsg();
适配器中instantiateItem方法
@Override
public Object instantiateItem(ViewGroup container, int position) {
imageView = new ImageView(context);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
ImageLoader.getInstance().displayImage(imag_list.get(position%imag_list.size()), imageView);
container.addView(imageView);
//跳转webview页面
dianji();
return imageView;
}
private void dianji() {
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(context, Main2Activity.class);
context.startActivity(intent);
}
});
}
web页面
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
initView();
// 允许写入JS代码
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl("https://www.baidu.com");
}
@Override
protected void onPause() {
super.onPause();
finish();
}
private void initView() {
web = (WebView) findViewById(R.id.web);
}
}