问题
I have 5 fragments (home, settings, newPost, notifications and profile) inside my Main2Activity
.
Inside HomeFragment
I have a RecyclerView
with a lot of post views, every post containing a "open" button which should open a PostFragment
. The problem is, I can only use setOnClickListener
in RecyclerViewAdapter
class, in populateItemRows() method, so when I can't use 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();
}
});
}
回答1:
You need Context object from construction.
FragmentTransaction fm = ((AppCompatActivity) context).getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.mobile_navigation, newFragment);
transaction.addToBackStack(null);
transaction.commit();
回答2:
*Note this is not recommend
Context context= viewHolder.seePostButton.getContext();
Fragment newFragment = new PostFragment();
FragmentTransaction transaction = ((Main2Activity)context).getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.mobile_navigation, newFragment);
transaction.addToBackStack(null);
transaction.commit();`
or you can use inteface to pass click listener from adapter to fragment and in fragment you can get your activity , check this link for this approach
回答3:
You can achieve using an interface.
public interface OnItemClickListener {
void onItemClick(ContentItem item);
}
You need to modify your adapter to take the listener as the parameter like the one stated below.
private final List<ContentItem> items;
private final OnItemClickListener listener;
public ContentAdapter(List<ContentItem> items, OnItemClickListener listener) {
this.items = items;
this.listener = listener;
}
Now in your onBindViewHolder method, set the click listener.
@Override public void onBindViewHolder(ViewHolder holder, int position) {
holder.bind(items.get(position), listener);
}
public void bind(final ContentItem item, final OnItemClickListener listener) {
...
itemView.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
listener.onItemClick(item);
}
});
}
Now setting the adapter in your RecyclerView.
recycler.setAdapter(new ContentAdapter(items, new ContentAdapter.OnItemClickListener() {
@Override public void onItemClick(ContentItem item) {
Fragment newFragment = new PostFragment();
FragmentTransaction transaction =
getActivity().getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.mobile_navigation, newFragment);
transaction.addToBackStack(null);
transaction.commit();
}
}));
来源:https://stackoverflow.com/questions/61432829/cant-open-a-fragment-from-another-fragment