textview + popupwindowはドロップダウンボックス機能を実装します

効果画像:
ここに写真の説明を挿入

1.最初にTextViewをリッスンします
2.リスニングイベントでtextviewの下にポップアップウィンドウを表示します
3.ポップアップウィンドウを設定します。コンテンツレイアウトは、個人的な好みに応じてrecyclerviewまたはlistviewを使用できます

1.まず、テキストビューをクリック可能にして、監視を設定する必要があります。

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

2.監視イベントに対処する

//使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.ポップアップウィンドウを設定します

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

その他のコード

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>

recyclerviewのコードはここには示されていません。recyclerviewの代わりにlistviewを使用することもできます。効果はほぼ同じです。

おすすめ

転載: blog.csdn.net/weixin_43477545/article/details/106932051