简体   繁体   English

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

[英]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.

相关问题 如何使用 AsyncTask、RecyclerView 和 RecyclerView.Adapter 将手机中的图像获取到 RecyclerView? - How can I use AsyncTask, RecyclerView, and a RecyclerView.Adapter to get images from my phone into my RecyclerView? 如何在 RecyclerView.Adapter 类中创建警报对话框构建器 - How can i create an Alert Dialog Builder in a RecyclerView.Adapter class 如何在 RecyclerView.Adapter 中使用 onActivityResult - How to use onActivityResult in RecyclerView.Adapter 如何从同一片段中的其他 recyclerview 适配器刷新 recyclerview 适配器 - How to refresh recyclerview adapter from other recyclerview adapter in same fragment 将片段作为参数传递给 RecyclerView.Adapter 是不好的做法吗? 帮助点击 - Is it bad practice to pass a fragment as argument to RecyclerView.Adapter? To help onClick 将数据从 RecyclerView.Adapter 传递给 onClick 片段 - Passing data from RecyclerView.Adapter to fragment onClick 什么是 RecyclerView.Adapter<MyAdapter.MyViewHolder> 以及它与 Android 中的 RecyclerView.Adapter 有何不同? - What is RecyclerView.Adapter<MyAdapter.MyViewHolder> and how it is different from RecyclerView.Adapter in Android? Android从中创建自定义RecyclerView.Adapter并创建其他类 - Android Create a Custom RecyclerView.Adapter and Create Other Class from it RecyclerView.Adapter类查询 - RecyclerView.Adapter Class Query 无法识别我在 RecyclerView.Adapter 中创建的方法 - Cannot recognize a method I created in RecyclerView.Adapter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM