简体   繁体   English

如何在ViewFlipper中添加单击事件

[英]How to add a click event in ViewFlipper

I am trying to design a view flipper with atleast 10 images. 我正在尝试设计至少10张图像的视角鳍状肢。 Now each image has its own activity to go to, if the user clicks on the image.How can i get differen click events for different images. 现在,如果用户单击图像,则每个图像都有其自己的活动。我如何获得不同图像的不同单击事件。 i mean if image1 is on the screen and user clicks the screen, toast image1 clicked, for image2 toast image2 clicked and so on.. Here is what I have done. 我的意思是,如果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 File: 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>

At the risk of encouraging premature optimization, I've solved a problem very similar to this (flipping a set of potentially dozens of more or less identical pages) which turned out quite nicely, and which would solve your problem albeit slightly indirectly. 冒着鼓励过早优化的风险,我解决了一个与此非常相似的问题(翻转一组可能包含数十个或多或少相同的页面),结果很好,尽管可以间接地解决您的问题。

The basis of my technique was to have a ViewFlipper containing a single child page. 我的技术基础是让ViewFlipper包含一个子页面。 When the user clicked 'previous' or 'next' the following things would happen: 当用户单击“上一个”或“下一个”时,将发生以下情况:

  1. Instantiate a special View which 'clones' an image of the ViewFlipper's real child view. 实例化一个特殊的View,以“克隆” ViewFlipper的真实子视图的图像。
  2. Add the clone View to the ViewFlipper and set it as the current child without any animation. 将克隆视图添加到ViewFlipper,并将其设置为当前子级,不添加任何动画。 So far the UI looks as if nothing has changed. 到目前为止,UI似乎没有任何变化。
  3. Update the real child view (now obscured by the clone) with the text and images etc for the page being flipped in. 使用要翻页的文本和图像等更新真实的子视图(现在已被副本遮挡)。
  4. Set the ViewFlipper's current page back to its real child view, this time with a nice animation. 将ViewFlipper的当前页面设置回其真实的子视图,这一次带有一个漂亮的动画。

I needed to go this extra mile because my page was very complex... several lists, all sorts of buttons, images etc. It would have been terribly wasteful to have 2 or 3 of them in my view hierarchy, let alone dozens. 我需要加倍努力,因为我的页面非常复杂...几个列表,各种按钮,图像等。如果在我的视图层次结构中只有2个或3个,那将是非常浪费的,更不用说几十个了。 The above technique minimizes your layout complexity, and the flipping animation gets to run a lot faster if the page is complex. 上面的技术可以最大程度地降低布局的复杂度,并且如果页面很复杂,则翻转动画的运行速度也要快得多。 More detail you can wacth there 您可以在那里找到更多细节

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

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