处理ViewPager中带有负页边距的点击事件 [英] Handling click events in ViewPager with negative page margin
问题描述
我有一个ViewPager,其中显示用户可以在其间滑动的卡片".卡是在它们自己的片段中创建的(因此,我正在使用FragmentStatePagerAdapter来创建ViewPager的内容).
I have a ViewPager showing "cards" that the user can swipe between. The cards are created in their own fragments (so I am using a FragmentStatePagerAdapter to create the content for the ViewPager).
我想显示下一张和上一张卡的边缘,因此,按照本网站上的建议,我将ViewPager的pageMargin设置为负数.这很好用,看起来也和我想要的一样.
I wanted to show the edge of the next and possibly previous cards, so as recommended on this site I set the pageMargin of the ViewPager to a negative number. This works well and looks just as I want it to.
我的问题是我需要响应卡片上的点击事件.但是使用负的pageMargin会导致页面重叠,有时会导致错误的页面接收到click事件.如何确定正确的卡收到了单击事件?基本上,我想要的是只有当前/选定的页面才能收到点击事件.
My problem is that I need to respond to click events on the cards. But using a negative pageMargin causes the pages to overlap, sometimes resulting in the wrong page receiving the click event. How can I make sure the click events are received by the correct card? Basically what I want is that only the current/selected page will receive click events.
在包含viewPager的主片段中:
From my main fragment containing the viewPager:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_cards_overview, null);
mCardsViewPager = (ViewPager) root.findViewById(R.id.cards_viewpager);
mPagerAdapter = new MyPagerAdapter(getActivity(), getFragmentManager(), mCardsList);
mCardsViewPager.setAdapter(mPagerAdapter);
mCardsViewPager.setOnPageChangeListener(mPagerAdapter);
// Necessary or the pager will only have one extra page to show
// make this at least however many pages you can see
mCardsViewPager.setOffscreenPageLimit(3);
// Set margin for pages as a negative number, so a part of next and
// previous pages will be showed (convert desired dp to pixels)
DisplayMetrics m = getResources().getDisplayMetrics();
int pagerWidthPx = (int) getResources().getDimension(R.dimen.card_width);
int screenWidthPx = m.widthPixels;
int px = (int) ((screenWidthPx - pagerWidthPx) * 1.6);
mCardsViewPager.setPageMargin(-px);
return root;
}
卡的布局:
<RelativeLayout
android:id="@+id/card_content"
android:layout_width="@dimen/card_width"
android:layout_height="@dimen/card_height"
android:background="@drawable/card_background" >
<ImageView
android:id="@+id/card_logo"
android:layout_width="50dp"
android:layout_height="35dp"
android:layout_margin="8dp"
android:contentDescription="@string/app_name"
android:scaleType="fitStart"
android:src="@drawable/card_logo" />
<Button
android:id="@+id/card_remove_button"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_margin="3dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="@drawable/btn_close" />
<TextView
android:id="@+id/card_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@id/card_logo"
android:layout_centerVertical="true"
android:layout_margin="8dp"
android:textSize="20sp" />
</RelativeLayout>
dimens.xml:
dimens.xml:
<dimen name="card_width">300dp</dimen>
<dimen name="card_height">200dp</dimen>
推荐答案
我想我自己解决了这个问题.我在PagerAdapter中实现了以下方法:
I think I solved this myself. I implemented the following method in my PagerAdapter:
@Override
public void setPrimaryItem(ViewGroup container, int position, Object object) {
super.setPrimaryItem(container, position, object);
if(object instanceof CardFragment){
CardFragment f = (CardFragment) object;
View v = f.getView();
if(v != null){
v.bringToFront();
}
}
}
这有助于将当前选定的片段放在其他片段的前面.但是在创建分片之前,它也被调用很多,因此进行了null-check.不确定是否有任何性能问题,但这似乎很完美
This helped put the currently selected Fragment in front of the others. But it gets called a lot, also before the Fragments are created, hence the null-check. Not sure if there are any performance-issues with this but it seems to be perfect
这篇关于处理ViewPager中带有负页边距的点击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!