Ghosting Effect: Two views on top of each other. Why?
Asked Answered
J

1

0

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.

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

Here's my code:

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>

Here's my activity:

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;
            }
        }
    }

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.

enter image description here

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

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

Jacki answered 8/10, 2014 at 17:24 Comment(2)
i would like to advice you to take one horizontal linear layout in which put your all 4 buttons i.e. save,ignore.... so on and put that linear layout below to view pager or you can put over view pager by keeping bottom true of your linear layout hope this will help youJaye
Can you give me an example? Do you mean to use a horizontalScrollView? Should I include the viewpager inside the parent layout above the buttons?Jacki
O
1

Change

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

to

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

Update:

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:

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>

then at instantiateItem use below layout:

<?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>

your main activity:

 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;
        }
Overnice answered 9/10, 2014 at 9:58 Comment(10)
In this case, parent cannot be resolved. Do you mean return view == object;?Jacki
Btw, still have the problem occuring :(Jacki
why do not you use fragment?Overnice
Because I want to have several pages of different things. Then with fragments, I would have 20 fragments. This is easier.Jacki
This is what is wrong with the app right now: dropbox.com/s/6x5qa16xyttzrwa/VIDEO0041.mp4?dl=0Jacki
Thank you, I will. Btw, I have a question when you say, "then assign your click listener of your main activity at this function" what do you mean? Thanks a lot in advance!Jacki
in your main activity you have some widget like back_button that you assigned it a click listener, i mean those listener. but first you must call findViewById at the instantiateItem to get the reference of themOvernice
Can you provide me with an example of what you mean with one of the buttons e.g. back_button? I wrote this out in InstantiateItem but I am still getting a nullpointer when I setOnClickListener in main activity: ImageView back_button = (ImageView) findViewById(R.id.back_nav_arrow); back_button.setImageResource(R.drawable.ic_action_back);Jacki
ohhh I see. So I don't have to include the onClickListener in my main activity but in my instantiate item?Jacki
I LOVE YOU @mmlooloo! It works. I've been having this problem for so long and you're the only one who solved it. Thank you so much! I really appreciate it! I wish I can repay you somehow. Thank you!Jacki

© 2022 - 2024 — McMap. All rights reserved.