效果图
1自动轮播
2手动轮播
3监听点击事件跳转界面
4每个可加标题(我的颜色太鲜艳了,就没有让文字显示)
接口回调步骤:
如果界面滚动了,获取当前的item,如果item==0,就设置为最后一个,如果,item==count+1,就设置为第一个,这样可以实现第一个和最后一个切换时不会出现闪现或卡顿的画面
这里用到了SparseBooleanArray,它是用来存储布尔值的,类似于key,value,根据其中存放的值来判断是否被选中ViewPager 添加fragment 就能直接滑动 他有一个监听setOnPageChangeListener() 是监听滑动的事件 滑动一次都会进入这
setCurrentItem(0); 这个0就是滑动的界面 0 表示第一个 1 表示第二界面 想滑动第几个界面就输入几第一个activity
package comqualitypictureactivity;
import javautilArrayList;
import javautilList;
import comqualitypictureR;
import androidappActivity;
import androidcontentContext;
import androidcontentIntent;
import androidosBundle;
import androidosParcelable;
import androidsupportv4viewPagerAdapter;
import androidsupportv4viewViewPager;
import androidsupportv4viewViewPagerOnPageChangeListener;
import androidutilDisplayMetrics;
import androidviewGestureDetector;
import androidviewKeyEvent;
import androidviewLayoutInflater;
import androidviewMotionEvent;
import androidviewView;
import androidviewWindow;
import androidviewGestureDetectorSimpleOnGestureListener;
import androidviewViewOnClickListener;
import androidwidgetButton;
import androidwidgetLinearLayout;
public class GuideActivity extends Activity
{
private ViewPager viewPager;
private List<View> mImageViews; // 滑动的集合
private int[] imageResId; // ID
private int currentItem = 0; // 当前的索引号
private GestureDetector gestureDetector; // 用户滑动
/ 记录当前分页ID /
private int flaggingWidth;// 互动翻页所需滚动的长度是当前屏幕宽度的1/3
@Override
public void onCreate(Bundle savedInstanceState)
{
superonCreate(savedInstanceState);
requestWindowFeature(WindowFEATURE_NO_TITLE);// 去掉标题栏
setContentView(Rlayoutguide_activity);
gestureDetector = new GestureDetector(new GuideViewTouch());
// 获取分辨率
DisplayMetrics dm = new DisplayMetrics();
getWindowManager()getDefaultDisplay()getMetrics(dm);
flaggingWidth = dmwidthPixels / 3;
imageResId = new int[]
{ Rdrawablepic_1, Rdrawablepic_2, Rdrawablepic_3, Rdrawablepic_4 };
mImageViews = new ArrayList<View>();
// 初始化资源
LayoutInflater viewInflater = (LayoutInflater) getSystemService(ContextLAYOUT_INFLATER_SERVICE);
// 0
View convertView0 = viewInflaterinflate(Rlayoutguide_item, null);
LinearLayout linearLayout0 = (LinearLayout) convertView0findViewById(Ridguide_item);
linearLayout0setBackgroundResource(imageResId[0]);
mImageViewsadd(linearLayout0);
// 1
View convertView1 = viewInflaterinflate(Rlayoutguide_item, null);
LinearLayout linearLayout1 = (LinearLayout) convertView1findViewById(Ridguide_item);
linearLayout1setBackgroundResource(imageResId[1]);
mImageViewsadd(linearLayout1);
// 2
View convertView2 = viewInflaterinflate(Rlayoutguide_item, null);
LinearLayout linearLayout2 = (LinearLayout) convertView2findViewById(Ridguide_item);
linearLayout2setBackgroundResource(imageResId[2]);
mImageViewsadd(linearLayout2);
// 3
View convertView3 = viewInflaterinflate(Rlayoutguide_item, null);
LinearLayout linearLayout3 = (LinearLayout) convertView3findViewById(Ridguide_item);
linearLayout3setBackgroundResource(imageResId[3]);
mImageViewsadd(linearLayout3);
// button监听
Button btn = (Button) convertView3findViewById(Ridstart);
btnsetVisibility(ViewVISIBLE);
btnsetOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
GoToMainActivity();
}
});
viewPager = (ViewPager) findViewById(Ridguide_view);
viewPagersetAdapter(new MyAdapter());// 设置填充ViewPager页面的适配器
// 设置一个监听器,当ViewPager中的页面改变时调用
viewPagersetOnPageChangeListener(new MyPageChangeListener());
}
@Override
public boolean dispatchTouchEvent(MotionEvent event)
{
if (gestureDetectoronTouchEvent(event))
{
eventsetAction(MotionEventACTION_CANCEL);
}
return superdispatchTouchEvent(event);
}
private class GuideViewTouch extends SimpleOnGestureListener
{
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
{
if (currentItem == 3)
{
if (Mathabs(e1getX() - e2getX()) > Mathabs(e1getY() - e2getY()) && (e1getX() - e2getX() <= (-flaggingWidth) || e1getX() - e2getX() >= flaggingWidth))
{
if (e1getX() - e2getX() >= flaggingWidth)
{
GoToMainActivity();
return true;
}
}
}
return false;
}
}
/
进入主界面
/
void GoToMainActivity()
{
Intent intent = new Intent(GuideActivitythis, TabHostActivityclass);
startActivity(intent);
finish();
}
/
当ViewPager中页面的状态发生改变时调用
@author Administrator
/
private class MyPageChangeListener implements OnPageChangeListener
{
public void onPageSelected(int position)
{
currentItem = position;
}
public void onPageScrollStateChanged(int arg0)
{
}
public void onPageScrolled(int arg0, float arg1, int arg2)
{
}
}
/
填充ViewPager页面的适配器
@author Administrator
/
private class MyAdapter extends PagerAdapter
{
@Override
public int getCount()
{
return imageResIdlength;
}
@Override
public Object instantiateItem(View arg0, int arg1)
{
((ViewPager) arg0)addView(mImageViewsget(arg1));
return mImageViewsget(arg1);
}
@Override
public void destroyItem(View arg0, int arg1, Object arg2)
{
((ViewPager) arg0)removeView((View) arg2);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1)
{
return arg0 == arg1;
}
@Override
public void restoreState(Parcelable arg0, ClassLoader arg1)
{
}
@Override
public Parcelable saveState()
{
return null;
}
@Override
public void startUpdate(View arg0)
{
}
@Override
public void finishUpdate(View arg0)
{
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
// TODO Auto-generated method stub
if (keyCode == KeyEventKEYCODE_BACK)
{
GoToMainActivity();
return false;
}
return superonKeyDown(keyCode, event);
}
}
布局文件
第一个
<xml version="10" encoding="utf-8">
<LinearLayout xmlns:android=" >
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)