У меня есть activity с navigation drawer и тремя фрагментами. В одном из них естьrecycler view. Надо сделать так, чтобы принажатии на его элементы октрывался новый фрагмент. Как это сделать? Пытаюсь через адаптер прописать
public FragmentManager f_manager;
public MessagesAdapter(Context mContext, ArrayList<MessagesObject> MessagesArrayList, ChatFragment chatFragment , FragmentManager f_manager) {
this.mContext = mContext;
this.f_manager = f_manager;
this.chatFragment = chatFragment;
this.MessagesArrayList = MessagesArrayList;
}
но тогда в родном фрагменте начинает подсвечивать строка
mAdapter = new MessagesAdapter(getContext(), chatRoomArrayList);
Как это исправить? (полные коды в спойлерах)
Java родного фрагмента
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_messages, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.my_recycler_view);
chatRoomArrayList = new ArrayList<>();
mAdapter = new MessagesAdapter(getContext(), chatRoomArrayList);
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
//recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(mAdapter);
recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), LinearLayoutManager.VERTICAL));
recyclerView.setHasFixedSize(true);
prepareMovieData();
return view;
}
Код адаптера
public class MessagesAdapter extends RecyclerView.Adapter {
private Context mContext;
private ChatFragment chatFragment;
private MessagesFragment context;
private ArrayList MessagesArrayList;
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView name, description, timestamp, count;
public ImageView avator;
public ViewHolder(View view) {
super(view);
avator = (ImageView)itemView.findViewById(R.id.item_chat_avatar);
name = (TextView) itemView.findViewById(R.id.dialog_title);
description = (TextView) itemView.findViewById(R.id.dialog_message);
timestamp = (TextView) itemView.findViewById(R.id.item_chat_date);
count = (TextView) itemView.findViewById(R.id.item_chat_unread_count);
}
}
public FragmentManager f_manager;
//in your constructor add FragmentManager
public MessagesAdapter(Context mContext, ArrayList MessagesArrayList, ChatFragment chatFragment , FragmentManager f_manager) {
this.mContext = mContext;
this.f_manager = f_manager;
this.chatFragment = chatFragment;
this.MessagesArrayList = MessagesArrayList;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_messege, parent, false);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ChatFragment newFragment = new ChatFragment();
f_manager.beginTransaction()
.replace(R.id.content_frame, new ChatFragment())
.commit();
}
});
return new ViewHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
MessagesObject songs = MessagesArrayList.get(position);
Glide.with(mContext).load(R.drawable.a_avator).into(holder.avator);
holder.name.setText(songs.getMessagesName());
holder.description.setText(songs.getMessagesDescription());
holder.timestamp.setText(songs.getMessagesTime());
holder.count.setText(songs.getMessagesUnread());
}
@Override
public int getItemCount() {
return MessagesArrayList.size();
}
}