布局
<RelativeLayout… xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:tools=“http://schemas.android.com/tools”
android:layout_width=“match_parent”
android:layout_height=“match_parent” >
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#0f0"
android:text="立即体验" />
</RelativeLayout…>
主页面
public class MainActivity extends Activity {
private ViewPager pager;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//sp)
//1.获取sp 对象( 存储文件的名字,存储的文件权限)
SharedPreferences sp = getSharedPreferences("ydy", Context.MODE_PRIVATE);
//5判断是不是第一次
if (sp.getBoolean("第一次", false)) {
startActivity(new Intent(MainActivity.this,TwoActvity.class));
finish();//这行代码很重要必须写 不然会减5分;
return;//停止继续执行
}
//2获取编辑器
Editor edit = sp.edit();
//3存储值
edit.putBoolean("第一次", true);
//4提交
edit.commit();
pager = (ViewPager) findViewById(R.id.pager);
button = (Button) findViewById(R.id.button1);
button.setVisibility(View.GONE);
// 第一步:数据
final ArrayList<ImageView> list = new ArrayList<ImageView>();
int arr[] = { R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher };
for (int i = 0; i < arr.length; i++) {
ImageView imageView = new ImageView(MainActivity.this);
imageView.setImageResource(arr[i]);
list.add(imageView);
}
// 第二部:适配器
pager.setAdapter(new PagerAdapter() {
@Override
public int getCount() {
return list.size();
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0 == arg1;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = list.get(position);
container.addView(imageView);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position,
Object object) {
container.removeView((View) object);
}
});
// 第三部:页面滑动事件
pager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
if (arg0==list.size()-1) {
button.setVisibility(View.VISIBLE);
}else {
button.setVisibility(View.GONE);
}
}
@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
}
});
// 第四部:点击 事件跳转页面
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this,TwoActvity.class));
finish();//这行代码很重要必须写 不然会减5分;
}
});
}
}
//--------------------------------------------------------------------------------------------------
public class Frag_01 extends Fragment {
private ViewPager pager;
private RadioGroup radioGroup;
private ListView listView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frag_01, container, false);
// 找控件
pager = (ViewPager) view.findViewById(R.id.pager);
radioGroup = (RadioGroup) view.findViewById(R.id.radioGroup1);
listView = (ListView) view.findViewById(R.id.listView1);
// pager数据
initPager();
// listview数据
initlist();
return view;
}
private void initlist() {
final ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i < 14; i++) {
list.add("小哥哥网恋吗?我萝莉音哦!!" + i);
}
// 适配器
listView.setAdapter(new MyListAdapter(getActivity(), list));
//点击发送广播
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent=new Intent("com.example.week_01.gb.GuangBo");
intent.putExtra("key", list.get(position));
//开启广播
getActivity().sendBroadcast(intent);
}
});
}
private void initPager() {
int arr[] = { R.drawable.a1, R.drawable.a2, R.drawable.a3,
R.drawable.a4 };
final ArrayList<ImageView> arrayList = new ArrayList<ImageView>();
for (int i = 0; i < arr.length; i++) {
// 初始化imageView数据
ImageView imageView = new ImageView(getActivity());
imageView.setImageResource(arr[i]);
arrayList.add(imageView);
// 初始化小圆点
RadioButton button = new RadioButton(getActivity());
radioGroup.addView(button);
}
radioGroup.check(radioGroup.getChildAt(0).getId());
// 适配器
pager.setAdapter(new MypagerAdapter(arrayList,handler));
// 页面切换小圆点跟着走
pager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
arg0 %= arrayList.size();
radioGroup.check(radioGroup.getChildAt(arg0).getId());
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
// /自动轮播
handler.sendEmptyMessageDelayed(0, 3000);
}
Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case 0:
int currentItem = pager.getCurrentItem() + 1;
pager.setCurrentItem(currentItem);
handler.sendEmptyMessageDelayed(0, 3000);
break;
}
};
};
@Override
public void onDestroy() {// TODO Auto-generated method stub
super.onDestroy();
handler.removeCallbacksAndMessages(null);
}
}
适配器
package com.example.week_01.adapter;
import java.util.ArrayList;
import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
public class MypagerAdapter extends PagerAdapter {
ArrayList list;
Handler handler;
public MypagerAdapter(ArrayList<ImageView> list, Handler handler) {
super();
this.list = list;
this.handler = handler;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return Integer.MAX_VALUE;
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0 == arg1;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
position %= list.size();
ImageView imageView = list.get(position);
// 触摸事件
imageView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
// 得到事件
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:// 按下
handler.removeCallbacksAndMessages(null);
Log.i("TAG", "按下");
break;
case MotionEvent.ACTION_MOVE:// 滑动
handler.removeCallbacksAndMessages(null);
Log.i("TAG", "滑动");
break;
case MotionEvent.ACTION_CANCEL:// 非人为原因结束本次事件
handler.sendEmptyMessageDelayed(0, 3000);
Log.i("TAG", "非人为原因结束本次事件");
break;
case MotionEvent.ACTION_UP:// 抬起View(与DOWN对应)
handler.sendEmptyMessageDelayed(0, 3000);
Log.i("TAG", "抬起View");
break;
}
return true;// 表示消费了触摸事件,onTouch之后才执行onClick,这里被消费了,所以,相关的点击事件不会执行。
}
});
container.addView(imageView);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}