Imagen de efecto:
1. Primero escuche TextView.
2. Haga que la ventana emergente se muestre debajo de la vista de texto durante el evento de monitoreo.
3. Configure la ventana emergente. El diseño de contenido puede usar la vista de reciclaje o la vista de lista, según las preferencias personales.
1. Primero, debe hacer que se pueda hacer clic en la vista de texto y configurar la supervisión.
//tv_online_count_land 为textview的变量名
tv_online_count_land = findViewById(R.id.tv_online_count_land);
tv_online_count_land.setOnClickListener(this);
2. Hacer frente a los eventos de seguimiento
//使popupwindow放置在textview下方的10像素位置
public void onClick(View v) {
if (v.getId() == R.id.tv_online_count_land) {
showOnlineWindow(v);
if (popupWindow != null && !popupWindow.isShowing()) {
popupWindow.showAsDropDown(v, 10, 10);
}
}
}
3. Establecer ventana emergente
//OnlinePeople为一个对象类
private List<OnlinePeople> onlinelist;
private PopupWindow popupWindow = null;
private void showOnlineWindow(View v){
init();
//popup_onlinepeople为popupwindow的contentview布局
View view = LayoutInflater.from(this).inflate(R.layout.popup_onlinepeople,null);
//recyclerview基本设置
recyclerView = (RecyclerView)view.findViewById(R.id.popup_onlinepeople);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
OnlinePepleAdapter adapter = new OnlinePepleAdapter(onlinelist);
recyclerView.setAdapter(adapter);
//popupWindow = new PopupWindow(view,tv_online_count_land.getWidth()-20, RecyclerView.LayoutParams.WRAP_CONTENT,true);
popupWindow = new PopupWindow(view,tv_online_count_land.getWidth()-20, 180,true);
popupWindow.setFocusable(true);
popupWindow.setOutsideTouchable(true);
//设置popupwindow背景
Drawable drawable = ContextCompat.getDrawable(this, R.drawable.bg_corner_online);
popupWindow.setBackgroundDrawable(drawable);
popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
//关闭窗口
popupWindow.dismiss();
}
});
//popupWindow.showAtLocation(v, Gravity.CENTER, 0, 0);140
}
//初始化数据
private void init(){
onlinelist = new ArrayList<>();
OnlinePeople item = new OnlinePeople("图1.jpg","小刘");
for(int i=0;i<=3;i++){
onlinelist.add(item);
onlinelist.add(item);
}
}
Otros códigos
OnlinePeople.java
//同学类
public class OnlinePeople {
//头像
private String headimg;
//名字
private String name;
//构造函数
public OnlinePeople(String head_uri,String name){
this.head_uri = head_uri;
this.name = name;
}
//获取头像
public String getHeadimg(){
return this.headimg;
}
//获取名字
public String getName(){
return this.name;
}
}
bg_corner_online.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!--设置背景颜色-->
<solid android:color="#ffffff" />
<!-- 设置圆角角度 -->
<corners
android:radius="6dp" />
</shape>
El código de la vista de reciclaje no se muestra aquí. También puede usar la vista de lista en lugar de la vista de reciclaje aquí, el efecto es casi el mismo