9

Sorry about my dumb title, I will describe it clearly below:

Situation

I have a ViewPager with 4 OnBoardingFragments inside. Each Fragment have exactly same layout which was inflatedfrom same xml file. This layout contain a Button which I called btnNext and I set the OnClickListener for it.

Function getItem of my PagerAdapter

@Override
public Fragment getItem(int position) {
    String title, description, button;
    int resource;
    boolean end = false;
    switch (position) {
        case 0:
            title = context.getString(R.string.on_boarding_title_1);
            description = context.getString(R.string.on_boarding_description_1);
            resource = R.drawable.on_boarding_bg_0;
            button = context.getString(R.string.on_boarding_button_1);
            break;
        case 1:
            title = context.getString(R.string.on_boarding_title_2);
            description = context.getString(R.string.on_boarding_description_2);
            resource = R.drawable.on_boarding_bg_1;
            button = context.getString(R.string.on_boarding_button_2);
            break;
        case 2:
            title = context.getString(R.string.on_boarding_title_3);
            description = context.getString(R.string.on_boarding_description_3);
            resource = R.drawable.on_boarding_bg_2;
            button = context.getString(R.string.on_boarding_button_3);
            break;
        default:
            title = context.getString(R.string.on_boarding_title_4);
            description = context.getString(R.string.on_boarding_description_4);
            resource = R.drawable.on_boarding_bg_3;
            button = context.getString(R.string.on_boarding_button_4);
            end = true;
    }
    return OnBoardingFragment.newInstance(title, description, resource, button, end);
}

@Override
public int getCount() {
    return 4;
}

Functions of OnBoardingFragment:

public static OnBoardingFragment newInstance(String title, String description, int resource,
                                             String button, boolean end) {
    Bundle bundle = new Bundle();
    bundle.putString(EXTRA_TITLE, title);
    bundle.putString(EXTRA_DESCRIPTION, description);
    bundle.putInt(EXTRA_IMAGE, resource);
    bundle.putString(EXTRA_BUTTON, button);
    bundle.putBoolean(EXTRA_END, end);
    OnBoardingFragment fragment = new OnBoardingFragment();
    fragment.setArguments(bundle);
    return fragment;
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Bundle bundle = getArguments();
    if (bundle != null) {
        title = bundle.getString(EXTRA_TITLE);
        description = bundle.getString(EXTRA_DESCRIPTION);
        button = bundle.getString(EXTRA_BUTTON);
        end = bundle.getBoolean(EXTRA_END);
        imageResource = bundle.getInt(EXTRA_IMAGE);
        show log ---> Log.e(this.toString() + "/" + end + "/" + title);
    }
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.on_boarding_fragment, container, false);
    TextView tvTitle = (TextView) rootView.findViewById(R.id.tv_title);
    TextView tvDescription = (TextView) rootView.findViewById(R.id.tv_description);
    TextView btnDiscovery = (TextView) rootView.findViewById(R.id.tv_discovery);
    imageView = (ImageView) rootView.findViewById(R.id.iv_image);
    final Button btnNext = (Button) rootView.findViewById(R.id.btn_next);
    if (end) {
        btnDiscovery.setVisibility(View.VISIBLE);
        btnDiscovery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startRegionActivity();
            }
        });
    }
    btnNext.setText(button);
    btnNext.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            show log ---> Log.e(OnBoardingFragment.this.toString() + "/" + end + "/" + title + "/" + ((Button)v).getText());
            if (end) {
                ((SplashActivity) getActivity()).gotoNextPage();
            } else {
                startLoginActivity();
            }
        }
    });
    tvTitle.setText(title);
    tvDescription.setText(description);
    ImageUtils.loadBitmap(getActivity(), imageResource, imageView, 0.8f, new ImageUtils.LoadBitmapCallback() {
        @Override
        public void onLoadComplete(ImageView imageView) {

        }

        @Override
        public void onLoadFail() {

        }
    });

    return rootView;
}

The problem is

When my app started it show the first Fragment, then I pressed the Button and trigger onItemClick() method, and the method was referenced to the last Fragment.

Log when create Fragment

OnBoardingFragment{a8ede47 id=0x7f10013b}/false/XIN CHÀO!
OnBoardingFragment{8787674 #0 id=0x7f10013b}/false/BÁN LIỀN TAY, KIẾM TIỀN NGAY
OnBoardingFragment{119f79d #1 id=0x7f10013b}/false/CHAT MIỄN PHÍ
OnBoardingFragment{1ee7412 #2 id=0x7f10013b}/true/NGƯỜI THẬT, HÀNG THẬT

Log when onClickListener() was triggered

OnBoardingFragment{1ee7412 #2 id=0x7f10013b}/true/NGƯỜI THẬT, HÀNG THẬT/Đi chợ ngay nào!

Log when the btnNext was initialized:

button: android.support.v7.widget.AppCompatButton{17c69e97 VFED..C. ......I. 0,0-0,0 #7f1002c8 app:id/btn_next}
button: android.support.v7.widget.AppCompatButton{31941c VFED..C. ......I. 0,0-0,0 #7f1002c8 app:id/btn_next}
button: android.support.v7.widget.AppCompatButton{30765b87 VFED..C. ......I. 0,0-0,0 #7f1002c8 app:id/btn_next}
button: android.support.v7.widget.AppCompatButton{9cf19e VFED..C. ......I. 0,0-0,0 #7f1002c8 app:id/btn_next}

Question

Why it happen and how to resolve?

34
  • I would guess your switch is the problem, Log the value received, since you use default for the last element, the int position is probably not the one expected. Did you implement the getCount() methods for the adapter ?
    – AxelH
    Oct 13, 2016 at 8:47
  • @AxelH yes I overrode the getCount() method and simplicity return 4.
    – mr.icetea
    Oct 13, 2016 at 8:55
  • @AxelH I think the switch statement is fine. All the fragments were initialized correctly.
    – mr.icetea
    Oct 13, 2016 at 9:02
  • 1
    Look at this answer. Simply save the currently visible fragment (as a static variable, for example). So when you tap on the button you are always able to get the fragment you are seeing at the moment.
    – Michele
    Oct 13, 2016 at 9:46
  • 1
    Post some more code. With what you shared it's impossibile to help you Oct 17, 2016 at 23:04

5 Answers 5

2
+25

Please refer the sample in the below link for using view pager with multiple fragments: https://guides.codepath.com/android/ViewPager-with-FragmentPagerAdapter

1
  • This not answer to my question.
    – mr.icetea
    Oct 19, 2016 at 7:39
0

When the user clicks on the button, send that event back to the activity (or where you have a reference of the viewpager). From there, you should check what fragment in the viewpager is visible (see Determine when a ViewPager changes pages and How to determine when Fragment becomes visible in ViewPager) and then you will know what to do with that event.

0

you can use a listener interface for getting the fragment and create a method for getting the position and send it on where you have declare the viewpager by 1. first create an anonymous object of that interface in that fragment 2. secondly pass that object to PagerAdapter 3. thirdly you can use that object in the PagerAdapter and call the method which used in the interface with the fragment position 4. you can include the fragment layout handling code on the method inside the anonymous innerclass of that interface

please try this,it will works. hope you can understand what i have said.

3
  • Using listener is a way I did for resolve my problem. But my question is "Why it happen", could you explain it?
    – mr.icetea
    Oct 18, 2016 at 2:53
  • It is because of the boolean value return true and go to the default value, so omit default value and add a case value to 3 include the code of default to it. and set the flag value true in case 3:, hope it will works, try it
    – Jijo
    Oct 18, 2016 at 4:10
  • mr.icetea , please put break in default
    – Jijo
    Oct 18, 2016 at 4:28
0

Use case 3: with a break at the end and in default, just put a single break and no other statement.

0

add break statement in default block and also pass argument end = false in case 0,1,2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.