在Framelayout中相互重叠的两个布局之间滑动

时间:2013-05-17 06:31:48

标签: android android-linearlayout swipe

我想在两个以帧布局包装的线性布局之间滑动。同时,只有一个线性布局可见。我正在使用按钮来显示/隐藏它们。我想在其中实现滑动功能。我试过了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>

2 个答案:

答案 0 :(得分:5)

<强>解决方案: 您需要使用GestureDetectorLinearLayout

上进行滑动时收到通知

请查看GestureDetector以供参考。

到目前为止,我已经在你的代码中应用了swipe,但是我没有你的drawables所以我刚刚使用了我的东西。这件事已经为你准备好了。所以只使用你的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>

我希望它会有所帮助!!

答案 1 :(得分:0)

  1. 创建Activity扩展FragmentActivity
  2. 创建两个片段 - 每个片段用于线性布局
  3. 创建View pager adapter扩展FragmentStatePagerAdapter
  4. 向您的View Viewr适配器提供fragments列表。