单选全选都是大家熟知的了,下面我说说自己的一种单选方法,没有CheckBox的两种状态图的一种实现,这里使用ListView和item中的三个控件来写的,一般单选的都是CheckBox的状态的改变,这里却是根据CheckBox来判断相对布局中的ImageView的显示隐藏,看看效果图:点击下载源码
上面的第一张图就是QQ的界面,第二张是我完成的效果,下面直接看代码:
1.MainActivity函数:
package com.example.administrator.myqqlistview;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import com.example.administrator.myqqlistview.adapter.LoanmarketSortingOrderPopupWindowAdapter;
import com.example.administrator.myqqlistview.bean.CheckBean;
import com.example.administrator.myqqlistview.utils.SelectPicPopupWindow;
import com.example.administrator.myqqlistview.utils.SelectPicPopupsWindow;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
@Override
public void onClick(View view) {
}
private Button bt_king_yes,bt_king_yes_two;
private PopupWindow popupwindow;
private View customScreenView;
private SelectPicPopupsWindow menuWindow;
private SelectPicPopupWindow menuWindow2;
private String[] sortingOrderName;
private List<CheckBean> mDatas;
private Context context;
private LoanmarketSortingOrderPopupWindowAdapter loanmarketSortingOrderPopupWindowAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
context = MainActivity.this;
bt_king_yes = (Button) findViewById(R.id.bt_king_yes);
bt_king_yes_two = (Button) findViewById(R.id.bt_king_yes_two);
sortingOrderName = new String[]{"借入总额升序", "借入总额升序", "需借入升序", "需借入降序", "借入周期升序", "借入周期降序", "日利率升序", "日利率降序"};//排序
mDatas = new ArrayList<CheckBean>();
for (int i = 0; i < sortingOrderName.length; i++) {
if(i == 0){//默认选中第一个
mDatas.add(new CheckBean(sortingOrderName[i],true));
continue;
}
mDatas.add(new CheckBean(sortingOrderName[i],false));
}
bt_king_yes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dismissPopupWindow();
screenPopupWindowView();
//设置在某个控件的下方
popupwindow.showAsDropDown(bt_king_yes, 0, 1);
// popupwindow.showAsDropDown(ll_loanmarket_comprehensive);
/* //实例化SelectPicPopupWindow
menuWindow = new SelectPicPopupWindow(this,itemsOnClick,1);
//显示窗口
menuWindow.showAsDropDown(MainActivity.this.findViewById(R.id.bt_king_yes));*/
}
});
bt_king_yes_two.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// View popView = LayoutInflater.from(MainActivity.this).inflate(
// R.layout.camera_popuwindow, null);
// LinearLayout rootView = (LinearLayout) findViewById(R.id.ll); // 當前頁面的根佈局
// final PopupWindow popupWindow = new PopupWindow(popView, LinearLayout.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
//
// backgroundAlpha(1f);//设置屏幕透明度
//
// popupWindow.setBackgroundDrawable(new BitmapDrawable());
// popupWindow.setFocusable(true);// 点击空白处时,隐藏掉pop窗口
// // 顯示在根佈局的底部
// popupWindow.showAsDropDown(bt_king_yes);
//实例化SelectPicPopupWindow
menuWindow2 = new SelectPicPopupWindow(MainActivity.this, itemsOnClick,1);
//显示窗口
// menuWindow2.showAtLocation(MainActivity.this.findViewById(R.id.bt_king_yes_two), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0); //设置layout在PopupWindow中显示的位置
menuWindow2.showAsDropDown(bt_king_yes_two);
}
});
}
//为弹出窗口实现监听类
private View.OnClickListener itemsOnClick = new View.OnClickListener() {
public void onClick(View v) {
menuWindow.dismiss();
switch (v.getId()) {
case R.id.ll_take_photo:
break;
case R.id.ll_pick_photo:
break;
default:
break;
}
}
};
/**
* popupwindow 显示借贷与质押
*/
public void screenPopupWindowView() {
// // 获取自定义布局文件pop.xml的视图
customScreenView = LayoutInflater.from(this).inflate(R.layout.sorting_order_popupwindow_new, null);//排序
// 创建PopupWindow实例,200,150分别是宽度和高度,设置全屏或匹配大小
popupwindow = new PopupWindow(customScreenView,
ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
// 设置动画效果 [R.style.AnimationFade 是自己事先定义好的]
// popupwindow.setAnimationStyle(R.style.AnimationFade);
// 自定义view添加触摸事件
// popupwindow.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
popupwindow.setOutsideTouchable(false); // 设置是否允许在外点击使其消失,到底有用没?
//点击空白处时,隐藏掉pop窗口
popupwindow.setFocusable(false);
popupwindow.setBackgroundDrawable(new BitmapDrawable());
backgroundAlpha(1f);
//实例化一个ColorDrawable颜色为半透明
ColorDrawable dw = new ColorDrawable(0xb0000000);
//设置SelectPicPopupWindow弹出窗体的背景
//mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
popupwindow.setBackgroundDrawable(dw);
ListView lv_sorting_order = (ListView) customScreenView.findViewById(R.id.lv_sorting_order);
loanmarketSortingOrderPopupWindowAdapter = new LoanmarketSortingOrderPopupWindowAdapter(this,mDatas);
lv_sorting_order.setAdapter(loanmarketSortingOrderPopupWindowAdapter);
AdapterView.OnItemClickListener listItemClickListener = new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> adapterView, View view, final int position, long l) {
// 取得ViewHolder对象,这样就省去了通过层层的findViewById去实例化我们需要的cb实例的步骤
final LoanmarketSortingOrderPopupWindowAdapter.ViewHolder viewHolder = (LoanmarketSortingOrderPopupWindowAdapter.ViewHolder) view.getTag();
viewHolder.cb_screen_pupopwindow.toggle();// 把CheckBox的选中状态改为当前状态的反,gridview确保是单一选中
for (CheckBean bean : mDatas) {//全部设为未选中
bean.setChecked(false);
}
mDatas.get(position).setChecked(true);//点击的设为选中
loanmarketSortingOrderPopupWindowAdapter.notifyDataSetChanged();
}
};
lv_sorting_order.setOnItemClickListener(listItemClickListener);
customScreenView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (popupwindow != null && popupwindow.isShowing()) {
popupwindow.dismiss();
popupwindow = null;
}
return false;
}
});
}
/**
* 设置添加屏幕的背景透明度
*
* @param bgAlpha
*/
public void backgroundAlpha(float bgAlpha) {//WindowManager.LayoutParams lp = ActivityB.getInstance().getMainActivity().getWindow().getAttributes();
WindowManager.LayoutParams lp = this.getWindow().getAttributes();
lp.alpha = bgAlpha; //0.0-1.0
this.getWindow().setAttributes(lp);
}
/**
* 关闭PopupWindow弹窗
*/
private void dismissPopupWindow() {
if (popupwindow != null && popupwindow.isShowing()) {
popupwindow.dismiss();
popupwindow = null;
}
}
}
2.activity_main布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.administrator.myqqlistview.MainActivity">
<Button
android:id="@+id/bt_king_yes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="仿QQ切换账号"/>
<Button
android:id="@+id/bt_king_yes_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="调用相册" />
</LinearLayout>
3.SelectPicPopupsWindow工具类(当然,这个类和单选没有关系,是调用相册用的,Demo里有就写进来吧,希望大家用得到):
package com.example.administrator.myqqlistview.utils;
/**
* Created by LGY on 2018/2/7.
*/
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.example.administrator.myqqlistview.R;
public class SelectPicPopupWindow extends PopupWindow {
private RelativeLayout rl_wallet_btc, rl_wallet_holiday, rl_wallet_loan;
private LinearLayout ll_wallet_back;
private View mMenuView;
private LayoutInflater inflater;
private LinearLayout btn_take_photo, btn_pick_photo, btn_cancel;
private Context context;
private TextView tv_2,tv_3,tv_4,tv_1;
private TextView[] textviewId;
private LinearLayout ll_wallet_back_pass;
private TextView tv_pay_forgetPwd;
public SelectPicPopupWindow(Activity context, OnClickListener itemsOnClick, int sort) {
super(context);
this.context = context;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
setSort(sort,itemsOnClick);
//设置SelectPicPopupWindow的View
this.setContentView(mMenuView);
//设置SelectPicPopupWindow弹出窗体的宽
this.setWidth(LayoutParams.FILL_PARENT);
//设置SelectPicPopupWindow弹出窗体的高
this.setHeight(LayoutParams.WRAP_CONTENT);
//设置SelectPicPopupWindow弹出窗体可点击
this.setFocusable(true);
//设置SelectPicPopupWindow弹出窗体动画效果
this.setAnimationStyle(R.style.AnimationPreview);
//实例化一个ColorDrawable颜色为半透明
ColorDrawable dw = new ColorDrawable(0xb0000000);
//设置SelectPicPopupWindow弹出窗体的背景
this.setBackgroundDrawable(dw);
//mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
mMenuView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
int height = mMenuView.findViewById(R.id.pop_layout).getTop();
int y=(int) event.getY();
if(event.getAction()== MotionEvent.ACTION_UP){
if(y<height){
dismiss();
}
}
return true;
}
});
}
private void setSort(int sort,OnClickListener itemsOnClick){
switch (sort) {
case 1: //为选择图片
mMenuView = inflater.inflate(R.layout.camera_popuwindow, null);
btn_take_photo = (LinearLayout) mMenuView.findViewById(R.id.ll_take_photo);
// btn_take_photo = (LinearLayout) mMenuView.findViewById(R.id.ll_take_photo);
btn_pick_photo = (LinearLayout) mMenuView.findViewById(R.id.ll_pick_photo);
btn_cancel = (LinearLayout) mMenuView.findViewById(R.id.ll_camera_cancel);
//设置按钮监听
btn_pick_photo.setOnClickListener(itemsOnClick);
btn_take_photo.setOnClickListener(itemsOnClick);
break;
case 2://筛选购彩信息
break;
}
//取消按钮
btn_cancel.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//销毁弹出框
dismiss();
}
});
}
}
4.适配器:
package com.example.administrator.myqqlistview.adapter;
import android.app.Activity;
import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import com.example.administrator.myqqlistview.R;
import com.example.administrator.myqqlistview.bean.CheckBean;
import java.util.HashMap;
import java.util.List;
/**
* 排序popupWindow Adapter
* Created by tldev on 2018/3/31.
*/
public class LoanmarketSortingOrderPopupWindowAdapter extends BaseAdapter {
private List<CheckBean> mDatas;
private Activity activity;//上下文
private Context context;
private LayoutInflater inflater;
private String[] sortingOrderName;
private int temp = -1;
//控制CheckBox选中情况
private static HashMap<Integer, Boolean> isSelected;
public LoanmarketSortingOrderPopupWindowAdapter(Activity context, List<CheckBean> mDatas) {
this.activity=context;
this.sortingOrderName = sortingOrderName;
isSelected = new HashMap<Integer, Boolean>();
this.mDatas = mDatas;
inflater = LayoutInflater.from(context);
// initData();
}
// private void initData() {//初始化isSelected的数据
// for (int i = 0; i < sortingOrderName.length; i++) {
// getIsSelected().put(i, false);
// }
// }
@Override
public int getCount() {
return null == mDatas ? 0 : mDatas.size();
}
@Override
public String getItem(int position) {
return mDatas.get(position).getStr();
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null) {
convertView = inflater.inflate(R.layout.screen_popupwindow_item, null);
viewHolder = new ViewHolder();
// viewHolder.tv_screen_pupopwindow = convertView.findViewById(R.id.tv_screen_pupopwindow);
viewHolder.cb_screen_pupopwindow = (CheckBox) convertView.findViewById(R.id.cb_screen_pupopwindow);
viewHolder.im_screen_pupopwindow = (ImageView) convertView.findViewById(R.id.im_screen_pupopwindow);
viewHolder.rl_screen_pupopwindow = (RelativeLayout) convertView.findViewById(R.id.rl_screen_pupopwindow);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.cb_screen_pupopwindow.setText(mDatas.get(position).getStr());
if(mDatas.get(position).isChecked()){//状态选中
viewHolder.cb_screen_pupopwindow.setChecked(true);
viewHolder.im_screen_pupopwindow.setVisibility(View.VISIBLE);
// finalHolder.cb_screen_pupopwindow.setTextColor(Color.BLUE);
viewHolder.cb_screen_pupopwindow.setTextColor(activity.getResources().getColor(
R.color.blue_one));//设置字体颜色
viewHolder.rl_screen_pupopwindow.setBackgroundColor(ContextCompat.getColor(activity, R.color.fengexian_one));
}else{
viewHolder.cb_screen_pupopwindow.setChecked(false);
viewHolder.cb_screen_pupopwindow.setChecked(true);
viewHolder.im_screen_pupopwindow.setVisibility(View.INVISIBLE);
// finalHolder.cb_screen_pupopwindow.setTextColor(Color.BLUE);
viewHolder.cb_screen_pupopwindow.setTextColor(activity.getResources().getColor(
R.color.black_one));//设置字体颜色
viewHolder.rl_screen_pupopwindow.setBackgroundColor(ContextCompat.getColor(activity, R.color.colorWhite));
}
// String s = list.get(position);
// viewHolder.tv_lm_pledge.setText(list.get(position));
// String address = wallet.getAddress();
final ViewHolder finalViewHolder = viewHolder;
// viewHolder.rl_screen_pupopwindow.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View view) {
// GlobalConfig.getSid3(activity);
tv_loanmarket_complete.setText(sortingOrderName[position]);
// if (finalViewHolder.cb_screen_pupopwindow.isChecked()) {
// finalViewHolder.im_screen_pupopwindow.setVisibility(View.VISIBLE);
finalHolder.cb_screen_pupopwindow.setTextColor(Color.BLUE);
// finalViewHolder.cb_screen_pupopwindow.setTextColor(activity.getResources().getColor(
// R.color.blue_one));//设置字体颜色
Resources resources = getContext().getResources();
Drawable btnDrawable = resources.getDrawable(R.drawable.layout_bg);
layout.setBackgroundDrawable(btnDrawable);
// finalViewHolder.rl_screen_pupopwindow.setBackgroundColor(ContextCompat.getColor(activity, R.color.fengexian_one));
// if (temp != -1) {
// //找到上次点击的checkbox,并把它设置为false,对重新选择时可以将以前的关掉
// CheckBox tempCheckBox = (CheckBox) activity.findViewById(temp);
// if (tempCheckBox != null)
// tempCheckBox.setChecked(false);
// }
temp = compoundButton.getId();//保存当前选中的checkbox的id值
//
//
// } else {
// finalViewHolder.im_screen_pupopwindow.setVisibility(View.INVISIBLE);
// }
//
popupwindow.dismiss();
// }
// });
// 根据isSelected来设置checkbox的选中状况
// viewHolder.cb_screen_pupopwindow.setChecked(getIsSelected().get(position));
// 根据isSelected来设置checkbox的选中状况
viewHolder.cb_screen_pupopwindow.setId(position);//对checkbox的id进行重新设置为当前的position
// viewHolder.cb_screen_pupopwindow.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
// @Override
// public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
// if (isChecked) {//实现checkbox的单选功能,同样适用于radiobutton
// finalViewHolder.im_screen_pupopwindow.setVisibility(View.VISIBLE);
finalHolder.cb_screen_pupopwindow.setTextColor(Color.BLUE);
// finalViewHolder.cb_screen_pupopwindow.setTextColor(activity.getResources().getColor(
// R.color.blue_one));//设置字体颜色
// if (temp != -1) {
// //找到上次点击的checkbox,并把它设置为false,对重新选择时可以将以前的关掉
// CheckBox tempCheckBox = (CheckBox) activity.findViewById(temp);
// if (tempCheckBox != null)
// tempCheckBox.setChecked(false);
// }
// temp = compoundButton.getId();//保存当前选中的checkbox的id值
// }else {
// temp = -1;
// finalViewHolder.im_screen_pupopwindow.setVisibility(View.INVISIBLE);
finalHolder.cb_screen_pupopwindow.setTextColor(Color.BLUE);
// finalViewHolder.cb_screen_pupopwindow.setTextColor(activity.getResources().getColor(
// R.color.black_one));//设置字体颜色
// }
// }
// });
if (position == temp)//比对position和当前的temp是否一致
viewHolder.cb_screen_pupopwindow.setChecked(true);
else
viewHolder.cb_screen_pupopwindow.setChecked(false);
return convertView;
}
public static class ViewHolder {
public CheckBox cb_screen_pupopwindow;
public ImageView im_screen_pupopwindow;
public RelativeLayout rl_screen_pupopwindow;
}
// public static HashMap<Integer, Boolean> getIsSelected() {
// return isSelected;
// }
// public static void setIsSelected(HashMap<Integer, Boolean> isSelected) {
// LoanmarketSortingOrderPopupWindowAdapter.isSelected = isSelected;
// }
}
5.适配器布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="@color/colorWhite"
android:orientation="vertical"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/rl_screen_pupopwindow"
android:layout_width="match_parent"
android:layout_height="44dp"
android:gravity="center"
android:orientation="horizontal">
<CheckBox
android:id="@+id/cb_screen_pupopwindow"
android:layout_width="wrap_content"
android:textColor="@color/gray_two"
android:layout_height="match_parent"
android:button="@null"
android:focusable="false"
android:layout_marginLeft="15dp"
android:textSize="14sp"
android:background="@null"
android:gravity="center"
android:text="风控规则"/>
<!--<TextView-->
<!--android:id="@+id/tv_screen_pupopwindow"-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="match_parent"-->
<!--android:text="风控规则"-->
<!--android:layout_marginLeft="15dp"-->
<!--android:gravity="center"-->
<!--android:textColor="@color/gray_two"-->
<!--android:textSize="14sp" />-->
<ImageView
android:id="@+id/im_screen_pupopwindow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:visibility="invisible"
android:src="@mipmap/paly_icon_choose"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_marginRight="15dp"
/>
</RelativeLayout>
</LinearLayout>
颜色值和其它的就不写了,可以直接下载Demo,里面都有的
点击下载源码