繁体   English   中英

如何在ViewFlipper中添加单击事件

[英]How to add a click event in ViewFlipper

我正在尝试设计至少10张图像的视角鳍状肢。 现在,如果用户单击图像,则每个图像都有其自己的活动。我如何获得不同图像的不同单击事件。 我的意思是,如果image1在屏幕上,并且用户单击屏幕,则单击toast image1,对于单击了image2 toast image2,依此类推。等等。

public class PaperSelectionActivity extends Activity implements OnClickListener 
{
    ViewFlipper page;
    LinearLayout lyt;

    Animation animFlipInForeward;
    Animation animFlipOutForeward;
    Animation animFlipInBackward;
    Animation animFlipOutBackward;

    /* Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_paper_selection);

        page = (ViewFlipper)findViewById(R.id.flipper);

        animFlipInForeward = AnimationUtils.loadAnimation(this, R.anim.flipin);
        animFlipOutForeward = AnimationUtils.loadAnimation(this, R.anim.flipout);
        animFlipInBackward = AnimationUtils.loadAnimation(this, R.anim.flipin_reverse);
        animFlipOutBackward = AnimationUtils.loadAnimation(this, R.anim.flipout_reverse);   
    }

    private void SwipeRight(){
        page.setInAnimation(animFlipInBackward);
        page.setOutAnimation(animFlipOutBackward);
        page.showPrevious();
    }

    private void SwipeLeft(){
        page.setInAnimation(animFlipInForeward);
        page.setOutAnimation(animFlipOutForeward);
        page.showNext();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        return gestureDetector.onTouchEvent(event);
    }

    SimpleOnGestureListener simpleOnGestureListener = new SimpleOnGestureListener(){
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) 
        {
            float sensitvity = 50;
            if((e1.getX() - e2.getX()) > sensitvity){
                SwipeLeft();
            }else if((e2.getX() - e1.getX()) > sensitvity){
                SwipeRight();
            }
            return true;
        }
    };

    GestureDetector gestureDetector = new GestureDetector(simpleOnGestureListener);

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.paper_selection, menu);
        return true;
    }
}

XML档案:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/background"
    android:orientation="vertical">

    <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"
            android:orientation="vertical">

            <ImageView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:src="@drawable/image1"/>

        </RelativeLayout>

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical">
            <ImageView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:src="@drawable/image2"/>

        </RelativeLayout>

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical">
            <ImageView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:src="@drawable/image3"/>

        </RelativeLayout>

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical">

            <ImageView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:src="@drawable/image4"/>  

        </RelativeLayout>

    </ViewFlipper>
</LinearLayout>

冒着鼓励过早优化的风险,我解决了一个与此非常相似的问题(翻转一组可能包含数十个或多或少相同的页面),结果很好,尽管可以间接地解决您的问题。

我的技术基础是让ViewFlipper包含一个子页面。 当用户单击“上一个”或“下一个”时,将发生以下情况:

  1. 实例化一个特殊的View,以“克隆” ViewFlipper的真实子视图的图像。
  2. 将克隆视图添加到ViewFlipper,并将其设置为当前子级,不添加任何动画。 到目前为止,UI似乎没有任何变化。
  3. 使用要翻页的文本和图像等更新真实的子视图(现在已被副本遮挡)。
  4. 将ViewFlipper的当前页面设置回其真实的子视图,这一次带有一个漂亮的动画。

我需要加倍努力,因为我的页面非常复杂...几个列表,各种按钮,图像等。如果在我的视图层次结构中只有2个或3个,那将是非常浪费的,更不用说几十个了。 上面的技术可以最大程度地降低布局的复杂度,并且如果页面很复杂,则翻转动画的运行速度也要快得多。 您可以在那里找到更多细节

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM