当前位置:网站首页>gridView自己定义做时间排版「建议收藏」
gridView自己定义做时间排版「建议收藏」
2022-07-07 20:57:00 【全栈程序员站长】
大家好,又见面了,我是全栈君。
公司有需求要做一个时间排版,原型例如以下
由于要用的gridView,曾经就是它的排版非常多,最看是想用一个checkbox搞定。
后来证实功能可以达到。可是排版是乱的。还是老老实实多写点吧(直接上代码)
技术不好什么地方不正确求指正
首先是主页:
public class MyActivity extends Activity {
/**
* Called when the activity is first created.
*/
private HashSet<Integer> shou = new HashSet<Integer>();
private int[] Mark = {1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.button);
final WrapGridView wrapGridView = (WrapGridView) findViewById(R.id.gridview);
final DayTimeAdapter dayTimeAdapter = new DayTimeAdapter(this, Mark);
wrapGridView.setAdapter(dayTimeAdapter);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MyActivity.this, "fdsgs", Toast.LENGTH_SHORT).show();
shou = dayTimeAdapter.remark();
Iterator<Integer> iterator = shou.iterator();
while (iterator.hasNext()) {
Log.i("mark", iterator.next() + "");
}
}
});
}
}
主要是 用来传入表示数组 和接受返回选中标识的
自己定义gridView:
public class WrapGridView extends GridView{
public WrapGridView(Context context) {
super(context);
}
public WrapGridView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public WrapGridView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec=MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE>>2,MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if (ev.getAction()==MotionEvent.ACTION_MOVE){
return true;//禁止滑动
}
return super.dispatchTouchEvent(ev);
}
}
这里是来来禁止滑动 只是 onMeasure方法 只是你后面要在代码里自己设置高宽的时候须要
最后是适配器:
public class DayTimeAdapter extends BaseAdapter{
private ArrayList<Integer> Mark = new ArrayList<Integer>();
private String[] Time = {"8:00", "8:30", "9:00", "9:30", "10:00", "10:30", "11:00", "11:30", "12:00", "12:30", "13:00", "13:30", "14:00", "14:30",
"15:00", "15:30", "16:00", "16:30", "17:00", "17:30", "18:00", "18:30", "19:00", "19:30", "20:00"};
private LayoutInflater mInflater;
private HashSet<Integer> returnMark = new HashSet<Integer>();
public DayTimeAdapter(Context context, int[] m) {
mInflater = LayoutInflater.from(context);
for (int i = 0; i < m.length; i++) {
Mark.add(m[i]);
}
}
@Override
public int getCount() {
return Mark.size();
}
@Override
public Object getItem(int i) {
return Mark.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(final int i, View view, ViewGroup viewGroup) {
DayTimeHolder dayTimeHolder=null;
if (view == null||view.getTag()==null) {
view = mInflater.inflate(R.layout.item_time, null);
dayTimeHolder=new DayTimeHolder();
dayTimeHolder.checkBox = (CheckBox) view.findViewById(R.id.checkBox);
dayTimeHolder.timeName= (TextView) view.findViewById(R.id.textView);
view.setTag(dayTimeHolder);
}else {
dayTimeHolder = (DayTimeHolder) view.getTag();
}
dayTimeHolder.timeName.setText(Time[i]);
int a = Mark.get(i);
if (a == 0) {
view.setBackgroundColor(Color.parseColor("#04385A"));
dayTimeHolder.checkBox.setVisibility(View.INVISIBLE);
dayTimeHolder.checkBox.setClickable(false);
}
final View finalView = view;
final Handler handler = new Handler() {
public void handleMessage(Message msg) {
if (msg.what == 1) {
finalView.setBackgroundColor(Color.parseColor("#F0AB4E"));
// Log.i("mark","shoushoushou");
} else {
finalView.setBackgroundColor(Color.parseColor("#000000"));
}
}
};
dayTimeHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
Message msg = new Message();
if (b) {
msg.what = 1;
Log.i("mark", "111111111111");
returnMark.add(i);
} else {
msg.what = 2;
Log.i("mark", "22222222222222");
returnMark.remove(i);
}
handler.sendMessage(msg);
}
});
return view;
}
public HashSet remark() {
return returnMark;
}
}
class DayTimeHolder{
public CheckBox checkBox;//多选button
public TextView timeName;//时间名称
}
这里就是 直接初始化gridview结构。
里面用到的handler和message是动态更新选中状态的须要啦。
好久没写过这些了,我们这行除了技术就是经验了积累。
唉。怎么越学感觉自己越懒。。
。
。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/116279.html原文链接:https://javaforall.cn
边栏推荐
- Implement secondary index with Gaussian redis
- Can Huatai Securities achieve Commission in case of any accident? Is it safe to open an account
- 程序猿赚的那点钱算个P啊!
- 【函数递归】简单递归的5个经典例子,你都会吗?
- Is embedded system really safe? [how does onespin comprehensively solve the IC integrity problem for the development team]
- Onespin | solve the problems of hardware Trojan horse and security trust in IC Design
- You want to kill a port process, but you can't find it in the service list. You can find this process and kill it through the command line to reduce restarting the computer and find the root cause of
- uva 12230 – Crossing Rivers(概率)「建议收藏」
- Codeforces 474 F. Ant colony
- 机械臂速成小指南(十二):逆运动学分析
猜你喜欢
Ubuntu安装mysql8遇到的问题以及详细安装过程
如何满足医疗设备对安全性和保密性的双重需求?
复杂因子计算优化案例:深度不平衡、买卖压力指标、波动率计算
Small guide for rapid formation of manipulator (12): inverse kinematics analysis
Don't fall behind! Simple and easy-to-use low code development to quickly build an intelligent management information system
Mysql子查询关键字的使用方式(exists)
Nebula Importer 数据导入实践
【C语言】指针进阶---指针你真的学懂了吗?
上海交大最新《标签高效深度分割》研究进展综述,全面阐述无监督、粗监督、不完全监督和噪声监督的深度分割方法
Airiot helps the urban pipe gallery project, and smart IOT guards the lifeline of the city
随机推荐
目标:不排斥 yaml 语法。争取快速上手
微服务远程Debug,Nocalhost + Rainbond微服务开发第二弹
Cantata9.0 | new features
95年专注安全这一件事 沃尔沃未来聚焦智能驾驶与电气化领域安全
guava多线程,futurecallback线程调用不平均
awk处理JSON处理
目前股票开户安全吗?可以直接网上开户吗。
ERROR: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your
Measure the height of the building
Flask1.1.4 werkzeug1.0.1 source code analysis: Routing
[paper reading] maps: Multi-Agent Reinforcement Learning Based Portfolio Management System
Implement secondary index with Gaussian redis
Apifox 接口一体化管理新神器
C语言 整型 和 浮点型 数据在内存中存储详解(内含原码反码补码,大小端存储等详解)
不落人后!简单好用的低代码开发,快速搭建智慧管理信息系统
Helix QAC 2020.2 new static test tool maximizes the coverage of standard compliance
How to meet the dual needs of security and confidentiality of medical devices?
Tensorflow2.x下如何运行1.x的代码
Apifox interface integrated management new artifact
部署、收回和删除解决方式—-STSADM和PowerShell「建议收藏」