ListView嵌套两个EditText相关显示问题
这里说明:本人第一次写博客,可能写的不算太好。可是这个相关类型的研究与拓展,是项目中比較难得的。所以开一篇博客来总结和思考。先让我们看看项目需求。
项目需求说明:
1、须要在点击EditText的时候显示Button
2、点击其它条目的EditText的时候,必须在当前条目中显示Button,其它条目中的Button必须隐藏
3、滚动ListView的时候不可以显示错乱信息和错乱显示
4、在滚动的时候。假设条目不显示。那么必须隐藏Button
Content:
注意:布局文件就不须要细致去看了,主要还是依据需求来写相应的代码。完毕功能就好。
/**我们就看这个适配器里面的内容,在listView显示和ListView适配的时候,适配器始终是一个重要的相关内容。我们把适配器提取出来,细致研究究竟是怎么做到适配的。
*/
public class ProductManagerAdapter extends BaseAdapter {
private List<Products> products;//数据源
private Context context;//上下文
private Handler handler;//主线程载体
//private static int temp = -1;//控件静态id来保持单一性
private static ViewHolder tempHolder = null;//控件静态重用对象。用来保持单一性
public ProductManagerAdapter(List<Products> products, Context context,
String type, Handler handler) {//构造方法,传递外界数据以及相应的上下文信息
super();
this.products = products;
this.context = context;
this.handler = handler;
}
public void updataAdapter(List<Products> products) {//公开方法,用来动态改变显示条目
if (products != null) {
this.products = products;
notifyDataSetChanged();
}
}
@Override
public int getCount() {
return products == null ? 0 : products.size();
}
@Override
public Object getItem(int position) {
return products.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final Products product = products.get(position);
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = View.inflate(context, R.layout.item_products_manager,
null);
holder.price_save = (TextView) convertView
.findViewById(R.id.price_save);
holder.product_chengben = (EditText) convertView
.findViewById(R.id.product_chengben);
holder.product_price = (EditText) convertView
.findViewById(R.id.product_price);
holder.product_chengben.setTag(position); //保存相应的条目位置,防止被重用
holder.product_price.setTag(position); //保存相应的条目位置。防止被重用
**savingPosition**(holder, holder.product_chengben, "chengben", position);//重要方法A~參见后面具体分析
**savingPosition**(holder, holder.product_price, "price", position);
convertView.setTag(holder);
holder.price_save.setVisibility(View.GONE);//条目创建的时候为了摆脱焦点控制机制,所以使用强制性质要求不可以显示button
} else {
holder = (ViewHolder) convertView.getTag();
loseFoucus(holder);//失去焦点
holder.product_price.setTag(position);
holder.product_chengben.setTag(position);
holder.price_save.setVisibility(View.GONE);//滑动过程中,让显示的条目消失
}
return convertView;
}
class ViewHolder {
/** 商品价格 */
EditText product_price;
/** 商品成本 */
EditText product_chengben;
/** 保存按钮 */
TextView price_save;
}
在这之前的代码是完整的实现listView显示的控制代码。里面分别的集中几种方法非常重要,必须单独拧出来谈。
下面是重要方法A:savingPosition,下面我们具体分析。
/** edittext公共操作
* @param position **/
private void savingPosition(ViewHolder holder, final EditText ev,
final String type, int position) {
ev.addTextChangedListener(new ThisWatcher(holder) {//为EditText做监听
@Override
public void afterTextChanged(Editable s, ViewHolder holder) {
int p = (Integer) ev.getTag();//获取相应的position
savaData(p, s.toString(), type, holder);//重要方法B~參见后面具体分析
}
});
ev.setOnFocusChangeListener(new ForcusChangeDate(holder));//重要监听~參见后面具体分析
}
重要方法B:在保存相应的EditText信息中。使得EditText的内容可以保持在本地缓存中,不被由于页面转换而被消失改变
private void savaData(int p, String s, String tag, ViewHolder holder) {
if (p > products.size()) {//控制焦点
if ("chengben".equals(tag)) {
holder.product_chengben.clearFocus();
} else if ("price".equals(tag)) {
holder.product_price.clearFocus();
}
return;
} else {
Products product = products.get(p);
if ("chengben".equals(tag)) {
product.setCostprice(s);
} else if ("price".equals(tag)) {
product.setSaleprice(s);
}
products.set(p, product);
}
}
private abstract class ThisWatcher implements TextWatcher {
ViewHolder holder;
public ThisWatcher(ViewHolder holder) {
super();
this.holder = holder;
}
@Override
public void afterTextChanged(Editable s) {
afterTextChanged(s, holder);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
public abstract void afterTextChanged(Editable s, ViewHolder holder);
}
重要监听:这里是最为困难的地方,在写这段代码的时候。遇到非常多困难。正是由于这些困难。我保留了一些遇到的问题代码。来具体解说。
private class ForcusChangeDate implements OnFocusChangeListener {
ViewHolder holder;
public ForcusChangeDate(ViewHolder holder) {
super();
this.holder = holder;
}
/**这里出现了比較多的问题,须要具体的去注意细节控制button的显示*/
@Override
public void onFocusChange(View v, boolean hasFocus) {
/**A:这是正确的方法,将holoder控制成静态方法,然后依据每一个条目的不同来进行*/
if(hasFocus)
{
if(tempHolder == null)
{
//temp = v.getId();
tempHolder = holder;
holder.price_save.setVisibility(View.VISIBLE);
}
else if(tempHolder == holder)
{
holder.price_save.setVisibility(View.VISIBLE);
}
else if(tempHolder != holder)
{
tempHolder.price_save.setVisibility(View.GONE);
tempHolder=holder;
holder.price_save.setVisibility(View.VISIBLE);
}
}
/**B:这是当中的一种思路。将每一个EditText的ID来控制下来。重用显示,可是由于有两个EditText存在,所以此方法不合适*/
// if (hasFocus) {
// if (temp == -1) {
// holder.price_save.setVisibility(View.VISIBLE);
// if (v.getId() == holder.product_chengben.getId()) {
// temp = holder.product_chengben.getId();
// }else if (v.getId() == holder.product_price.getId()) {
// temp = holder.product_price.getId();
// }
// }else if (temp == holder.product_chengben.getId()|| temp == holder.product_price.getId()) {
// holder.price_save.setVisibility(View.VISIBLE);
// }
//
//
//
// }
/**C: 这是刚刚開始的方法,将两种进行推断,可是在实践的过程中发现Android5.0下面的机制可以实现。
Android5.0以上。将focus机制产生了变化。
使得此监听器一直处于运转状态,故一直在true和false中进行无限推断循环
所以不适用
*/
/*if (hasFocus) {
holder.price_save.setVisibility(View.VISIBLE);
} else {
holder.price_save.setVisibility(View.GONE);
}*/
}
}
/**让EditText失去焦点*/
private void loseFoucus(ViewHolder holder) {
holder.product_chengben.clearFocus();
holder.product_price.clearFocus();
}
}
ListView嵌套两个EditText相关显示问题的更多相关文章
- ListView嵌套 EditText的光标不显示
ListView嵌套EditView,有可能会出现了下面现象: 点击EditView,EditView获取焦点后,有可能光标不显示,也有可能光标不闪烁.点击多次后,光标才正常显示. 获取焦点后,edi ...
- ScrollView中嵌套两个ListView
做的项目中要使用两个ListView在同一个页面上下显示,因为数据源不同,不能通过在Adapter中设置标志位去区分显示,最后只能硬着头皮做一个ScrollView嵌套两个ListView,但按正常情 ...
- ListView嵌套GridView,显示不全解决办法
ListView嵌套GridView时,遇到了GridView只显示一行,其余都显示不出来的问题,最终解决办法如下: 需要自定义GridView,重新绘制高度即可: public class MyGr ...
- ListView嵌套ListView优化
在做业务时候,一个ListView显示多种数据类型,我们想到的方法是ListView在嵌套一个ListView,对于子ListView 占父ListView的一行,就攻克了问题,可是这种逻辑是不是有点 ...
- Listview嵌套Listview
今天做项目,打算模仿淘宝的订单管理,需要Listview嵌套Listview,都是两个控件都是沿着一个方向滑动的,嵌套在一起不幸福,以下是解决方案,打个笔记,以后估计还得用: 其中onMeasure函 ...
- 浅谈ScrollView嵌套ListView及ListView嵌套的高度计算
引言 在Android开发中,我们有时会需要使用ScrollView中嵌套ListView的需求.例如:在展示信息的ListView上还有一部分信息展示区域,并且要求这部分信息展示区域在ListVie ...
- android listView嵌套gridview的使用心得
在开发的过程中可能需要用到listview嵌套gridview的场景,但是在Android中, 不能在一个拥有Scrollbar的组件中嵌入另一个拥有Scrollbar的组件,因为这不科学,会混淆滑动 ...
- 关于ViewPager被嵌套在ScrollView中不显示的问题
关于ViewPager被嵌套在ScrollView中不显示的问题 进入全屏 ScrollView 嵌套ViewPager,要不是业务需求这样,估计没人愿意这么干!因为这种方式,会问题多多,简单百度一下 ...
- ListView嵌套GridView
首先,我们通过两个实例来了解下本篇文章所讲的重点,看下图: 微博: 陌陌: 大家应该对这两款软件并不陌生,接下来,我将列举下本文将要实现的几个点: 1.ListView嵌套GridView,互不冲突, ...
随机推荐
- ValueStack、ActionContext
笔者不知道该用哪个词来形容ValueStack.ActionContext等可以在Struts2中用来存放数据的类.这些类使用的范围不同,得到的方法也不同,下面就来一一介绍. 1. ValueStac ...
- java中json依赖包
Exception in thread "main" java.lang.NoClassDefFoundError: org/jaxen/JaxenException 上面的是do ...
- linux系统web日志分析脚本
linux系统web日志分析这方面工具比较多,比如logwatch或awstats等使用perl语言开发,功能都非常强大.但这些软件都需要进行一些配置,很多朋友往往在技术方面没有投入太多力量,即便参照 ...
- 洛谷9月月赛round2
洛谷9月月赛2 t1 题意:懒得说了 分析:模拟 代码: program flag; var a:..,..]of char; n,i,m,j,x,y,ans,k:longint; begin ass ...
- 【bzoj4869】[Shoi2017]相逢是问候 扩展欧拉定理+并查集+树状数组
题目描述 Informatik verbindet dich und mich. 信息将你我连结. B君希望以维护一个长度为n的数组,这个数组的下标为从1到n的正整数.一共有m个操作,可以分为两种:0 ...
- GDOI2018 爆零记,Challenge Impossibility
蒟蒻的GDOI又双叒叕考挂啦...... Day 0 && Day -1 学校月考,貌似考的还不错? 然而考完试再坐船去中山实在是慢啊......晚上10点才到酒店 wifi差评... ...
- BZOJ2657 [Zjoi2012]旅游(journey) 【树的直径】
题目 到了难得的暑假,为了庆祝小白在数学考试中取得的优异成绩,小蓝决定带小白出去旅游~~ 经过一番抉择,两人决定将T国作为他们的目的地.T国的国土可以用一个凸N边形来表示,N个顶点表示N个入境/出境口 ...
- 【HDU 1711 Number Sequence】
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission ...
- dockerfile 镜像构建
1.镜像的构建有手动与自动方式,这里我们介绍自动化的构建方式 ,dockerfile常用指令如下 2.构建指令build Usage: docker image build [OPTIONS] PAT ...
- PAT 天梯赛 L1-043 阅览室
L1-043. 阅览室 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 天梯图书阅览室请你编写一个简单的图书借阅统计程序.当读者 ...