繁体   English   中英

如何在RecyclerView.Adapter中打开片段以使用户转到其他用户配置文件

[英]How can I open a fragment in a RecyclerView.Adapter to let User go to other users profile

今天是个好日子,

我正在建立一个社交媒体样的应用程序。 在我的“ UserAdapter.java”中

用户可以通过在user_item上点击更长的时间来与其他用户聊天。 聊天功能正常工作。 我为此使用“ setOnLongClickListener”。 这个想法是,用户可以选择是否要通过长时间单击或单击单个标签来访问他们的个人资料来聊天,但是访问他们的个人资料会导致崩溃。 我想做这样的事情:

Intent intent = new Intent(context, ChatActivity.class);
intent.putExtra("hisUid", hisUid);
context.startActivity(intent);

这就是我用于带有ChatActivity的Chatfunction的功能。 碎片对我来说还是个新事物,所以我不知道该如何处理。 我尝试使用与MainActivity中相同的代码:

PersonProfileFragment是其他用户配置文件片段。

PersonProfileFragment personProfileFragment = new PersonProfileFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.container, personProfileFragment, "");
fragmentTransaction.commit();

可悲的是显示错误:找不到符号方法“ getSupportFragmentManager”

那么我该怎么做才能允许该用户访问其他用户的个人资料?

我一直在寻找Stackoverflow上的一些解决方案,但没有任何效果。

公共类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);
    }

}

}

如果还不够的话,我将提供所需的任何代码。

我试过了:

 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);
            }

        }
    });
}

但这给我一个错误:错误:类UserAdapter中的构造函数UserAdapter。 必需:上下文,列表,布尔值。 找到:FragmentActivity,列表

getSupportFragmentManager是活动的方法,因此您需要将活动传递给recycler_view适配器并调用: mActivity.getSupportFragmentManager()以获取片段管理器。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM