Android:如何在 Activity (不是 fragment )之间滑动,主/细节最佳设置

标签 android android-layout android-activity android-fragments adt

我正在开发一个 Android 应用程序,我对这一切还很陌生,包括移动应用程序开发,所以我有几个问题。任何帮助都会很棒!

1) 是否可以在整个 Activity (包括操作栏)之间滑动? 我的意思不是像 viewPager 在 fragment 之间交换,我的意思是交换 整个屏幕(就像 iOS 上的 snapchat 一样)。这可能吗?如果是这样, 我该怎么做?

2) 实现主/从类型布局的最佳方式是什么?喜欢推特 例如,如果我有推文的 ListView ,当有人点击 tweet 它将带您到该特定推文的详细 View ......那是什么 最好的方法来完成这个?我会有一个带有 ListView 的 Activity 并创建 单击推文后的第二个 Activity ?或者我会用 fragment 代替,一个 用于 ListView ,一个用于详细 View ?

3) 有没有办法让每个 fragment 都有不同的操作栏?

非常感谢!!!

最佳答案

是的,可以滑动:

OnTouchSwipeListener

import android.content.Context;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class OnSwipeTouchListener implements OnTouchListener {



private final GestureDetector gestureDetector;
private Context context;

/* (non-Javadoc)
 * @see android.view.View.OnTouchListener#onTouch(android.view.View, android.view.MotionEvent)
 */
public boolean onTouch(final View view, final MotionEvent motionEvent) {
    return gestureDetector.onTouchEvent(motionEvent);
}

/**
 * Gets the gesture detector.
 * 
 * @return the gesture detector
 */
public GestureDetector getGestureDetector(){
    return  gestureDetector;
}

/**
 * Instantiates a new on swipe touch listener.
 * 
 * @param context
 *            the context
 */
public OnSwipeTouchListener(Context context) {
    super();
    this.context = context;
    gestureDetector = new GestureDetector(context, new GestureListener());
}

private final class GestureListener extends SimpleOnGestureListener {

    private static final int SWIPE_THRESHOLD = 100;
    private static final int SWIPE_VELOCITY_THRESHOLD = 100;

    /* (non-Javadoc)
     * @see android.view.GestureDetector.SimpleOnGestureListener#onDown(android.view.MotionEvent)
     */
    @Override
    public boolean onDown(MotionEvent e) {
        return true;
    }

    /* (non-Javadoc)
     * @see android.view.GestureDetector.SimpleOnGestureListener#onFling(android.view.MotionEvent, android.view.MotionEvent, float, float)
     */

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        boolean result = false;
        try {
            float diffY = e2.getRawY() - e1.getRawY();
            float diffX = e2.getRawX() - e1.getRawX();
            if ((Math.abs(diffX) - Math.abs(diffY)) > SWIPE_THRESHOLD) {
                if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffX > 0) {
                        onSwipeRight();
                    } else {
                        onSwipeLeft();
                    }
                }
            } else {
                if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffY > 0) {
                        onSwipeBottom();
                    } else {
                        onSwipeTop();
                    }
                }
            }
        } catch (Exception e) {

        }
        return result;
    }
}

/**
 * On swipe right.
 */
public void onSwipeRight() {
}

/**
 * On swipe left.
 */
public void onSwipeLeft() {
}

/**
 * On swipe top.
 */
public void onSwipeTop() {
}

/**
 * On swipe bottom.
 */
public void onSwipeBottom() {
}
}

实现:

OnSwipeTouchListener onSwipeTouchListener = new OnSwipeTouchListener(Activity.this) {
        @Override
        public void onSwipeLeft() {
            //your actions
        }
    };

关于Android:如何在 Activity (不是 fragment )之间滑动,主/细节最佳设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24254722/

相关文章:

android - xml 文件中已知属性的未知属性

java - 从界面停止 Activity

android - 如何在 Android 中让闹钟开始响铃监听器?

android - 向上按钮 - getSupportActionBar().setDisplayShowHomeEnabled(true) - 关闭应用

java - 从 SQLite Column 中获取所有数字字符串并计算总和

java - 将列表转换为数组

java - 将 cartoDB 浏览器中的图层添加到 Android 应用程序

java - 如何检测空白区域的点击?

android - 如何找到我的 Google Play 服务 Android BASE64_PUBLIC_KEY

android - 为什么Android示例软键盘布局是这样的?