下拉菜单或在幻灯片上查看

发布于 2024-11-01 03:02:56 字数 121 浏览 1 评论 0原文

我想知道是否有人可以指导我如何完成以下任务,

当用户按住标题栏并向下拖动时,用户有一个标题栏;它向用户展示了不同的视图。这是驻留在 tabhost 中的视图。

它类似于android的默认状态栏。

I was wondering if someone could guide me on how to accomplish the following,

The user has a title bar, when the user holds the title bar and drags down; it shows the user a different view. This is a view residing in a tabhost.

It is similar to android's default statusbar.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

您应该尝试使用 ViewFlipperRelativeLayout 子项并在 header 上附加 OnTouchListeners 来完成此操作(类似 AccordionView 的行为)。这对我有用。

示例应用程序

ma​​in.xml //在res/layouts

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent">
    <ViewFlipper android:id="@+id/flipper" 
        android:layout_width="fill_parent" android:layout_height="fill_parent">
        <RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent">
            <TextView android:id="@+id/txt_states" android:text="First panel" 
                android:layout_centerHorizontal="true"
                android:textSize="18dp" android:textStyle="bold" android:textColor="@android:color/white"
                android:layout_width="wrap_content" android:layout_height="wrap_content" />
            <Button android:id="@+id/btn_next" android:text="@string/goto_second"
                android:layout_width="fill_parent" android:layout_height="wrap_content" 
                android:gravity="center_horizontal" android:layout_centerHorizontal="true" android:layout_alignParentBottom="true"/>
            <TextView android:id="@+id/txt_content1" android:text="@string/loremipsum1" 
                 android:layout_width="fill_parent" android:layout_height="fill_parent" 
                 android:layout_below="@id/txt_states" android:layout_above="@id/btn_next" 
                 android:layout_marginTop="10dp" />
        </RelativeLayout>
        <RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent">
            <Button android:id="@+id/btn_prev" android:text="@string/goto_first"
                android:layout_width="fill_parent" android:layout_height="wrap_content"
                android:gravity="center_horizontal" android:layout_centerHorizontal="true" />
            <TextView android:id="@+id/txt_commands" android:text="Second panel" 
                android:layout_centerHorizontal="true"
                android:textSize="18dp" android:textStyle="bold" android:textColor="@android:color/white"
                android:layout_width="wrap_content" android:layout_height="wrap_content" 
                android:layout_below="@id/btn_prev" />
            <TextView android:id="@+id/txt_content2" android:text="@string/loremipsum2" 
                 android:layout_width="fill_parent" android:layout_height="fill_parent"
                 android:layout_below="@id/txt_commands" android:layout_marginTop="10dp" />
        </RelativeLayout>
    </ViewFlipper>
</RelativeLayout>

androidManifest.xml //仅活动声明

<activity android:name=".AccordionSample" android:label="@string/app_name">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

AccordionSample.java //你的主要活动

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ViewFlipper;

public class AccordionSample extends Activity implements View.OnTouchListener 
{
    private float oldTouchValue;
    private ViewFlipper flipper;
    private Button currentButton;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        flipper = (ViewFlipper)findViewById(R.id.flipper);
        findViewById(R.id.btn_prev).setOnTouchListener(this);
        findViewById(R.id.btn_next).setOnTouchListener(this);
    }

    private boolean onButtonTouchEvent(MotionEvent touchevent)
    {
        if (currentButton == null)
            return false;
        switch (touchevent.getAction())
        {
            case MotionEvent.ACTION_DOWN:
            {
                oldTouchValue = touchevent.getY();
                break;
            }
            case MotionEvent.ACTION_UP:
            {
                float currentY = touchevent.getY();
                final float diff = oldTouchValue - currentY;
                if ((diff < -100) && (currentButton.getId() == R.id.btn_prev))
                {
                    //Up --> Bottom
                    flipper.setInAnimation(AccordionAnimation.inFromTopAnimation());
                    flipper.setOutAnimation(AccordionAnimation.outToBottomAnimation());
                    flipper.showNext();
                }
                else if ((diff > 100) && (currentButton.getId() == R.id.btn_next))
                {
                    //Bottom --> Up
                    flipper.setInAnimation(AccordionAnimation.inFromBottomAnimation());
                    flipper.setOutAnimation(AccordionAnimation.outToTopAnimation());
                    flipper.showPrevious();
                }
                break;
            }
        }
        currentButton = null;
        return true;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event)
    {
        currentButton = (Button)v;
        final boolean result = this.onButtonTouchEvent(event);
        return result;
    }
}

AccordionAnimation.java //用于上下滑动

import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;

public class AccordionAnimation
{
    public static Animation inFromBottomAnimation()
    {
        Animation inFromBottom =
                new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f,
                        Animation.RELATIVE_TO_PARENT, +1.0f, Animation.RELATIVE_TO_PARENT, 0.0f);
        inFromBottom.setDuration(350);
        inFromBottom.setInterpolator(new AccelerateInterpolator());
        return inFromBottom;
    }

    public static Animation outToTopAnimation()
    {
        Animation outtoTop =
                new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f,
                        Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, -1.0f);
        outtoTop.setDuration(350);
        outtoTop.setInterpolator(new AccelerateInterpolator());
        return outtoTop;
    }

    public static Animation inFromTopAnimation()
    {
        Animation inFromTop =
                new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f,
                        Animation.RELATIVE_TO_PARENT, -1.0f, Animation.RELATIVE_TO_PARENT, 0.0f);
        inFromTop.setDuration(350);
        inFromTop.setInterpolator(new AccelerateInterpolator());
        return inFromTop;
    }

    public static Animation outToBottomAnimation()
    {
        Animation outtoBottom =
                new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f,
                        Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, +1.0f);
        outtoBottom.setDuration(350);
        outtoBottom.setInterpolator(new AccelerateInterpolator());
        return outtoBottom;
    }
}

strings.xml / /inside res/values

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="goto_first">Slide down to see the first panel</string>
    <string name="goto_second">Slide up to see the second panel</string>
    <string name="loremipsum1">Lorem ipsum dolor sit amet...</string>
    <string name="loremipsum2">Sed ut perspiciatis unde omnis iste...</string>
    <string name="app_name">AccordionSample</string>
</resources>

编辑
如果您想将不同的视图拆分为不同的布局 xml,例如

  1. first_view.xml 和
  2. secondary view.xml,

您应该修改您的 onCreate 方法:

flipper = (ViewFlipper)findViewById(R.id.flipper);
LayoutInflater inflater = getLayoutInflater();

final View firstView = inflater.inflate(R.layout.first_view, flipper, false);
flipper.addView(firstView);

final View secondView = inflater.inflate(R.layout.second_view, flipper, false);
flipper.addView(secondView);

firstView.findViewById(R.id.btn_next).setOnTouchListener(this);
secondView.findViewById(R.id.btn_prev).setOnTouchListener(this);

Edit
或者更优雅、更高效,只需将您为不同视图创建的布局 xml 包含到 main.xml 中:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent">
    <ViewFlipper android:id="@+id/flipper" 
        android:layout_width="fill_parent" android:layout_height="fill_parent">
        <include android:id="@+id/flipping_view_1" layout="@layout/flipping_view_1" />
        <include android:id="@+id/flipping_view_2" layout="@layout/flipping_view_2" />
    </ViewFlipper>
</RelativeLayout>

其中

  • layout/flipping_view_1.xml
    包含第一个 RelativeLayout
  • layout/flipping_view_2.xml
    第二个 RelativeLayout

包含原始 main.xml 中 ViewFlipper

。要了解有关 android 布局的可重用性以及整体布局技术的更多信息,您应该查看 Romain Guy 的精彩帖子,介绍了布局技巧

You should try to accomplish this (an AccordionView-like behavior) using ViewFlipper with RelativeLayout children and attached OnTouchListeners on the headers. It worked for me.

Sample application

main.xml //inside res/layouts

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent">
    <ViewFlipper android:id="@+id/flipper" 
        android:layout_width="fill_parent" android:layout_height="fill_parent">
        <RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent">
            <TextView android:id="@+id/txt_states" android:text="First panel" 
                android:layout_centerHorizontal="true"
                android:textSize="18dp" android:textStyle="bold" android:textColor="@android:color/white"
                android:layout_width="wrap_content" android:layout_height="wrap_content" />
            <Button android:id="@+id/btn_next" android:text="@string/goto_second"
                android:layout_width="fill_parent" android:layout_height="wrap_content" 
                android:gravity="center_horizontal" android:layout_centerHorizontal="true" android:layout_alignParentBottom="true"/>
            <TextView android:id="@+id/txt_content1" android:text="@string/loremipsum1" 
                 android:layout_width="fill_parent" android:layout_height="fill_parent" 
                 android:layout_below="@id/txt_states" android:layout_above="@id/btn_next" 
                 android:layout_marginTop="10dp" />
        </RelativeLayout>
        <RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent">
            <Button android:id="@+id/btn_prev" android:text="@string/goto_first"
                android:layout_width="fill_parent" android:layout_height="wrap_content"
                android:gravity="center_horizontal" android:layout_centerHorizontal="true" />
            <TextView android:id="@+id/txt_commands" android:text="Second panel" 
                android:layout_centerHorizontal="true"
                android:textSize="18dp" android:textStyle="bold" android:textColor="@android:color/white"
                android:layout_width="wrap_content" android:layout_height="wrap_content" 
                android:layout_below="@id/btn_prev" />
            <TextView android:id="@+id/txt_content2" android:text="@string/loremipsum2" 
                 android:layout_width="fill_parent" android:layout_height="fill_parent"
                 android:layout_below="@id/txt_commands" android:layout_marginTop="10dp" />
        </RelativeLayout>
    </ViewFlipper>
</RelativeLayout>

androidManifest.xml //activity declaration only

<activity android:name=".AccordionSample" android:label="@string/app_name">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

AccordionSample.java //your main activity

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ViewFlipper;

public class AccordionSample extends Activity implements View.OnTouchListener 
{
    private float oldTouchValue;
    private ViewFlipper flipper;
    private Button currentButton;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        flipper = (ViewFlipper)findViewById(R.id.flipper);
        findViewById(R.id.btn_prev).setOnTouchListener(this);
        findViewById(R.id.btn_next).setOnTouchListener(this);
    }

    private boolean onButtonTouchEvent(MotionEvent touchevent)
    {
        if (currentButton == null)
            return false;
        switch (touchevent.getAction())
        {
            case MotionEvent.ACTION_DOWN:
            {
                oldTouchValue = touchevent.getY();
                break;
            }
            case MotionEvent.ACTION_UP:
            {
                float currentY = touchevent.getY();
                final float diff = oldTouchValue - currentY;
                if ((diff < -100) && (currentButton.getId() == R.id.btn_prev))
                {
                    //Up --> Bottom
                    flipper.setInAnimation(AccordionAnimation.inFromTopAnimation());
                    flipper.setOutAnimation(AccordionAnimation.outToBottomAnimation());
                    flipper.showNext();
                }
                else if ((diff > 100) && (currentButton.getId() == R.id.btn_next))
                {
                    //Bottom --> Up
                    flipper.setInAnimation(AccordionAnimation.inFromBottomAnimation());
                    flipper.setOutAnimation(AccordionAnimation.outToTopAnimation());
                    flipper.showPrevious();
                }
                break;
            }
        }
        currentButton = null;
        return true;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event)
    {
        currentButton = (Button)v;
        final boolean result = this.onButtonTouchEvent(event);
        return result;
    }
}

AccordionAnimation.java //for the up and down sliding

import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;

public class AccordionAnimation
{
    public static Animation inFromBottomAnimation()
    {
        Animation inFromBottom =
                new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f,
                        Animation.RELATIVE_TO_PARENT, +1.0f, Animation.RELATIVE_TO_PARENT, 0.0f);
        inFromBottom.setDuration(350);
        inFromBottom.setInterpolator(new AccelerateInterpolator());
        return inFromBottom;
    }

    public static Animation outToTopAnimation()
    {
        Animation outtoTop =
                new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f,
                        Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, -1.0f);
        outtoTop.setDuration(350);
        outtoTop.setInterpolator(new AccelerateInterpolator());
        return outtoTop;
    }

    public static Animation inFromTopAnimation()
    {
        Animation inFromTop =
                new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f,
                        Animation.RELATIVE_TO_PARENT, -1.0f, Animation.RELATIVE_TO_PARENT, 0.0f);
        inFromTop.setDuration(350);
        inFromTop.setInterpolator(new AccelerateInterpolator());
        return inFromTop;
    }

    public static Animation outToBottomAnimation()
    {
        Animation outtoBottom =
                new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f,
                        Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, +1.0f);
        outtoBottom.setDuration(350);
        outtoBottom.setInterpolator(new AccelerateInterpolator());
        return outtoBottom;
    }
}

strings.xml //inside res/values

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="goto_first">Slide down to see the first panel</string>
    <string name="goto_second">Slide up to see the second panel</string>
    <string name="loremipsum1">Lorem ipsum dolor sit amet...</string>
    <string name="loremipsum2">Sed ut perspiciatis unde omnis iste...</string>
    <string name="app_name">AccordionSample</string>
</resources>

Edit
If you'd like to split your different views into different layout xmls, say

  1. first_view.xml and
  2. second view.xml,

you should modify your onCreate method:

flipper = (ViewFlipper)findViewById(R.id.flipper);
LayoutInflater inflater = getLayoutInflater();

final View firstView = inflater.inflate(R.layout.first_view, flipper, false);
flipper.addView(firstView);

final View secondView = inflater.inflate(R.layout.second_view, flipper, false);
flipper.addView(secondView);

firstView.findViewById(R.id.btn_next).setOnTouchListener(this);
secondView.findViewById(R.id.btn_prev).setOnTouchListener(this);

Edit
or more elegantly and efficiently, simply just include the layout xmls you've created for the different views into your main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent">
    <ViewFlipper android:id="@+id/flipper" 
        android:layout_width="fill_parent" android:layout_height="fill_parent">
        <include android:id="@+id/flipping_view_1" layout="@layout/flipping_view_1" />
        <include android:id="@+id/flipping_view_2" layout="@layout/flipping_view_2" />
    </ViewFlipper>
</RelativeLayout>

where

  • the layout/flipping_view_1.xml
    contains the first RelativeLayout,
    and
  • the layout/flipping_view_2.xml
    contains the second RelativeLayout

of the ViewFlipper inside your original main.xml.

To learn more about the reusability of android layouts, and layout techniques overall, you should take a look at Romain Guy's great post covering layout tricks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文