2013-05-17 64 views
3

我想在框架佈局中包裹的兩個線性佈局之間滑動。有一次,只有一個線性佈局是可見的。我正在使用按鈕來顯示/隱藏它們。我想在它們中實現滑動功能。我嘗試了ViewFliper,但沒有用手指輕掃。有沒有可能使用ViewPager做到這一點?在框架佈局中互相重疊的兩個佈局之間滑動

佈局XML是

<?xml version="1.0" encoding="utf-8"?> 

<FrameLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:background="#00000000" > 

     <LinearLayout 
      android:id="@+id/layTopUp" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:background="@drawable/yellowspace" 
      android:gravity="center_vertical|center_horizontal" 
      android:orientation="vertical" 
      android:padding="25dp" 
      android:visibility="visible" > 

      <TextView 
       android:id="@+id/textView1" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="3dp" 
       android:text="XXX" 
       android:textColor="#000000" /> 

      <Spinner 
       android:id="@+id/spnTopupOperator" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="3dp" /> 

      <TextView 
       android:id="@+id/textView2" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="3dp" 
       android:text="XXX" 
       android:textColor="#000000" /> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="10dp" 
       android:gravity="center_vertical|center_horizontal" > 

       <TextView 
        android:id="@+id/textView3" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="XXX" 
        android:textColor="#000000" /> 

       <EditText 
        android:id="@+id/etTopUpCode" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:ems="3" 
        android:hint="Code" 
        android:inputType="number" 
        android:maxLength="3" > 

        <requestFocus /> 
       </EditText> 

       <EditText 
        android:id="@+id/etTopUpNumber" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:ems="7" 
        android:hint="Number" 
        android:inputType="number" 
        android:maxLength="7" 
        android:shadowColor="#000000" /> 
      </LinearLayout> 

      <TextView 
       android:id="@+id/textView4" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="3dp" 
       android:text="XXX" 
       android:textColor="#000000" /> 

      <Spinner 
       android:id="@+id/spnTopupAmount" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="3dp" /> 

      <LinearLayout 
       android:id="@+id/layConv" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="3dp" 
       android:gravity="center_vertical|center_horizontal" 
       android:orientation="horizontal" > 

       <TextView 
        android:id="@+id/tvConvertedAmount" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:gravity="center_vertical|center_horizontal" 
        android:text="XXX " 
        android:textColor="#000000" /> 

       <ProgressBar 
        android:id="@+id/pbConversion" 
        style="?android:attr/progressBarStyleSmall" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:visibility="gone" /> 
      </LinearLayout> 

      <ImageButton 
       android:id="@+id/bTopUpSend" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="3dp" 
       android:background="@drawable/sendrechbotton" /> 
     </LinearLayout> 

     <LinearLayout 
      android:id="@+id/layScratchCard" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:background="@drawable/yellowspace" 
      android:gravity="center_vertical|center_horizontal" 
      android:orientation="vertical" 
      android:padding="25dp" 
      android:visibility="gone" > 

      <TextView 
       android:id="@+id/textView6" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="XXXX" 
       android:textColor="#000000" /> 

      <Spinner 
       android:id="@+id/spnRechargeOperator" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" /> 

      <TextView 
       android:id="@+id/textView7" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="XXX" 
       android:textColor="#000000" /> 

      <EditText 
       android:id="@+id/etRechargeEmail" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:ems="10" 
       android:hint="Email" 
       android:inputType="textEmailAddress" /> 

      <TextView 
       android:id="@+id/textView8" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="XXXX" 
       android:textColor="#000000" /> 

      <Spinner 
       android:id="@+id/spnRechargeAmount" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" /> 

      <ImageButton 
       android:id="@+id/bSendCard" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:background="@drawable/sendcardbotton" /> 
     </LinearLayout> 
    </FrameLayout> 
</LinearLayout> 

+0

是的,你可以創建兩個片段,並將它們提供給Viewpager的適配器。 – Anukool

回答

5

解決方案: 您需要使用GestureDetector只要輕掃你的LinearLayout

發生時得到通知210

請查看GestureDetector以供參考。

截至目前,我已經在你的代碼中應用了刷卡功能,但是我沒有你的可繪製物,所以我剛剛使用過mine.rest的東西已經準備好了,因此只需使用你的xml文件而不是我的。

實施例:

MainActivity.java

package com.mehuljoisar.swipe; 
import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.GestureDetector; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.View.OnTouchListener; 
import android.view.animation.Animation; 
import android.view.animation.AnimationUtils; 
import android.widget.FrameLayout; 
import android.widget.ImageView; 
import android.widget.LinearLayout; 
import android.widget.Toast; 



public class MainActivity extends Activity { 

private GestureDetector gesturedetector = null; 

private Intent i; 

private FrameLayout flContainer; 
private LinearLayout ivLayer1,ivLayer2; 

@Override 
public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 

    setContentView(R.layout.acti); 

    flContainer = (FrameLayout) findViewById(R.id.flContainer); 
    ivLayer1 = (LinearLayout)findViewById(R.id.layTopUp); 
    ivLayer2 = (LinearLayout)findViewById(R.id.layScratchCard); 


    gesturedetector = new GestureDetector(new MyGestureListener()); 

    flContainer.setOnTouchListener(new OnTouchListener() { 

     @Override 
     public boolean onTouch(View v, MotionEvent event) { 

      gesturedetector.onTouchEvent(event); 

      return true; 

     } 

    }); 

} 

public boolean dispatchTouchEvent(MotionEvent ev) { 

    super.dispatchTouchEvent(ev); 

    return gesturedetector.onTouchEvent(ev); 

} 

class MyGestureListener extends GestureDetector.SimpleOnGestureListener { 

    private static final int SWIPE_MIN_DISTANCE = 20; 

    private static final int SWIPE_MAX_OFF_PATH = 100; 

    private static final int SWIPE_THRESHOLD_VELOCITY = 100; 

    @Override 
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, 

    float velocityY) { 

     float dX = e2.getX() - e1.getX(); 

     float dY = e1.getY() - e2.getY(); 

     if (Math.abs(dY) < SWIPE_MAX_OFF_PATH && 

     Math.abs(velocityX) >= SWIPE_THRESHOLD_VELOCITY && 

     Math.abs(dX) >= SWIPE_MIN_DISTANCE) { 

      if (dX > 0) { 

       Toast.makeText(getApplicationContext(), "Right Swipe", 
         Toast.LENGTH_SHORT).show(); 
       //Now Set your animation 

       if(ivLayer2.getVisibility()==View.GONE) 
       { 
        Animation fadeInAnimation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.slide_right_in); 
        ivLayer2.startAnimation(fadeInAnimation); 
        ivLayer2.setVisibility(View.VISIBLE); 
       } 
      } else { 

       Toast.makeText(getApplicationContext(), "Left Swipe", 
         Toast.LENGTH_SHORT).show(); 

       if(ivLayer2.getVisibility()==View.VISIBLE) 
       { 
        Animation fadeInAnimation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.slide_left_out); 
        ivLayer2.startAnimation(fadeInAnimation); 
        ivLayer2.setVisibility(View.GONE); 
       } 

      } 

      return true; 

     } else if (Math.abs(dX) < SWIPE_MAX_OFF_PATH && 

     Math.abs(velocityY) >= SWIPE_THRESHOLD_VELOCITY && 

     Math.abs(dY) >= SWIPE_MIN_DISTANCE) { 

      if (dY > 0) { 

       Toast.makeText(getApplicationContext(), "Up Swipe", 
         Toast.LENGTH_SHORT).show(); 

      } else { 

       Toast.makeText(getApplicationContext(), "Down Swipe", 
         Toast.LENGTH_SHORT).show(); 
      } 

      return true; 

     } 

     return false; 

    } 

} 

} 

acti.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/linear" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 
<FrameLayout 
    android:id="@+id/flContainer" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_horizontal" 
    android:background="@android:color/black" > 

     <LinearLayout 
      android:id="@+id/layTopUp" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:background="@android:color/darker_gray" 
      android:gravity="center_vertical|center_horizontal" 
      android:orientation="vertical" 
      android:padding="25dp" 
      android:visibility="visible" > 

      <TextView 
       android:id="@+id/textView1" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="3dp" 
       android:text="XXX" 
       android:textColor="#000000" /> 

      <Spinner 
       android:id="@+id/spnTopupOperator" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="3dp" /> 

      <TextView 
       android:id="@+id/textView2" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="3dp" 
       android:text="XXX" 
       android:textColor="#000000" /> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="10dp" 
       android:gravity="center_vertical|center_horizontal" > 

       <TextView 
        android:id="@+id/textView3" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="XXX" 
        android:textColor="#000000" /> 

       <EditText 
        android:id="@+id/etTopUpCode" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:ems="3" 
        android:hint="Code" 
        android:inputType="number" 
        android:maxLength="3" > 

        <requestFocus /> 
       </EditText> 

       <EditText 
        android:id="@+id/etTopUpNumber" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:ems="7" 
        android:hint="Number" 
        android:inputType="number" 
        android:maxLength="7" 
        android:shadowColor="#000000" /> 
      </LinearLayout> 

      <TextView 
       android:id="@+id/textView4" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="3dp" 
       android:text="XXX" 
       android:textColor="#000000" /> 

      <Spinner 
       android:id="@+id/spnTopupAmount" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="3dp" /> 

      <LinearLayout 
       android:id="@+id/layConv" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="3dp" 
       android:gravity="center_vertical|center_horizontal" 
       android:orientation="horizontal" > 

       <TextView 
        android:id="@+id/tvConvertedAmount" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:gravity="center_vertical|center_horizontal" 
        android:text="XXX " 
        android:textColor="#000000" /> 

       <ProgressBar 
        android:id="@+id/pbConversion" 
        style="?android:attr/progressBarStyleSmall" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:visibility="gone" /> 
      </LinearLayout> 

      <ImageButton 
       android:id="@+id/bTopUpSend" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="3dp" 
       android:background="@drawable/icon" /> 
     </LinearLayout> 

     <LinearLayout 
      android:id="@+id/layScratchCard" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:background="@drawable/icon" 
      android:gravity="center_vertical|center_horizontal" 
      android:orientation="vertical" 
      android:padding="25dp" 
      android:visibility="gone" > 

      <TextView 
       android:id="@+id/textView6" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="XXXX" 
       android:textColor="#000000" /> 

      <Spinner 
       android:id="@+id/spnRechargeOperator" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" /> 

      <TextView 
       android:id="@+id/textView7" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="XXX" 
       android:textColor="#000000" /> 

      <EditText 
       android:id="@+id/etRechargeEmail" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:ems="10" 
       android:hint="Email" 
       android:inputType="textEmailAddress" /> 

      <TextView 
       android:id="@+id/textView8" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="XXXX" 
       android:textColor="#000000" /> 

      <Spinner 
       android:id="@+id/spnRechargeAmount" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" /> 

      <ImageButton 
       android:id="@+id/bSendCard" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:background="@drawable/icon" /> 
     </LinearLayout> 
</FrameLayout> 


<!--  <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd" /> 
--> 
</LinearLayout> 

slide_left_out.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"> 
    <translate android:fromXDelta="0" android:toXDelta="-100%p" android:duration="800"/> 
</set> 

slide_right_in.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"> 
    <translate android:fromXDelta="-100%p" android:toXDelta="0" android:duration="800"/> 
</set> 

我希望這會對您有所幫助!

+0

感謝隊友去實施它 – kaibuki

+0

我很高興幫助! –

0
  1. 創建一個Activity延伸FragmentActivity
  2. 創建兩個片段 - 每個用於線性佈局
  3. 創建您的View pager adapter延伸FragmentStatePagerAdapter
  4. 給您提供一份fragments的列表查看尋呼機適配器。