У меня есть 5 фрагментов (дома, настройки, newPost, уведомления и профиль) внутри моего Main2Activity
.
Внутри HomeFragment
У меня есть RecyclerView
с большим количеством просмотров сообщений, каждое сообщение содержит кнопку "Открыть", которая должна открыть PostFragment
. Проблема в том, что я могу использовать только setOnClickListener
в RecyclerViewAdapter
классе, в методе populateItemRows (), поэтому, когда я не могу использовать getActivity()
..
HomeFragment:
public class HomeFragment extends Fragment{
private RecyclerView recyclerView;
private RecyclerViewAdapter recyclerViewAdapter;
private boolean isLoading = false;
private ArrayList<String> rowsArrayList = new ArrayList<>();
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_home, container, false);
recyclerView = root.findViewById(R.id.recyclerView);
initAdapter();
initScrollListener();
return root;
}
private void initAdapter() {
recyclerViewAdapter = new RecyclerViewAdapter(getActivity(),2);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setAdapter(recyclerViewAdapter);
}
private void initScrollListener() {
...
}
private void loadMore() {
...
}
}
RecyclerViewAdapter:
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private final int VIEW_TYPE_ITEM = 0;
private final int VIEW_TYPE_LOADING = 1;
private ArrayList<Post> list = new ArrayList();
private static final String TAG = RecyclerViewAdapter.class.getSimpleName();
private String userId = FirebaseAuth.getInstance().getCurrentUser() != null ? FirebaseAuth.getInstance().getCurrentUser().getUid() : null;
private FirebaseFirestore db = FirebaseFirestore.getInstance();
private Context context;
private User currentUser;
private int type;
public RecyclerViewAdapter(Context context, int type) {
this.context = context;
this.type = type;
if (type == 0) {
myPostsRetriever(context);
} else if (type == 1) {
appliedToPostsRetriever(context);
} else if (type == 2) {
homePostsRetriever(context);
}
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
...
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
if (viewHolder instanceof ItemViewHolder) {
populateItemRows((ItemViewHolder) viewHolder, position);
} else if (viewHolder instanceof LoadingViewHolder) {
showLoadingView((LoadingViewHolder) viewHolder, position);
}
}
private void populateItemRows(ItemViewHolder viewHolder, int position) {
...
viewHolder.itemView.findViewById(R.id.seePostButton).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Fragment newFragment = new PostFragment();
FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.mobile_navigation, newFragment);
transaction.addToBackStack(null);
transaction.commit();
}
});
}