In ActivityMain
my application, I have created a DrawerLayout
and a NavigationView
which works perfectly and within the menu that is displayed I have several links fragment
that change as they are clicked.
So far so good.
I have the problem inside a fragment
. In this fragment
I only show an image that occupies the entire screen and my intention is that when the image is clicked, the DrawerLayout
side menu will be displayed.
I have tried to create an object of the class MainActivity
to call drawerLayout
but this causes an error and I think that this is not the solution.
Right now my code fragment
is:
public class FragmentInicio extends Fragment {
public FragmentInicio() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_inicio, container, false);
ImageView logo = (ImageView) v.findViewById(R.id.imageEccaInicio);
logo.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
Toast.makeText(view.getContext(), "Se ha tocado la imagen", Toast.LENGTH_LONG).show();
//drawerLayout.openDrawer(GravityCompat.START);
}
});
return v;
}
}
I also tried to do this from the MainActivity
but it also produced an error.
A friend listener
You implement it in your MainActivity and pass it as an argument to the fragment
and in the snippet
In the end it turned out easier than I thought.
1º In
MainActivity
it I created a method to open theDrawerLayout
2º In the
Fragment
one where I have the image when the event occursonClick
I have called the method of theMainActivity
Apparently a better way is to create a Listener, but I didn't get it. This is the solution that worked for me.
I hope it helps you, greetings and thanks to Nicol Israel Olvera Acosta