Estoy desarrollando una aplicación donde utilizo un navigation drawer que contiene una serie de fragments con una lista, para ello he utilizado un RecyclerView, al seleccionar un ítem de la lista me lleve a otro fragment con texto en su contenido.
Quisiera saber cómo implemento el evento onClickListener teniendo en cuenta los siguientes códigos java.
MainActivity.java
public class MainActivity extends AppCompatActivity {
private DrawerLayout drawerLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
agregarToolbar();
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
if (navigationView != null) {
prepararDrawer(navigationView);
// Seleccionar ítem por defecto
seleccionarItem(navigationView.getMenu().getItem(0));
}
}
private void agregarToolbar() {
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final ActionBar ab = getSupportActionBar();
if (ab != null) {
// Agregar ícono del drawer toggle
ab.setHomeAsUpIndicator(R.drawable.menu);
ab.setDisplayHomeAsUpEnabled(true);
}
}
private void prepararDrawer(NavigationView navigationView) {
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
menuItem.setChecked(true);
seleccionarItem(menuItem);
drawerLayout.closeDrawers();
return true;
}
});
}
private void seleccionarItem(MenuItem itemDrawer) {
Fragment fragmentoGenerico = null;
FragmentManager fragmentManager = getSupportFragmentManager();
switch (itemDrawer.getItemId()) {
case R.id.nav_inicio:
fragmentoGenerico = new FragmentoInicio();
break;
case R.id.nav_cap1:
fragmentoGenerico = new FragmentoCap1();
break;
case R.id.nav_cap2:
fragmentoGenerico = new FragmentoCap2();
break;
case R.id.nav_cap3:
fragmentoGenerico = new FragmentoCap3();
break;
case R.id.nav_cap4:
fragmentoGenerico = new FragmentoCap4();
break;
case R.id.nav_cap5:
fragmentoGenerico = new FragmentoCap5();
break;
case R.id.nav_cap6:
fragmentoGenerico = new FragmentoCap6();
break;
case R.id.nav_cap7:
fragmentoGenerico = new FragmentoCap7();
break;
case R.id.nav_cap8:
fragmentoGenerico = new FragmentoCap8();
break;
case R.id.nav_cap9:
fragmentoGenerico = new FragmentoCap9();
break;
case R.id.nav_cap10:
fragmentoGenerico = new FragmentoCap10();
break;
case R.id.nav_cap11:
fragmentoGenerico = new FragmentoCap11();
break;
case R.id.nav_cap12:
fragmentoGenerico = new FragmentoCap12();
break;
}
if (fragmentoGenerico != null) {
fragmentManager.beginTransaction().replace(R.id.contenedor_principal, fragmentoGenerico).commit();
}
// Setear el título actuar
setTitle(itemDrawer.getTitle());
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
drawerLayout.openDrawer(GravityCompat.START);
return true;
}
return super.onOptionsItemSelected(item);
}
}
FragmentoCap1.java
public class FragmentoCap1 extends Fragment {
private LinearLayoutManager linearLayout;
public FragmentoCap1() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragmento_cap1, container, false);
RecyclerView reciclador = (RecyclerView) view.findViewById(R.id.reciclador);
linearLayout = new LinearLayoutManager(getActivity());
reciclador.setLayoutManager(linearLayout);
AdaptadorListadoCap1 adaptador = new AdaptadorListadoCap1();
reciclador.setAdapter(adaptador);
reciclador.addItemDecoration(new DecoracionLineaDivisoria(getActivity()));
return view;
}
}
AdaptadorListadoCap1.java
public class AdaptadorListadoCap1 extends RecyclerView.Adapter<AdaptadorListadoCap1.ViewHolder> {
public static class ViewHolder extends RecyclerView.ViewHolder {
// Campos respectivos de un ítem
public TextView titulo;
public ViewHolder(View v) {
super(v);
titulo = (TextView) v.findViewById(R.id.tituloListado);
}
}
public AdaptadorListadoCap1() {
}
@Override
public int getItemCount() {
return Articulos.CAP1.size();
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_listado, viewGroup, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
Articulos item = Articulos.CAP1.get(i);
viewHolder.titulo.setText(item.getTitulo());
}
}
Articulos.java
public class Articulos {
private int titulo;
public Articulos(int titulo) {
this.titulo = titulo;
}
public static final List<Articulos> CAP1 = new ArrayList<Articulos>();
static {
CAP1.add(new Articulos(R.string.titulo_art1));
CAP1.add(new Articulos(R.string.titulo_art2));
CAP1.add(new Articulos(R.string.titulo_art3));
CAP1.add(new Articulos(R.string.titulo_art4));
CAP1.add(new Articulos(R.string.titulo_art5));
CAP1.add(new Articulos(R.string.titulo_art6));
}
public int getTitulo() {
return titulo;
}
}
DecoracionLineaDivisoria.java
public class DecoracionLineaDivisoria extends RecyclerView.ItemDecoration {
private Drawable lineaDivisoria;
public DecoracionLineaDivisoria(Context context) {
lineaDivisoria = ContextCompat.getDrawable(context, R.drawable.linea_divisoria);
}
@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
int left = parent.getPaddingLeft();
int right = parent.getWidth() - parent.getPaddingRight();
int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
View child = parent.getChildAt(i);
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
int top = child.getBottom() + params.bottomMargin;
int bottom = top + lineaDivisoria.getIntrinsicHeight();
lineaDivisoria.setBounds(left, top, right, bottom);
lineaDivisoria.draw(c);
}
}
}
Agradezco su ayuda.