我有一个包含回收站视图的片段,在该回收站视图中我有一个弹出菜单,我尝试让其中一个选项启动另一个片段,但我无法让它工作。 我的适配器类:
public class RecyclerAdapterMusico extends RecyclerView.Adapter<RecyclerAdapterMusico.ViewHolder> {
private Context mContext;
private ArrayList<Musico> listaM;
public RecyclerAdapterMusico(Context context, ArrayList<Musico> listaMusicos) {
mContext = context;
listaM = listaMusicos;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View vista;
vista = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_musicos, parent, false);
ViewHolder viewHolder = new ViewHolder(vista);
return viewHolder;
}
@Override
public void onBindViewHolder(final RecyclerAdapterMusico.ViewHolder holder, int position) {
final Musico musicoItem = (Musico) listaM.get(position);
CircleImageView imagenMusico = holder.img;
TextView nom = holder.nombre;
TextView ins = holder.instrumento;
TextView est = holder.estilo;
TextView desc = holder.descripcion;
TextView anun = holder.anuncios;
ImageView busc = holder.buscando;
ImageButton btnMenu = holder.menuButton;
nom.setText(musicoItem.getNombre());
ins.setText(musicoItem.getInstrumento().get(0));
est.setText(musicoItem.getEstilo());
desc.setText(musicoItem.getDescripcion());
anun.setText(String.valueOf(musicoItem.getAnuncio().size()));
btnMenu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//creacion del menu para el cardview
PopupMenu popupMenu = new PopupMenu(mContext, holder.menuButton);
popupMenu.inflate(R.menu.navigation_visitar_perfil);
//Listener del menu
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.itemVisitarPerfil:
Toast.makeText(mContext, "VISITAR PERFIL", Toast.LENGTH_SHORT).show();
break;
case R.id.itemVisitarAnuncios:
/* FragmentManager fm = getSupportFragmentManager();// If you're already inside another fragment
FragmentVisitarPerfil_Anuncios yfObj = new FragmentVisitarPerfil_Anuncios();
fm.beginTransaction().replace(R.id.contenedormivisitarperfil, yfObj).commit();*/
Toast.makeText(mContext, "VISITAR ANUNCIOS", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return false;
}
});
//mostrar menu
popupMenu.show();
}
});
try {
if (musicoItem.getBuscando().equalsIgnoreCase("si")) {
busc.setImageDrawable(mContext.getDrawable(R.drawable.yes));
} else {
busc.setImageDrawable(mContext.getDrawable(R.drawable.no));
}
} catch (NullPointerException ex) {
System.out.println("Sale por aqui en caso de que venga del primer registro");
}
new BDBAA().accesoFotoPerfil("musico",'n',imagenMusico,mContext);
}
@Override
public int getItemCount() {
return listaM.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
CircleImageView img;
ImageView buscando;
TextView nombre, instrumento, estilo, descripcion, anuncios;
ImageButton menuButton;
public ViewHolder(View itemView) {
super(itemView);
//finds de los componentes de los items
img = itemView.findViewById(R.id.imgItemMusico);
buscando = itemView.findViewById(R.id.imgBuscandoItemMusico);
nombre = itemView.findViewById(R.id.txtNombreMusicoItemMusico);
instrumento = itemView.findViewById(R.id.txtInstrumentoItemMusico);
estilo = itemView.findViewById(R.id.txtEstiloItemMusico);
descripcion = itemView.findViewById(R.id.txtDescripcionItemMusico);
anuncios = itemView.findViewById(R.id.txtCantidadAnunciosItemMusico);
menuButton = itemView.findViewById(R.id.btnMenuMusicos);
}
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
}
My Fragment class that contains the recycler:
public class FragmentMusicosTabInicio extends Fragment {
RecyclerView recyclerViewMusicos;
View vista;
ArrayList<Musico> listaMusicos = new ArrayList<>();
public FragmentMusicosTabInicio() {
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
vista = inflater.inflate(R.layout.tab_musicos_fragment, container, false);
recyclerViewMusicos = vista.findViewById(R.id.recyclerMusicos);
new BDBAA().cargarDatos(listaMusicos, recyclerViewMusicos, getActivity(),"musico");
return vista;
}
及其布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/tabmusicos"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
>
<!--PARTE DE LOS ITEMS-->
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/md_white_1000"
android:fitsSystemWindows="true"
android:theme="@style/AppTheme">
<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleTextAppearance="@android:color/transparent"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="@color/md_black_1000"
android:contentDescription="@string/app_name"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"/>
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No olvides tu historia ni tu destino"
android:layout_marginLeft="40dp"
android:layout_marginTop="30dp"
android:textAlignment="center"
android:textColor="@color/cardview_light_background"
android:textSize="20sp"
android:textStyle="bold|italic" />
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bob Marley"
android:layout_marginLeft="280dp"
android:layout_marginTop="60dp"
android:textAlignment="center"
android:textColor="@color/cardview_light_background"
android:textSize="11sp"
android:textStyle="bold|italic" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerMusicos"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</android.support.v7.widget.RecyclerView>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
菜单包含两个项目
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menu_visitar_perfil"
android:title="Ir al perfil"/>
<item
android:id="@+id/menu_visitar_anuncios"
android:title="Ir a anuncios"/>
</menu>
目的是点击&#34; Anuncios&#34;将打开一个属于buttomNavigationView的片段,其中包含另一个recyclerview
这个课是这样的:
public class FragmentAnuncios extends Fragment {
View vista;
FloatingActionButton miFAB;
RecyclerView recyclerViewAnuncios;
Anuncio anuncio;
ArrayList<Anuncio> lista;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
vista = inflater.inflate(R.layout.fragment_anuncios_v_fragment_perfil, container, false);
recyclerViewAnuncios = vista.findViewById(R.id.recycleranuncios);
recyclerViewAnuncios.setNestedScrollingEnabled(false);
lista=new ArrayList<>();
lista.add(new Anuncio("NECESITAMOS BAJISTA ","Hola, somos un grupo que necesita incorporar a un bajista","25/05/2018","Asturias","Comillas","Pop/Rock","Bajista","Mujer"));
lista.add(new Anuncio("NECESITAMOS BAJISTA ","Hola, somos un grupo que necesita incorporar a un bajista","25/05/2018","Asturias","Comillas","Pop/Rock","Bajista","Mujer"));
lista.add(new Anuncio("NECESITAMOS BAJISTA ","Hola, somos un grupo que necesita incorporar a un bajista","25/05/2018","Asturias","Comillas","Pop/Rock","Bajista","Mujer"));
lista.add(new Anuncio("NECESITAMOS BAJISTA ","Hola, somos un grupo que necesita incorporar a un bajista","25/05/2018","Asturias","Comillas","Pop/Rock","Bajista","Mujer"));
lista.add(new Anuncio("NECESITAMOS BAJISTA ","Hola, somos un grupo que necesita incorporar a un bajista","25/05/2018","Asturias","Comillas","Pop/Rock","Bajista","Mujer"));
LinearLayoutManager layoutManager=new LinearLayoutManager(getActivity());
RecyclerView.LayoutManager rcLayoutManager =layoutManager;
recyclerViewAnuncios.setLayoutManager(rcLayoutManager);
RecyclerAdapterAnuncioPropio adapterAnuncio=new RecyclerAdapterAnuncioPropio(getActivity(),lista);
recyclerViewAnuncios.setAdapter(adapterAnuncio);
miFAB = (FloatingActionButton) vista.findViewById(R.id.fabAñadirAnuncio);
miFAB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
android.app.FragmentManager fm = getActivity().getFragmentManager();
FragmentDialogAñadirAnuncio alerta = new FragmentDialogAñadirAnuncio();
alerta.show(fm, "AlertaAnuncio");
}
});
return vista;
}
}
XML布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/anuncios"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.v4.widget.NestedScrollView
android:layout_marginTop="44dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycleranuncios"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</android.support.v4.widget.NestedScrollView>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fabAñadirAnuncio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_marginBottom="5dp"
android:backgroundTint="@color/md_teal_800"
android:src="@android:drawable/ic_input_add"
app:fabSize="normal" />
</android.support.design.widget.CoordinatorLayout>
我只找到一个解决方案来打开片段,如果&#34;父母&#34;活动从AppCompactActivity延伸而不是从Fragment扩展。
任何解决方案?
非常感谢你。