Viewpager PageTranformer 阻止 UI 更改某些设备中的 onclick

Viewpager PageTranformer blocks UI changes onclick in some devices

我使用了扩展 Viewpager 的自定义 ViewPager。我已经使用手势检测器来执行滚动事件。

这是我的代码,

public class VerticalViewPager extends ViewPager {
    private GestureDetector gestureDetector;
    public boolean isScrollEvent;
    public VerticalViewPager(Context context) {
        super(context);
        init();
    }

    public VerticalViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        gestureDetector=new GestureDetector(getContext(),new GestureListener());
        setPageTransformer(true, new VerticalPageTransformer());
        setOverScrollMode(OVER_SCROLL_NEVER);
    }


    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        boolean res=gestureDetector.onTouchEvent((ev));
        Log.d("event", "interceotontouch      " + res);
        return res;

    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        boolean res= gestureDetector.onTouchEvent((ev));
        if((ev.getAction() == MotionEvent.ACTION_CANCEL ||ev.getAction()==MotionEvent.ACTION_UP)) {
            if(isScrollEvent) {
                try {
                    endFakeDrag();
                } catch (Exception e) {}
            }
            return true;
        }
        Log.d("event", "ontouch      "+res);
        return res;
    }

    public void setScrollDuration(int duration) {
        mScroller.setScrollDuration(duration);
    }




    private final class GestureListener extends GestureDetector.SimpleOnGestureListener {
        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            isScrollEvent=false;
           Log.d("touch","singletap");
            return false;
        }
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
           Log.d("touch","fling");
            isScrollEvent=false;
            try {
                float diffY = e2.getY() - e1.getY();
                if (diffY > 20) {
                    onSwipeDown();
                    return  false;
                } else if(diffY<-20){
                    onSwipeUp();
                    return  false;
                }
                else
                {
                    endFakeDrag();
                    int id = getCurrentItem();
                    setCurrentItem(id, true);
                }


            } catch (Exception exception) {
                exception.printStackTrace();
            }
            return false;

        }
        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2,
                                float distanceX, float distanceY) {
         Log.d("touch", "scroll" + " " + distanceX);
            beginFakeDrag();
            fakeDragBy(-distanceY);
            isScrollEvent=true;
            return true;

        }
    }
    public void onSwipeUp() {
        setCurrentItem(getCurrentItem() + 1);

    }
    public void onSwipeDown() {
        setCurrentItem(getCurrentItem() - 1);

    }
}

这是我的 PageTranformer,

public class VerticalPageTransformer implements android.support.v4.view.ViewPager.PageTransformer {
    @Override
    public void transformPage(View view, float position) {
        final float MIN_SCALE = 0.85f;
         final float MIN_ALPHA = 0.5f;

                if (position < -1) {
                    view.setAlpha(0);
                } else if (position <= 0) { // [-1,0]
                    view.setAlpha(1);
                    view.setTranslationX(-1 * view.getWidth() * position);
                    float yPosition = position * view.getHeight();
                    view.setTranslationY(yPosition);
                    view.setScaleX(1);
                    view.setScaleY(1);
                } else if (position <= 1) { // (0,1]
                    view.setAlpha(1 - position);
                    view.setTranslationX(-1 * view.getWidth() * position);
                    float scaleFactor = MIN_SCALE + (1 - MIN_SCALE) * (1 - Math.abs(position));
                    view.setScaleX(scaleFactor);
                    view.setScaleY(scaleFactor);
                } else {
                    view.setAlpha(0);
                }
    }

}

一切正常。但问题是,当我点击当前页面中的某些内容时,onClick() event invoked。但它 doesn't update 的观点。更改页面后视图会更新。

这是我的适配器,

public class MainViewPagerAdapter extends PagerAdapter {

    private Context context;
    private List<NHBean> newslist = new ArrayList<>();
    ImageLoader imageLoader;
    List<String> list = new ArrayList<String>();
    SharedPreferences preferences;
    SimpleDateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    SimpleDateFormat displayFormat = new SimpleDateFormat("MMM dd, hh:mm a");
    public MainViewPagerAdapter(Context context) {
        this.context = context;
        preferences = context.getSharedPreferences("NewsUpdatePref", Context.MODE_PRIVATE);
        imageLoader = new ImageLoader(context);
        String serialized = preferences.getString("BOOKMARKS", null);
        if (serialized != null)
            list.addAll(Arrays.asList(TextUtils.split(serialized, ",")));

    }

    public void addItems(List<NHBean> newslist) {
        this.newslist.clear();
        this.newslist.addAll(newslist);
        notifyDataSetChanged();
    }

    @Override
    public Object instantiateItem(final ViewGroup collection, int position) {
        LayoutInflater inflater = LayoutInflater.from(context);
        final NHBean nhBean = newslist.get(position);
        Log.d("ImageURL"+position,nhBean.getImageUrl());
        ViewGroup layout;
        layout= (ViewGroup) inflater.inflate(R.layout.page, collection, false);


        TextView text = (TextView) layout.findViewById(R.id.text);
        TextView timeText = (TextView) layout.findViewById(R.id.time_text);
        TextView textHead = (TextView) layout.findViewById(R.id.text_head);
        ImageView shareNews = layout. findViewById(R.id.share_news);
        ImageView firstPage = layout. findViewById(R.id.first_page);
        final ImageView bookmark=layout.findViewById(R.id.bookmark);
        firstPage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((MainActivity)context).moveToFirstPage();
            }
        });
        shareNews.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //code for share
                }
            }
        });
        bookmark.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("clicked","yes");
                if (list.contains(nhBean.getDate())) {
                    list.remove(nhBean.getDate());
                    bookmark.setImageResource(R.drawable.bookmark_not_added);
                } else {
                    bookmark.setImageResource(R.drawable.bookmark_added);
                    list.add(nhBean.getDate());
                }
                preferences.edit().putString("BOOKMARKS", TextUtils.join(",", list)).apply();

            }
        });

        try {
            timeText.setText(displayFormat.format(dateTimeFormat.parse(nhBean.getDate())));
        } catch (Exception e) {
        }

       text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((MainActivity) context).setToolBarVisibility();

            }
        });

        if (list.contains(nhBean.getDate())) {
            bookmark.setImageResource(R.drawable.bookmark_added);
        } else {
            bookmark.setImageResource(R.drawable.bookmark_not_added);
        }
        textHead.setText(nhBean.getHeadline());
        text.setText(nhBean.getContent() + "\n");

        textHead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, WebViewActivity.class);
                intent.putExtra("text_head", nhBean.getHeadline());
                intent.putExtra("news_url", nhBean.getNewsUrl());
                context.startActivity(intent);
            }
        });
        collection.addView(layout);
        return layout;
    }

    @Override
    public void destroyItem(ViewGroup collection, int position, Object view) {
        collection.removeView((View)view);
    }

    @Override
    public int getCount() {
        return newslist.size();
    }

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

    @Override
    public CharSequence getPageTitle(int position) {

        return null;
    }

    public NHBean getItem(int position) {
        return newslist.get(position);
    }


}

当我单击这些按钮时,将调用 onClick()。看书签按钮onclick,setImageResource()没做。但令人惊讶的是,当我滚动时,按钮图像发生了变化。

编辑:

我的 MainActivity 代码,

    mainViewPager = findViewById(R.id.flip_view);
    mainViewPagerAdapter = new MainViewPagerAdapter(this);
    mainViewPager.setAdapter(mainViewPagerAdapter);
    mainViewPager.setScrollDuration(300);
    mainViewPager.addOnPageChangeListener(new VerticalViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}

        @Override
        public void onPageSelected(int position) {

            if (position != 0 || !toolbar.getTitle().toString().equals(getString(R.string.allnews))) {
                swipeRefreshLayout.setEnabled(false);
            } else
                swipeRefreshLayout.setEnabled(true);

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });
    updateNews();

以及方法代码,

public void updateNews() {
    try {
        final ArrayList<NHBean> headlines_1 = DBHelper.getInstance(this).getHeadlines();
        mainViewPagerAdapter.addItems(headlines_1);
        swipeRefreshLayout.setRefreshing(false);
    } catch (Throwable e) {
        e.printStackTrace();
    }

}

而且每个页面的启动动画在选择页面时都会卡住。

救救我!!!提前致谢

尝试在 onClick() 调用后调用 notifyDataSetChanged()

像这样重写您的 PagerAdapter 中的 getItemPosition:

public int getItemPosition(Object object) {
    return POSITION_NONE;
}

这样,当您调用 notifyDataSetChanged() 时,视图分页器将删除所有视图并重新加载它们。这样就得到了reload的效果。

问题不在于代码,

我在清单中使用了android:hardwareAccelerated="false"。经过大量研究,我发现了这个。

我刚改了,

android:hardwareAccelerated="true"

解决了我的问题。