重影效果:对彼此顶部两种观点。为什么? [英] Ghosting Effect: Two views on top of each other. Why?

查看:355
本文介绍了重影效果:对彼此顶部两种观点。为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够点击按钮来向前和向后导航通过几个意见,以及向左滑动或视图之间的权利。

I want to be able to click on buttons to navigate forward and backward through several views as well as swiping left or right between views.

所以我决定实施ViewPager多个视图之间刷卡。

So I decided to implement the ViewPager for swiping between multiple views.

下面是我的code:

布局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"
            android:background="@color/white"
        >

  <android.support.v4.view.ViewPager
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/viewPager"/>

    <ImageView
            android:id="@+id/apple"
            android:layout_width="200sp"
            android:layout_height="150sp"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:src="@drawable/apple"
            android:contentDescription="apple"/>

    <TextView
            android:id="@+id/number"
            android:layout_width="100sp"
            android:layout_height="55sp"
            android:layout_marginTop="47dp"
            android:layout_below="@+id/apple" android:layout_alignStart="@+id/apple"/>

    <Button
            android:id="@+id/save"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/save"
            android:layout_alignTop="@+id/ignore" android:layout_toStartOf="@+id/apple"/>

    <Button
            android:id="@+id/ignore"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/Ignore"
            android:layout_alignParentBottom="true" android:layout_toEndOf="@+id/apple"/>

    <ImageView
            android:id="@+id/back_nav_arrow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_action_back"
            android:contentDescription="back">
            </ImageView>


    <ImageView
            android:id="@+id/forward_nav_arrow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_action_forward"
            android:layout_alignParentTop="true" android:layout_alignParentEnd="true"
            android:contentDescription="forward">

    </ImageView>

</RelativeLayout>

下面是我的活动:

public class CollectionPager extends Activity {

    private PagerAdapter pagerAdapter;
    ActionBar actionbar;
    MyAdapter myAdapter;
    private Context context;
    private TextView textView;
    private int currentPage;
    ViewPager viewPager;
    int progressChanged = 0;

    public static final String TAG = "CollectionPager";

    public CollectionPager() {
        context = this;
    }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        super.setContentView(R.layout.collection);
        viewPager = (ViewPager) findViewById(R.id.viewPager);

        myAdapter = new MyAdapter();
        viewPager.setAdapter(myAdapter);

        ActionBar actionBar = getActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }

        //Initialize the back button and add an onClick event listener to the button
        final ImageView back_button = (ImageView) findViewById(R.id.back_nav_arrow);
        back_button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {

                //it doesn't matter if you're already in the first item
                viewPager.setCurrentItem(viewPager.getCurrentItem() - 1);
            }
        });

        //Initialize the forward button and add an onClick event listener to the button
        final ImageView forward_button = (ImageView) findViewById(R.id.forward_nav_arrow);
        forward_button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {

                //it doesn't matter if you're already in the last item
                viewPager.setCurrentItem(viewPager.getCurrentItem() + 1);
            }
        });

        final Button save_button = (Button) findViewById(R.id.save);
        save_button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {

                //save
            }
        });

        final Button ignore_button = (Button) findViewById(R.id.ignore);
        ignore_button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {

             //ignore

            }
        });

        //Attach the page change listener inside the activity
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            // This method will be invoked when the current page is scrolled
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            //This method will be invoked when a new page becomes selected
            @Override
            public void onPageSelected(int position) {
                //get position
                currentPage = position;

            }

            // Called when the scroll state changes:
            // SCROLL_STATE_IDLE, SCROLL_STATE_DRAGGING, SCROLL_STATE_SETTLING
            @Override
            public void onPageScrollStateChanged(int i) {

                //get state

            }
        });

    }

        private class MyAdapter extends PagerAdapter {


            int NumberOfPages = 10;

            LayoutInflater inflater = (LayoutInflater) CollectionPager.this
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            @Override
            public int getCount() {
                return NumberOfPages;
            }

            @Override
            public Object instantiateItem(ViewGroup parent, int position) {

                   View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.collection, parent, false);

                    ImageView imageView = (ImageView) view
                            .findViewById(R.id.apple);
                    imageView.setImageResource(R.drawable.apple);
                    parent.addView(view,0);


                    return view;
            }

            @Override
            public void destroyItem(ViewGroup parent, int position, Object object) {
                ((ViewPager) parent).removeView((View) object);
            }

            @Override
            public boolean isViewFromObject(View parent, Object object) {
                return parent== ((View) object);
            }

            @Override
            public Parcelable saveState() {
                return null;
            }
        }
    }

时,检测到onClickEvent但这里所发生的事情的看法截图。两个查看在彼此的顶部。一种观点是固定在屏幕上,另一种是正常滚动。

The onClickEvent is detected but here's a screenshot on what is happening to the view. Two view on top of each other. One view is fixed on the screen and the other one is scrolling correctly.

我不知道为什么会这样。是什么原因造成这在我的code发生的?

I'm not sure why this happens. What is causing this to occur in my code?

编辑:这里的突出问题视频:的https:// WWW .dropbox.com / S / 6x5qa16xyttzrwa / VIDEO0041.mp4?DL = 0

Here's a video highlighting the issue: https://www.dropbox.com/s/6x5qa16xyttzrwa/VIDEO0041.mp4?dl=0

推荐答案

修改

   @Override
   public boolean isViewFromObject(View parent, Object object) {
        return parent== ((View) object);
   }

@Override
public boolean isViewFromObject(View v, Object o) {
    return parent ==  object;
}

更新:

在观看电影后,你的问题是,你把你的viewpager顶部一些静态wedget,所以删除,这意味着你的布局将成为这样的事情:

After watching your movie your problem is you put some static wedget on the top of your viewpager, so remove that, which means your layout will become something like this:

这是你的 main_activity.xml 您必须将其分配给您的活动按功能的setContentView

This is your main_activity.xml you must assign it to your activity by function setContentView

<?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"
            android:background="@color/white"
        >

  <android.support.v4.view.ViewPager
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/viewPager"/>

</RelativeLayout>

然后在 instantiateItem 使用以下布局:

<?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"
        android:background="@color/white"
    >

<ImageView
        android:id="@+id/apple"
        android:layout_width="200sp"
        android:layout_height="150sp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:src="@drawable/apple"
        android:contentDescription="apple"/>

<TextView
        android:id="@+id/number"
        android:layout_width="100sp"
        android:layout_height="55sp"
        android:layout_marginTop="47dp"
        android:layout_below="@+id/apple" android:layout_alignStart="@+id/apple"/>

<Button
        android:id="@+id/save"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/save"
        android:layout_alignTop="@+id/ignore" android:layout_toStartOf="@+id/apple"/>

<Button
        android:id="@+id/ignore"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/Ignore"
        android:layout_alignParentBottom="true" android:layout_toEndOf="@+id/apple"/>

<ImageView
        android:id="@+id/back_nav_arrow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_action_back"
        android:contentDescription="back">
        </ImageView>


<ImageView
        android:id="@+id/forward_nav_arrow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_action_forward"
        android:layout_alignParentTop="true" android:layout_alignParentEnd="true"
        android:contentDescription="forward">

</ImageView>

您的主要活动:

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    super.setContentView(R.layout.collection);
    viewPager = (ViewPager) findViewById(R.id.viewPager);

    myAdapter = new MyAdapter();
    viewPager.setAdapter(myAdapter);

    ActionBar actionBar = getActionBar();
    if (actionBar != null) {
        actionBar.hide();
    }

    //Attach the page change listener inside the activity
    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        // This method will be invoked when the current page is scrolled
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        //This method will be invoked when a new page becomes selected
        @Override
        public void onPageSelected(int position) {
            //get position
            currentPage = position;

        }

        // Called when the scroll state changes:
        // SCROLL_STATE_IDLE, SCROLL_STATE_DRAGGING, SCROLL_STATE_SETTLING
        @Override
        public void onPageScrollStateChanged(int i) {

            //get state

        }
    });

}

然后在这个函数分配您的主要活动的点击监听器

then assign your click listener of your main activity at this function

        @Override
        public Object instantiateItem(ViewGroup parent, int position) {

               View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.collection, parent, false);

                ImageView imageView = (ImageView) view
                        .findViewById(R.id.apple);
                imageView.setImageResource(R.drawable.apple);

               final ImageView back_button = (ImageView) view.findViewById(R.id.back_nav_arrow);
               back_button.setOnClickListener(new View.OnClickListener() {
               public void onClick(View view) {

                    //it doesn't matter if you're already in the first item
                    viewPager.setCurrentItem(viewPager.getCurrentItem() - 1);
               }
               });



                parent.addView(view,0);


                return view;
        }

这篇关于重影效果:对彼此顶部两种观点。为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆