重影效果:两个视图相互叠加。为什么?

时间:2014-10-08 17:24:50

标签: java android android-viewpager onclicklistener

我希望能够点击按钮来向前和向后浏览多个视图,以及在视图之间向左或向右滑动。

所以我决定实现ViewPager在多个视图之间滑动。

这是我的代码:

layout 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但是这里是关于视图发生了什么的屏幕截图。两个视图在彼此的顶部。一个视图固定在屏幕上,另一个视图正确滚动。

enter image description here

我不确定为什么会这样。在我的代码中导致这种情况的原因是什么?

编辑:以下是突出显示问题的视频:https://www.dropbox.com/s/6x5qa16xyttzrwa/VIDEO0041.mp4?dl=0

1 个答案:

答案 0 :(得分:1)

更改

   @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,所以删除它,这意味着你的布局会变成这样:

这是您的main_activity.xml,您必须按功能activity

将其分配给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

        }
    });

}

然后在此功能

指定主要活动的点击监听器
        @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;
        }