Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FragmentSlide.Builder.fragment() accepts only FragmentSlide.Fragment as parameter #1

Closed
panospcm opened this issue Feb 20, 2016 · 7 comments

Comments

@panospcm
Copy link

First of all, Awesome plugin! But i want to create a FragmentSlider and i want to add button listeners to my fragment layout. But i get a null reference error. How do i "load" the fragment elements (and listeners) inside the current FragmentSlider ?

@janheinrichmerker
Copy link
Owner

Are you using FragmentSlide with a layout resource or with a custom Fragment?
Normally if you're using a custom Fragment you should override the Fragment's onCreateView() and inflate/find all views there and then add click listeners or such.

@panospcm
Copy link
Author

Thank you for your reply, congrats on your development skills.
Could you maybe show me an example with a custom Fragment? Because in your documentation I only see a layout resource example.

This is my current code snippet:
addSlide(new FragmentSlide.Builder()
.background(R.color.md_blue_grey_100)
.backgroundDark(R.color.md_blue_grey_600)
.fragment(R.layout.fragment_login, R.style.AppTheme)
.build());

All i want is to create an onclicklistener for a button inside R.layout.fragment_login.

@janheinrichmerker
Copy link
Owner

First create a LoginFragment class extending Fragment as described here:
http://developer.android.com/training/basics/fragments/creating.html#Create
(You'll just need to follow the steps in chapter 'Create a Fragment Class'.)

To use click events from a button inside your LoginFragment refer to this guide:
http://developer.android.com/training/basics/firstapp/starting-activity.html#RespondToButton
(Again, just follow the steps in 'Respond to the Send Button'.)

Then add your LoginFragment to the intro like this:

addSlide(new FragmentSlide.Builder()
                .background(R.color.md_blue_grey_100)
                .backgroundDark(R.color.md_blue_grey_600)
                .fragment(new LoginFragment())
                .build());

I hope this explanation helps you!

@janheinrichmerker janheinrichmerker changed the title Help! FragmentSlider fragment elements don't work How to add custom fragments? Feb 21, 2016
@janheinrichmerker janheinrichmerker self-assigned this Feb 21, 2016
@panospcm
Copy link
Author

Yes, i tried what you suggested and i get an error 'Cannot resolve method 'fragment(LoginFragment)' .
The LoginFragment is not the issue here, I think, because it works if i load it in another activity.

@janheinrichmerker
Copy link
Owner

Can you post your LoginFragment code and the exact code where you add the slide here?

@panospcm
Copy link
Author

LoginFragment.java :

public class LoginFragment extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private OnFragmentInteractionListener mListener;

    public LoginFragment() {
        // Required empty public constructor
    }

    // TODO: Rename and change types and number of parameters
    public static LoginFragment newInstance(String param1, String param2) {
        LoginFragment fragment = new LoginFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_login, container, false);
    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }
}

Now, MaterialIntroActivity.java (as seen on your repo)

public class MaterialIntroActivity extends IntroActivity {

    public static final String EXTRA_FULLSCREEN = "com.heinrichreimersoftware.materialintro.demo.EXTRA_FULLSCREEN";
    public static final String EXTRA_CUSTOM_FRAGMENTS = "com.heinrichreimersoftware.materialintro.demo.EXTRA_CUSTOM_FRAGMENTS";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Intent intent = getIntent();

        boolean fullscreen = intent.getBooleanExtra(EXTRA_FULLSCREEN, false);
        boolean customFragments = intent.getBooleanExtra(EXTRA_CUSTOM_FRAGMENTS, false);

        setFullscreen(fullscreen);

        super.onCreate(savedInstanceState);

        addSlide(new SimpleSlide.Builder()
                .title(R.string.title_material_metaphor)
                .description(R.string.description_material_metaphor)
                .image(R.drawable.art_material_metaphor)
                .background(R.color.color_material_metaphor)
                .backgroundDark(R.color.color_dark_material_metaphor)
                .build());
        addSlide(new SimpleSlide.Builder()
                .title(R.string.title_material_bold)
                .description(R.string.description_material_bold)
                .image(R.drawable.art_material_bold)
                .background(R.color.color_material_bold)
                .backgroundDark(R.color.color_dark_material_bold)
                .build());
        addSlide(new SimpleSlide.Builder()
                .title(R.string.title_material_motion)
                .description(R.string.description_material_motion)
                .image(R.drawable.art_material_motion)
                .background(R.color.color_material_motion)
                .backgroundDark(R.color.color_dark_material_motion)
                .build());
        addSlide(new SimpleSlide.Builder()
                .title(R.string.title_material_shadow)
                .description(R.string.description_material_shadow)
                .image(R.drawable.art_material_shadow)
                .background(R.color.color_material_shadow)
                .backgroundDark(R.color.color_dark_material_shadow)
                .build());

        if(customFragments){
            addSlide(new FragmentSlide.Builder()
                    .background(R.color.color_custom_fragment_1)
                    .backgroundDark(R.color.color_dark_custom_fragment_1)
                    .fragment(new LoginFragment())  // **## ERROR IS HERE**
                    .build());
            addSlide(new FragmentSlide.Builder()
                    .background(R.color.color_custom_fragment_2)
                    .backgroundDark(R.color.color_dark_custom_fragment_2)
                    .fragment(R.layout.fragment_custom_2, R.style.AppTheme)
                    .build());
        }

        //Feel free to add and remove page change listeners to request permissions or such
        /*
        addOnPageChangeListener(new ViewPager.OnPageChangeListener(){
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        */
    }
}

@janheinrichmerker
Copy link
Owner

Now I see the issue. Currently FragmentSlide accepts only FragmentSlide.Fragments as parameter. I'll fix this soon.

@janheinrichmerker janheinrichmerker changed the title How to add custom fragments? FragmentSlide.Builder.fragment() accepts only FragmentSlide.Fragment as parameter Feb 21, 2016
@janheinrichmerker janheinrichmerker added this to the Release 1.1 milestone Feb 21, 2016
CMircea added a commit to TaxiBarby/material-intro that referenced this issue Oct 10, 2020
Catch OutOfMemoryError and set imageView GONE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants