【问题标题】:Ghosting Effect: Two views on top of each other. Why?重影效果:两个视图相互叠加。为什么?
【发布时间】:2014-10-10 02:21:43
【问题描述】:

我希望能够单击按钮在多个视图中向前和向后导航,以及在视图之间向左或向右滑动。

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

这是我的代码:

布局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,但这里是视图发生情况的屏幕截图。两个视图在彼此之上。一个视图固定在屏幕上,另一个视图正确滚动。

我不确定为什么会发生这种情况。是什么导致我的代码中发生这种情况?

编辑:这是一个突出问题的视频:https://www.dropbox.com/s/6x5qa16xyttzrwa/VIDEO0041.mp4?dl=0

【问题讨论】:

  • 我想建议您采用一种水平线性布局,将所有 4 个按钮(即保存、忽略...通过保持线性布局的底部真实来查看寻呼机希望这会对您有所帮助
  • 你能举个例子吗?你的意思是使用horizo​​ntalScrollView?我应该在按钮上方的父布局中包含 viewpager 吗?

标签: java android android-viewpager onclicklistener


【解决方案1】:

改变

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

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

更新:

看完你的电影后,你的问题是你在浏览器的顶部放了一些静态楔子,所以删除它,这意味着你的布局将变成这样:

这是您的 main_activity.xml,您必须通过函数 setContentView 将其分配给您的 activity

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

【讨论】:

  • 在这种情况下,无法解析父级。你的意思是返回视图 == 对象;?
  • 顺便说一句,问题仍然存在:(
  • 为什么不用fragment?
  • 因为我想要几页不同的东西。然后使用片段,我将有 20 个片段。这更容易。
  • 我爱你@mmlooloo!有用。这个问题困扰我很久了,只有你一个人解决了。太感谢了!对此,我真的非常感激!我希望我能以某种方式回报你。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多