【核心问题在于PopupWindow上下文环境不知道怎么写?】
onClick()中的View popview = LayoutInflater.from(mcontext).inflate(R.layout.popupwindow_layout, null, false);
的from()参数
和onCreateViewHolder()中的View view= LayoutInflater.from(mcontext).inflate(R.layout.listview_item,parent,false);
的上下文是同一个上下文。
可以写parent.getContext()
,也可以写个mcontext类成员变量。
ItemAdapter.java:RecyclerView的自定义Adapter
public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ViewHolder> {
private List<Item> mitemList;
private Context mcontext;
static class ViewHolder extends RecyclerView.ViewHolder{...}
public ItemAdapter(Context context,List<Item> itemList)
{
this.mitemList=itemList;
this.mcontext=context;
}
@Override
public int getItemCount() {...}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view= LayoutInflater.from(mcontext).inflate(R.layout.listview_item,parent,false);
final ViewHolder viewHolder=new ViewHolder(view);
final View popview=LayoutInflater.from(mcontext).inflate(R.layout.popupwindow_layout,null,false);
....
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {...}
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.listview_item,parent,false);
final ViewHolder viewHolder=new ViewHolder(view);
final View popview=LayoutInflater.from(parent.getContext()).inflate(R.layout.popupwindow_layout,null,false);
final PopupWindow popupWindow=new PopupWindow(
popview,
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
true
);
viewHolder.itemBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
popupWindow.showAtLocation(popview,Gravity.CENTER,0,0);
}
});
popview.findViewById(R.id.close).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
popupWindow.dismiss();
}
});
return viewHolder;
}
效果:
要设置弹出窗口中文本根据点击不同的条目会有不同显示。
【核心问题:找不到textView的R.id】
不知道为什么它能显示别的控件的R.id的选项,但不会显示我们想要的TextView的R.id.content。只有你自己打出来才行。
【核心问题:setText()方法没有】
分开写才行,合在一起写就没有。
TextView textView=popview.findViewById(R.id.content);
textView.setText("hello");
> ItemAdapter.java
```java
package com.example.myapplication;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class ItemAdapter extends RecyclerView.Adapter {
private List- mitemList;
private Context mcontext;
static class ViewHolder extends RecyclerView.ViewHolder
{
TextView itemName;
TextView itemProgress;
Button itemBtn;
public ViewHolder(View view)
{
super(view);
itemName=view.findViewById(R.id.name);
itemProgress=view.findViewById(R.id.progress);
itemBtn=view.findViewById(R.id.show);
}
}
public ItemAdapter(Context context,List
- itemList)
{
this.mitemList=itemList;
this.mcontext=context;
}
@Override
public int getItemCount() {
return mitemList.size();
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view= LayoutInflater.from(mcontext).inflate(R.layout.listview_item,parent,false);
final ViewHolder viewHolder=new ViewHolder(view);
final View popview=LayoutInflater.from(mcontext).inflate(R.layout.popupwindow_layout,null,false);
final PopupWindow popupWindow=new PopupWindow(
popview,
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
true
);
viewHolder.itemBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int pos=viewHolder.getAdapterPosition();
TextView textView=popview.findViewById(R.id.content);
switch (pos)
{
case 0:
{
textView.setText("hello");
break;
}
case 1:
{
textView.setText("world");
break;
}
default:
break;
}
popupWindow.showAtLocation(popview,Gravity.CENTER,0,0);
}
});
popview.findViewById(R.id.close).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
popupWindow.dismiss();
}
});
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
Item item=mitemList.get(position);
holder.itemName.setText(item.getName());
holder.itemProgress.setText(item.getProgress());
}
}