[英]How can I open a fragment in a RecyclerView.Adapter to let User go to other users profile
Good day everyone, 今天是个好日子,
I am setting up a social media kinda app. 我正在建立一个社交媒体样的应用程序。 In my "UserAdapter.java"
在我的“ UserAdapter.java”中
The user can chat with other users by tapping longer on the user_item. 用户可以通过在user_item上点击更长的时间来与其他用户聊天。 The chat function work without any problems.
聊天功能正常工作。 I am using "setOnLongClickListener" for this.
我为此使用“ setOnLongClickListener”。 The idea is, that the user can choose, wether he wants to chat by longer clicking or single tab to visit their profile but visiting their profile results in a crash.
这个想法是,用户可以选择是否要通过长时间单击或单击单个标签来访问他们的个人资料来聊天,但是访问他们的个人资料会导致崩溃。 I want to make it something like this:
我想做这样的事情:
Intent intent = new Intent(context, ChatActivity.class);
intent.putExtra("hisUid", hisUid);
context.startActivity(intent);
This is what I use for the Chatfunction with ChatActivity. 这就是我用于带有ChatActivity的Chatfunction的功能。 Fragments are still kinda new to me, so I don't know how to deal with them.
碎片对我来说还是个新事物,所以我不知道该如何处理。 I tried with the same code as in my MainActivity:
我尝试使用与MainActivity中相同的代码:
PersonProfileFragment is the other Users Profile Fragment. PersonProfileFragment是其他用户配置文件片段。
PersonProfileFragment personProfileFragment = new PersonProfileFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.container, personProfileFragment, "");
fragmentTransaction.commit();
Sadly an error show with: Cannot find symbol method "getSupportFragmentManager" 可悲的是显示错误:找不到符号方法“ getSupportFragmentManager”
So what can I do to allow the user to visit the other users profile? 那么我该怎么做才能允许该用户访问其他用户的个人资料?
I've been looking around on Stackoverflow for some solutions but nothing worked yet. 我一直在寻找Stackoverflow上的一些解决方案,但没有任何效果。
public class UserAdapter extends RecyclerView.Adapter { 公共类UserAdapter扩展了RecyclerView.Adapter {
private Context context;
private List<User> userList;
// Constructor
public UserAdapter(Context context, List<User> userList) {
this.context = context;
this.userList = userList;
}
@NonNull
@Override
public MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
// Inflate layout (row_user.xml)
View view = LayoutInflater.from(context).inflate(R.layout.user_item, viewGroup, false);
return new MyHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyHolder myHolder, final int i) {
// Get data
final String hisUid = userList.get(i).getUid();
String userImage = userList.get(i).getImage();
String userName = userList.get(i).getUsername();
final String userFullname = userList.get(i).getFullname();
// Set data
myHolder.mNameTv.setText(userName);
myHolder.mFullnameTv.setText(userFullname);
try {
Picasso.get().load(userImage)
.placeholder(R.drawable.profile)
.into(myHolder.mAvatarIv);
} catch (Exception e) {
}
// Handle item click: Get signed in User to ChatActivity to chat with other User
myHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
/* Click user from user list to start chatting/messaging. Start activity
by putting UID of receiver. We will use that UID to identify the user
we are going to chat with
*/
Intent intent = new Intent(context, ChatActivity.class);
intent.putExtra("hisUid", hisUid);
context.startActivity(intent);
return true;
}
});
// Handle item click: Get signed in User to other User's profile
myHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
@Override
public int getItemCount() {
return userList.size();
}
// View Holder class
class MyHolder extends RecyclerView.ViewHolder {
private ImageView mAvatarIv;
private TextView mNameTv, mFullnameTv;
public MyHolder(@NonNull View itemView) {
super(itemView);
// Init views
mAvatarIv = itemView.findViewById(R.id.avatarIv);
mNameTv = itemView.findViewById(R.id.username_userItem);
mFullnameTv = itemView.findViewById(R.id.fullname_userItem);
}
}
} }
I'll provide any code, that is need, if this isn't enough. 如果还不够的话,我将提供所需的任何代码。
I've tried: 我试过了:
private boolean isFragment;
public UserAdapter(Context context, List<User> userList, boolean isFragment) {
this.context = context;
this.userList = userList;
this.isFragment = isFragment;
}
// Handle item click: Get signed in User to other User's profile
myHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isFragment) {
SharedPreferences.Editor editor = context.getSharedPreferences("SP_USER", Context.MODE_PRIVATE).edit();
editor.putString("hisUid", hisUid);
editor.apply();
((FragmentActivity) context).getSupportFragmentManager().beginTransaction().replace(R.id.container,
new PersonProfileFragment()).commit();
} else {
Intent intent = new Intent(context, DashboardActivity.class);
intent.putExtra("hisUid", hisUid);
context.startActivity(intent);
}
}
});
}
But it gives me an Error: error: constructor UserAdapter in class UserAdapter. 但这给我一个错误:错误:类UserAdapter中的构造函数UserAdapter。 Required: Context, List, boolean.
必需:上下文,列表,布尔值。 found: FragmentActivity, List
找到:FragmentActivity,列表
getSupportFragmentManager
是活动的方法,因此您需要将活动传递给recycler_view适配器并调用: mActivity.getSupportFragmentManager()
以获取片段管理器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.