在 recyclerview 适配器类中显示自定义弹出窗口 - Android Java
Posted
技术标签:
【中文标题】在 recyclerview 适配器类中显示自定义弹出窗口 - Android Java【英文标题】:Show customize pop up in recycler view adapter class - Android Java 【发布时间】:2021-07-28 01:03:32 【问题描述】:我有一个包含编辑文本、按钮和recycler view
的片段类。我的回收站视图使用card view
的xml 布局。我的卡片视图包含文本视图和一个按钮(?),在被点击后显示一个弹出窗口。
我尝试像往常一样在我的片段类中添加弹出代码,但它指出我的片段无权访问回收者的视图卡视图布局。所以我尝试将它添加到我的适配器类中,但我遇到了getActivty()
无法识别的问题。
这是我在on click
内的view holder
方法中添加的内容
btnHelp.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
Popup_FreedomWall popup_freedomWall = new Popup_FreedomWall();
Bundle args = new Bundle();
System.out.println("=================TVFWID==========================");
System.out.println(tvFwId.getText().toString());
System.out.println("=====================TVFWID======================");
//data to get for report - uid(reported,get reported),freedom wall id
args.putString("user", mod_freedomWall.uid_report);
args.putString("reportedPost", tvFwId.getText().toString());
popup_freedomWall.setArguments(args);
popup_freedomWall.show();
popup_freedomWall.show(getActivity().getSupportFragmentManager(), "My Fragment");
);
```
<br/>
`Mod_FreedomWall.java` 我的片段
public class Mod_FreedomWall extends Fragment
RecyclerView recyclerView;
Mod_FreedomWall_UserAdapter myAdapter;
FragmentManager fragmentManager;
ArrayList<Mod_FreedomWall_User> user;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
View v = inflater.inflate(R.layout.mod_freedomwall, container, false);
recyclerView = (RecyclerView) v.findViewById(R.id.freedomWallPost);
return v;
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState)
super.onViewCreated(view, savedInstanceState);
recyclerView = (RecyclerView) view.findViewById(R.id.freedomWallPost);
recyclerView.setHasFixedSize(true);
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, true);
layoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(layoutManager);
btnPost.setOnClickListener(new View.OnClickListener()
final Date c = Calendar.getInstance().getTime();
final SimpleDateFormat df = new SimpleDateFormat("MM-dd-yy", Locale.getDefault());
final String formattedDate = df.format(c);
final Date currentTime = Calendar.getInstance().getTime();
final DateFormat dateFormat = new SimpleDateFormat("hh.mm aa");
final String dateString = dateFormat.format(new Date());
@Override
public void onClick(View v)
String message = etPost.getText().toString();
createPost(message, user_name, dateString, formattedDate, level);
myAdapter.notifyDataSetChanged();
etPost.setText("");
);
user = new ArrayList<Mod_FreedomWall_User>();
retrievePost();
myAdapter = new Mod_FreedomWall_UserAdapter(this, user);
myAdapter.setListener(new Mod_FreedomWall_UserAdapter.MyListener()
@Override
public void onItemClicked(String uid, String fwid)
Popup_FreedomWall popup_freedomWall = new Popup_FreedomWall();
Bundle args = new Bundle();
//just to see if I'm getting data
System.out.println("=================TVFW==========================");
System.out.println(fwid);
System.out.println("=====================TVFW======================");
args.putString("user",uid);
args.putString("reportedPost", fwid);
popup_freedomWall.setArguments(args);
popup_freedomWall.show(getActivity().getSupportFragmentManager(), "pop_upFreedomWall");
);
recyclerView.setAdapter(myAdapter);
我的适配器
public class Mod_FreedomWall_UserAdapter extends RecyclerView.Adapter<Mod_FreedomWall_UserAdapter.ViewHolder>
Mod_FreedomWall mod_freedomWall = new Mod_FreedomWall();
private ArrayList<Mod_FreedomWall_User> user;
String freeId,reporterId;
public Mod_FreedomWall_UserAdapter(Mod_FreedomWall context, ArrayList<Mod_FreedomWall_User> list)
user = list;
public class ViewHolder extends RecyclerView.ViewHolder
ImageView ivUserProfile;
TextView tv_message,tv_username,tv_date,tvTime,tvFwId;
Button btnHelp;
public ViewHolder(@NonNull View itemView)
super(itemView);
btnHelp = (Button) itemView.findViewById(R.id.btnHelp);
ivUserProfile = (ImageView) itemView.findViewById(R.id.ivUserProfile);
tv_message = (TextView) itemView.findViewById(R.id.tv_message);
tv_username = (TextView) itemView.findViewById(R.id.tv_username);
tv_date = (TextView) itemView.findViewById(R.id.tv_date);
tvTime = (TextView) itemView.findViewById(R.id.tvTime);
tvFwId = (TextView) itemView.findViewById(R.id.tvFwId);
itemView.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
btnHelp.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
System.out.println("=================TVFWID==========================");
System.out.println(tvFwId.getText().toString());
System.out.println("=====================TVFWID======================");
freeId = tvFwId.getText().toString();
reporterId = mod_freedomWall.uid_report;
);
);
@NonNull
@Override
public Mod_FreedomWall_UserAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType)
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.mod_freedomwall_post,viewGroup,false);
return new ViewHolder(v);
@Override
public void onBindViewHolder(@NonNull Mod_FreedomWall_UserAdapter.ViewHolder viewHolder, int position)
viewHolder.itemView.setTag(user.get(position));
viewHolder.tvFwId.setVisibility(View.INVISIBLE);
viewHolder.tv_message.setText(user.get(position).getMessage());
viewHolder.tv_username.setText(user.get(position).getUsername());
viewHolder.tv_date.setText(user.get(position).getDate());
viewHolder.tvTime.setText(user.get(position).getTime());
viewHolder.tvFwId.setText(user.get(position).getFreedomWallId());
viewHolder.ivUserProfile.setImageResource(R.drawable.ic_username_white);
@Override
public int getItemCount()
return user.size();
public interface MyListener
void onItemClicked(String uid,String fwid);
MyListener mListener;
public void setListener(MyListener listener)
mListener = listener;
先感谢您!
【问题讨论】:
【参考方案1】:据我了解,您有两种可能的方法来解决这个问题。
-
在 Adapter 的构造函数中发送一个 Context with,并将需要“getActivity()”的部分替换为您传递的上下文(通过 Adapter 中的变量 obv)。
使用接口方法。
我从不喜欢 Context 样式,所以我会给你一个接口方法的例子。
在您的适配器中:
public abstract class YourAdapter extends ...<>
public interface MyListener
void onItemClicked(String someData); //can change this variable to whatever is needed, if any.
MyListener mListener;
...
然后让按钮的onClickListener调用你刚刚创建的Interface函数,而不是直接尝试做弹窗:
btnHelp.setOnClickListener(v -> onItemClicked("this is my data i pass through");
然后将一个函数添加到您的适配器以设置侦听器(或将其作为参数添加到构造函数中,无论您想要什么):
public void setListener(MyListener listener)
mListener = listener;
并在您的 Fragment 中实现监听器:
adapter.setListener(new YourAdapter.MyListener()
@Override
public void onItemClicked(String myData)
//create your popup and simply call getActivity() since this is inside the Fragment
);
--- 你的片段 ---
public class Mod_FreedomWall extends Fragment
RecyclerView recyclerView;
RecyclerView.Adapter myAdapter; //<-- 1 *remove*
RecyclerView.LayoutManager layoutManager; // obsolete, can be deleted. You make a local variable later on.
Mod_FreedomWall_UserAdapter adapter; // <-- 2 *rename to myAdapter*
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
View v = inflater.inflate(R.layout.mod_freedomwall, container, false);
recyclerView = (RecyclerView) v.findViewById(R.id.freedomWallPost);
return v;
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState)
super.onViewCreated(view, savedInstanceState);
recyclerView = (RecyclerView) view.findViewById(R.id.freedomWallPost);
recyclerView.setHasFixedSize(true);
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, true);
layoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(layoutManager);
// You call a function of `adapter` here, but adapter hasn't been
// properly initialised yet, it's null so far, so that won't go well.
// *rename adapter to myAdapter and move this part down*
adapter.setListener(new Mod_FreedomWall_UserAdapter.MyListener()
@Override
public void onItemClicked(String uid, String fwid)
Popup_FreedomWall popup_freedomWall = new Popup_FreedomWall();
Bundle args = new Bundle();
//data to get for report - uid(reported,get reported),freedom wall id
args.putString("user",uid);
args.putString("reportedPost", fwid);
popup_freedomWall.setArguments(args);
popup_freedomWall.show(getActivity().getSupportFragmentManager(), "My Fragment");
);
btnPost.setOnClickListener(new View.OnClickListener()
final Date c = Calendar.getInstance().getTime();
final SimpleDateFormat df = new SimpleDateFormat("MM-dd-yy", Locale.getDefault());
final String formattedDate = df.format(c);
final Date currentTime = Calendar.getInstance().getTime();
final DateFormat dateFormat = new SimpleDateFormat("hh.mm aa");
final String dateString = dateFormat.format(new Date());
@Override
public void onClick(View v)
String message = etPost.getText().toString();
createPost(message, user_name, dateString, formattedDate, level);
myAdapter.notifyDataSetChanged();
etPost.setText("");
);
user = new ArrayList<Mod_FreedomWall_User>();
retrievePost();
//Here you initialise your adapter
myAdapter = new Mod_FreedomWall_UserAdapter(this, user);
//you can move your `myAdapter.setListener` to this spot.
recyclerView.setAdapter(myAdapter);
好吧,您声明了两个适配器,但实际上只使用了一个。好像有点乱。
待办事项;
报废其中一个适配器(编号 1)。那么最简单的方法是将编号 2 的 adapter
重命名为 myAdapter
并将 adapter.setListener
的 adapter
更改为 myAdapter.setListener(...
。
然后将myAdapater.setListener(...
移动到myAdapter = new Mod_FreedomWall_UserAdapter(this, user);
下方的位置,因为那是您实际初始化适配器的位置。在此之前,您无法安全地调用适配器的函数。
【讨论】:
您好,谢谢您的回答。我尝试按照您的说明在我的代码中实现它,但我在最后一部分迷失了,即在我的片段中实现侦听器。那里的适配器是什么?我应该把代码放在哪里?在onViewCreated
?抱歉,我是 android 新手。
"adapter" 是您在 Fragment 中初始化以附加到 RecyclerView 的适配器变量。某处您可能有类似“recyclerView.setAdapter(adapter)”的行,括号之间的部分是我的目标,您可能已将其命名为其他名称 - 将我的示例中的 adapter
替换为您使用的名称.是的,只要您的adapter
在此之前已初始化,您就可以将它放在onViewCreated
中。如果您仍然有问题,那么您可能希望将您的片段代码添加到您的帖子中,以便我们更好地查看:)
我在最后一部分仍然有问题。这就是我所做的,我使用了myAdapter.setListener(new adapter.MyListener()
,它在整行中都给了我一个错误,所以我尝试了adapter.setListener(new Mod_FreedomWall_UserAdapter.MyListener()
,它没有在编译器中显示错误,但是当我运行应用程序时,它说 MyListerner 为空。附言。我在上面添加了我的片段代码:)
我在您的代码中使用 cmets 编辑了我的帖子,并在哪里进行了更改。希望能有所帮助
我现在发现了我的错误,你的指示很棒,只是我没有得到它。我的第一个错误是我在单击按钮btnHelp
时没有在MyListener void onItemClicked(String uid,String fwid);
中传递任何内容,您可以在上面看到我的错误,因为我也发布了My Adapter class
。然后我的第二个错误是在片段中。谢谢你,它有很大帮助!以上是关于在 recyclerview 适配器类中显示自定义弹出窗口 - Android Java的主要内容,如果未能解决你的问题,请参考以下文章
InvocationTargetException 在适配器类中膨胀视图
Recyclerview 项目在适配器中应用条件时未在 cardview 中显示数据