textview+popupwindow implements drop-down box function

Effect picture:
Insert picture description here

1. First listen to the TextView
2. Make the popupwindow displayed below the textview in the listening event
3. Set the popupwindow, the content layout can use recyclerview or listview, depending on personal preference

1. First, you need to make the textview clickable and set up monitoring.

//tv_online_count_land 为textview的变量名
tv_online_count_land = findViewById(R.id.tv_online_count_land);
tv_online_count_land.setOnClickListener(this);

2. Deal with monitoring events

//使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. Set popupwindow

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

Other codes

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>

The code of recyclerview is not shown here. You can also use listview instead of recyclerview here, the effect is almost the same

Guess you like

Origin blog.csdn.net/weixin_43477545/article/details/106932051