Handling click events in ViewPager with negative page margin

非 Y 不嫁゛ 提交于 2019-12-21 22:21:11

问题


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).

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.

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.

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

Layout of the card:

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

<dimen name="card_width">300dp</dimen>
<dimen name="card_height">200dp</dimen>

回答1:


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

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



来源:https://stackoverflow.com/questions/17649216/handling-click-events-in-viewpager-with-negative-page-margin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!