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>

4

2 回答 2

5

解决方案: 您需要使用GestureDetector来在您的手机上发生滑动时收到通知LinearLayout

请查看GestureDetector以供参考。

到目前为止,我已经在你的代码中应用了滑动,但是我没有你的 drawable,所以我只使用了我的。剩下的东西已经为你准备好了。所以只需使用你的 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;

    }

}

}

活动.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>

我希望它会有所帮助!

于 2013-05-17T06:57:41.883 回答
0
  1. 创建一个Activity扩展FragmentActivity.
  2. 创建两个片段 - 每个片段用于线性布局
  3. 创建您的View pager adapter扩展FragmentStatePagerAdapter.
  4. 为您提供一个fragments查看寻呼机适配器的列表。
于 2013-05-17T07:01:39.807 回答