我的Main2Activity
中有5个片段(家庭,设置,newPost,通知和个人资料)。
在HomeFragment
内,我有一个RecyclerView
,其中包含很多帖子视图,每个帖子都包含一个“打开”按钮,应该会打开一个PostFragment
。问题是,我只能在populateItemRows()方法的setOnClickListener
类中使用RecyclerViewAdapter
,所以当我不能使用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();
}
});
}
答案 0 :(得分:1)
您需要从构造中获取Context对象。
FragmentTransaction fm = ((AppCompatActivity) context).getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.mobile_navigation, newFragment);
transaction.addToBackStack(null);
transaction.commit();
答案 1 :(得分:0)
*注意,不推荐
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();`
或者您可以使用界面将Click侦听器从适配器传递到片段,并在片段中获得您的活动,请为此方法检查link
答案 2 :(得分:0)
您可以使用界面来实现。
public interface OnItemClickListener {
void onItemClick(ContentItem item);
}
您需要修改适配器以将侦听器作为参数,如下所述。
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);
}
});
}
现在在您的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();
}
}));