http://s10.postimg.org/knckl2n4p/Untitled.png [^]
我喜欢这个结果。请帮我。我尝试过,但我不是这样的。我是android的新手。感谢..
答案 0 :(得分:1)
由于您不需要标签功能,因此可以使用GestureDetector.SimpleOnGestureListener
在您的活动类中:
class MyGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
try {
float slope = (e1.getY() - e2.getY()) / (e1.getX() - e2.getX());
float angle = (float) Math.atan(slope);
float angleInDegree = (float) Math.toDegrees(angle);
// left to right
if (e1.getX() - e2.getX() > 20 && Math.abs(velocityX) > 20) {
if ((angleInDegree < 45 && angleInDegree > -45)) {
//code for left to right swipe should go here
}
// right to left fling
} else if (e2.getX() - e1.getX() > 20
&& Math.abs(velocityX) > 20) {
if ((angleInDegree < 45 && angleInDegree > -45)) {
//code for right to left swipe should go here
}
}
return true;
} catch (Exception e) {
// nothing
}
return false;
}
}
然后,您可以注册任何视图以接收/收听手势:
final GestureDetector gestureDetector = new GestureDetector(new MyGestureDetector());
gameView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) return false;
return false;
}
});
//the parent layout
findViewById(R.id.parent_layout).setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) return false;
return false;
}
});
//an image view
findViewById(R.id.image_view).setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) return false;
return false;
}
});
答案 1 :(得分:0)
我知道你很困惑并希望得到答案,但我们非常感谢您发布自己的代码和错误(如果有的话),以便我们可以通过查看错误的位置直接帮助您。!
可能是这个(我这样做了)会帮助你:
1-根据需要制作任意数量的页面。在您的布局中,即page1.xml,page2.xml .....
2-将此内容写入您的activity_main.xml
<android.support.v4.view.ViewPager
android:id="@+id/customviewpager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
3-在主要班级
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create and set adapter
Pager adapter = new Pager();
ViewPager myPager = (ViewPager) findViewById(R.id.customviewpager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(0); // 0 shows starting page which you have on switch case index 0
}
4-在同一个包中创建另一个类我
public class Pager extends PagerAdapter {
public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int resId = 0;
switch (position) {
case 0: {
resId = R.layout.page1;
break;
}
case 1: {
resId = R.layout.page2;
break;
}
case 2: {
resId = R.layout.page3;
break;
}
}
View view = inflater.inflate(resId, null);
((ViewPager) collection).addView(view, 0);
return view;
}
@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 == ((View) arg1);
}
@Override
public Parcelable saveState() {
return null;
}
@Override
public int getCount() {
return 3; //total page count
}
}
5-导入所需的库。!
希望这会对你有所帮助,我以前也学到了这一点:)