问题
I am trying to design one screen which contain some swipe part. I have one screen with mapview, listview and some text. My Mapview is main part of swipe, if i swipe at left then contact will shown and if i swipe right then address will be display.
Here is my image. i am showing rounded part which i need to swipe.
How can i implement this functionality in my app. I go through with one tutorial but it didn't help me. This tutorial teach how to swipe full screen but i need to swipe some part of the screen.
Is it possible. Give me some reference or hint. I didn't understand
viewPager
.
回答1:
Finally i got answer to swipe some part of the screen. in onCreate() you need to add following line
MyPagerAdapter adapter = new MyPagerAdapter();
myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(3);
I extend PagerAdapter class in my class.
private class MyPagerAdapter extends PagerAdapter {
public int getCount() {
return 3;
}
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 = showHotelContact();
break;
case 1:
resId = showHotelAddress();
break;
case 2:
resId = showHotelMap();
break;
}
View view = inflater.inflate(resId, null);
((ViewPager) collection).addView(view, 0);
return view;
}
}
public int showHotelMap()
{
int resId;
resId = R.layout.hotelmap;
return resId;
}
public int showHotelAddress()
{
int resId;
resId = R.layout.hoteladdress;
return resId;
}
public int showHotelContact()
{
int resId;
resId = R.layout.hotelcontact;
return resId;
}
Here is my xml file
?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<android.support.v4.view.ViewPager
android:id="@+id/myfivepanelpager"
android:layout_width="wrap_content"
android:layout_height="186dp"
android:fadingEdge="vertical" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hotelName" />
</LinearLayout>
回答2:
Create a class like this :
public class OnSwipeTouchListener implements OnTouchListener {
private final GestureDetector gestureDetector = new GestureDetector(new GestureListener());
public boolean onTouch(final View v, final MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
private final class GestureListener extends SimpleOnGestureListener {
private static final int SWIPE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
onTouch(e);
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
boolean result = false;
try {
float diffY = e2.getY() - e1.getY();
float diffX = e2.getX() - e1.getX();
if (Math.abs(diffX) > Math.abs(diffY)) {
if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (diffX > 0) {
onSwipeRight();
} else {
onSwipeLeft();
}
}
} else {
// onTouch(e);
}
} catch (Exception exception) {
exception.printStackTrace();
}
return result;
}
}
public void onTouch(MotionEvent e) {
}
public void onDown(MotionEvent e) {
}
public void onSwipeRight() {
}
public void onSwipeLeft() {
}
public void onSwipeTop() {
}
public void onSwipeBottom() {
}
}
and then add OnSwipeTouchListener
to your MapView
来源:https://stackoverflow.com/questions/13969837/swipe-some-part-of-the-screen