gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 12:57:57 25 4
gpt4 key购买 nike

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

最佳答案

解决方案:您需要使用 GestureDetector 来在您的 LinearLayout

上发生滑动时收到通知

请看GestureDetector供引用。

到目前为止,我已经在你的代码中应用了滑动,但我没有你的可绘制对象,所以我只使用了我的。剩下的东西已经为你准备好了。所以只需使用你的 xml 文件而不是我的。

示例:

主 Activity .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>

希望对您有所帮助!!

关于android - 在 Framelayout 中相互重叠的两个布局之间滑动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16602864/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com