- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我在 ListView 的一些列表项中有三个按钮,单击该按钮我想更改该列表项的布局,但我面临的问题如下所列。
1).单击按钮时,另一个列表项布局会发生变化。2).在滚动 ListView 时,另一个列表项布局发生了变化,我没有点击它。
这是适配器类的代码。
import java.util.ArrayList;
import com.xsinfosol.DOT.R;
import com.xsinfosol.DOT.ImageLoading.ImageLoader;
import com.xsinfosol.DOT.LibraryClasses.RippleView;
import com.xsinfosol.DOT.model.DOT_Common_Model;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v7.internal.widget.ButtonBarLayout;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class Agenda_Adapter extends BaseAdapter {
Context context;
ArrayList<DOT_Common_Model> arrayList;
ViewHolder viewHolder;
public Agenda_Adapter(Context context , ArrayList<DOT_Common_Model> arrayList) {
// TODO Auto-generated constructor stub
this.context = context;
this.arrayList = arrayList;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return arrayList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return arrayList.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if(convertView==null)
{
viewHolder = new ViewHolder();
convertView = LayoutInflater.from
(context).inflate(R.layout.agenda_event_list_item, null);
viewHolder.linearLayout = (LinearLayout)convertView.findViewById(R.id.agenda_button_layout);
viewHolder.linearLayout.setVisibility(View.GONE);
viewHolder.checkBox = (CheckBox)convertView.findViewById(R.id.agenda_event_checkbox);
viewHolder.eventName = (TextView)convertView.findViewById(R.id.agenda_event_name);
viewHolder.imageView = (ImageView)convertView.findViewById(R.id.agenda_event_imae);
viewHolder.place = (TextView)convertView.findViewById(R.id.agenda_event_place);
viewHolder.time = (TextView)convertView.findViewById(R.id.agenda_event_date_time);
viewHolder.going = (RippleView)convertView.findViewById(R.id.agenda_rippleview_going);
viewHolder.notGoing = (RippleView)convertView.findViewById(R.id.agenda_rippleview_not_going);
viewHolder.mayBe = (RippleView)convertView.findViewById(R.id.agenda_rippleview_maybe);
viewHolder.going.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
viewHolder.going.setVisibility(View.GONE);
viewHolder.notGoing.setVisibility(View.GONE);
viewHolder.mayBe.setVisibility(View.GONE);
Drawable tick = context.getResources().getDrawable(R.drawable.ic_action_tick);
tick.setBounds(0,0, 30, 30);
Button going = new Button(context);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(new LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 40));
params.setMargins(10,0, 10, 4);
going.setLayoutParams(params);
going.setBackgroundColor(context.getResources().getColor(android.R.color.holo_green_dark));
going.setText("Going");
going.setTextSize(15);
going.setTextColor(R.color.white);
going.setCompoundDrawables(null, null, tick, null);
going.setCompoundDrawablePadding(5);
going.setGravity(Gravity.CENTER);
if(viewHolder.linearLayout!=null)
viewHolder.linearLayout.removeAllViews();
viewHolder.linearLayout.addView(going);
}
});
viewHolder.notGoing.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
viewHolder.going.setVisibility(View.GONE);
viewHolder.notGoing.setVisibility(View.GONE);
viewHolder.mayBe.setVisibility(View.GONE);
Drawable cross = context.getResources().getDrawable(R.drawable.ic_action_cancel);
cross.setBounds(0,0, 30, 30);
Button button = new Button(context);
LinearLayout.LayoutParams paramsCross = new LinearLayout.LayoutParams(new LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 40));
paramsCross.setMargins(10,0, 10, 4);
button.setLayoutParams(paramsCross);
button.setBackgroundColor(context.getResources().getColor(R.color.red));
button.setText("Not Going");
button.setTextSize(15);
button.setTextColor(R.color.white);
button.setCompoundDrawables(null, null, cross, null);
button.setCompoundDrawablePadding(5);
button.setGravity(Gravity.CENTER);
if(viewHolder.linearLayout!=null)
viewHolder.linearLayout.removeAllViews();
viewHolder.linearLayout.addView(button);
}
});
viewHolder.mayBe.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
viewHolder.going.setVisibility(View.GONE);
viewHolder.notGoing.setVisibility(View.GONE);
viewHolder.mayBe.setVisibility(View.GONE);
Drawable mayBe = context.getResources().getDrawable(R.drawable.ic_action_emo_err);
mayBe.setBounds(0,0, 30, 30);
Button maybe = new Button(context);
LinearLayout.LayoutParams paramsMaybe = new LinearLayout.LayoutParams(new LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 40));
paramsMaybe.setMargins(10,0, 10, 4);
maybe.setLayoutParams(paramsMaybe);
maybe.setBackgroundColor(context.getResources().getColor(R.color.yellow));
maybe.setText("May Be");
maybe.setTextSize(15);
maybe.setTextColor(R.color.white);
maybe.setCompoundDrawables(null, null, mayBe, null);
maybe.setCompoundDrawablePadding(5);
maybe.setGravity(Gravity.CENTER);
if(viewHolder.linearLayout!=null)
viewHolder.linearLayout.removeAllViews();
viewHolder.linearLayout.addView(maybe);
}
});
viewHolder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
int getPosition = (Integer)buttonView.getTag();
arrayList.get(getPosition).setChecked(buttonView.isChecked());
}
});
convertView.setTag(viewHolder);
convertView.setTag(R.id.agenda_event_checkbox, viewHolder.checkBox);
convertView.setTag(R.id.agenda_event_imae, viewHolder.imageView);
convertView.setTag(R.id.agenda_event_name, viewHolder.eventName);
convertView.setTag(R.id.agenda_event_place, viewHolder.place);
convertView.setTag(R.id.agenda_event_date_time, viewHolder.time);
convertView.setTag(R.id.agenda_button_layout, viewHolder.linearLayout);
convertView.setTag(R.id.agenda_rippleview_going, viewHolder.going);
convertView.setTag(R.id.agenda_rippleview_not_going, viewHolder.notGoing);
convertView.setTag(R.id.agenda_rippleview_maybe, viewHolder.mayBe);
}else
viewHolder = (ViewHolder)convertView.getTag();
viewHolder.checkBox.setTag(position);
viewHolder.going.setTag(position);
viewHolder.notGoing.setTag(position);
viewHolder.mayBe.setTag(position);
ImageLoader imageLoader = new ImageLoader(context);
ImageView imageView = viewHolder.imageView;
imageLoader.DisplayImage(arrayList.get(position).getImage(), imageView);
viewHolder.checkBox.setChecked(arrayList.get(position).ischecked());
switch (arrayList.get(position).getFlag()) {
case "0":
// hasn's seleted any option
if(viewHolder.linearLayout.getVisibility()==View.GONE)
viewHolder.linearLayout.setVisibility(View.VISIBLE);
if(viewHolder.going.getVisibility()==View.GONE)
viewHolder.going.setVisibility(View.VISIBLE);
if(viewHolder.mayBe.getVisibility()==View.GONE)
viewHolder.mayBe.setVisibility(View.VISIBLE);
if(viewHolder.notGoing.getVisibility()==View.GONE)
viewHolder.notGoing.setVisibility(View.VISIBLE);
break;
case "1":
// selected going
viewHolder.going.setVisibility(View.GONE);
viewHolder.notGoing.setVisibility(View.GONE);
viewHolder.mayBe.setVisibility(View.GONE);
Drawable tick = context.getResources().getDrawable(R.drawable.ic_action_tick);
tick.setBounds(0,0, 30, 30);
Button going = new Button(context);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(new LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 40));
params.setMargins(10,0, 10, 4);
going.setLayoutParams(params);
going.setBackgroundColor(context.getResources().getColor(android.R.color.holo_green_dark));
going.setText("Going");
going.setTextSize(15);
going.setTextColor(context.getResources().getColor(R.color.white));
going.setCompoundDrawables(null, null, tick, null);
going.setCompoundDrawablePadding(5);
going.setGravity(Gravity.CENTER);
if(viewHolder.linearLayout!=null)
viewHolder.linearLayout.removeAllViews();
viewHolder.linearLayout.addView(going);
break;
case "2":
// select not going
viewHolder.going.setVisibility(View.GONE);
viewHolder.notGoing.setVisibility(View.GONE);
viewHolder.mayBe.setVisibility(View.GONE);
Drawable cross = context.getResources().getDrawable(R.drawable.ic_action_cancel);
cross.setBounds(0,0, 30, 30);
Button button = new Button(context);
LinearLayout.LayoutParams paramsCross = new LinearLayout.LayoutParams(new LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 40));
paramsCross.setMargins(10,0, 10, 4);
button.setLayoutParams(paramsCross);
button.setBackgroundColor(context.getResources().getColor(R.color.red));
button.setText("Not Going");
button.setTextSize(15);
button.setTextColor(context.getResources().getColor(R.color.white));
button.setCompoundDrawables(null, null, cross, null);
button.setCompoundDrawablePadding(5);
button.setGravity(Gravity.CENTER);
if(viewHolder.linearLayout!=null)
viewHolder.linearLayout.removeAllViews();
viewHolder.linearLayout.addView(button);
break;
case "3":
// selected may be
viewHolder.going.setVisibility(View.GONE);
viewHolder.notGoing.setVisibility(View.GONE);
viewHolder.mayBe.setVisibility(View.GONE);
Drawable mayBe = context.getResources().getDrawable(R.drawable.ic_action_emo_err);
mayBe.setBounds(0,0, 30, 30);
Button maybe = new Button(context);
LinearLayout.LayoutParams paramsMaybe = new LinearLayout.LayoutParams(new LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 40));
paramsMaybe.setMargins(10,0, 10, 4);
maybe.setLayoutParams(paramsMaybe);
maybe.setBackgroundColor(context.getResources().getColor(R.color.yellow));
maybe.setText("May Be");
maybe.setTextSize(15);
maybe.setTextColor(context.getResources().getColor(R.color.black_overlay));
maybe.setCompoundDrawables(null, null, mayBe, null);
maybe.setCompoundDrawablePadding(5);
maybe.setGravity(Gravity.CENTER);
if(viewHolder.linearLayout!=null)
viewHolder.linearLayout.removeAllViews();
viewHolder.linearLayout.addView(maybe);
break;
case "4":
// event doesn't have any invitatin option
if(viewHolder.linearLayout.getVisibility()==View.VISIBLE)
viewHolder.linearLayout.setVisibility(View.GONE);
break;
}
viewHolder.eventName.setText(arrayList.get(position).getName());
viewHolder.place.setText(arrayList.get(position).getPlace());
viewHolder.time.setText(arrayList.get(position).getTime());
return convertView;
}
static class ViewHolder
{
TextView eventName , place, time;
CheckBox checkBox;
ImageView imageView;
LinearLayout linearLayout;
RippleView going , notGoing , mayBe;
}
}
请帮助我,我深陷其中,
最佳答案
第 1 步:在此行之后放置点击监听器(viewHolder = (ViewHolder)convertView.getTag())
第 2 步:在 onclick 方法中删除 View 创建/删除逻辑。更改模型值(“在你的情况下当前项目标志”)并调用 notifyDataset changed 方法
@Override
public void onClick(View view) {
//Set the flag based on your view click
((DOT_Common_Model) getItem( int position)).setFlag(0);
notifyDatasetChanged();
}
第 3 步:根据您的模型值(“标志值”)更改 View
if(((DOT_Common_Model) getItem( int position)).getFlag()==0){
view1.setVisibility(View.VISIBLE);
view2.setVisibility(View.GONE);
view3.setVisibility(View.GONE);
}else if(((DOT_Common_Model) getItem( int position)).getFlag()==1){
view1.setVisibility(View.GONE);
view2.setVisibility(View.VISIBLE);
view3.setVisibility(View.GONE);
}else if(((DOT_Common_Model) getItem( int position)).getFlag()==2){
view1.setVisibility(View.GONE);
view2.setVisibility(View.GONE);
view3.setVisibility(View.VISIBLE);
}else{
view1.setVisibility(View.VISIBLE);
view2.setVisibility(View.VISIBLE);
view3.setVisibility(View.VISIBLE);
}
注意: ListView 仅绑定(bind)数据,它不会为每个数据值更改 View 持有者 View 对象,我们需要根据数据值更改 View 可见性/背景。如果您使用“if condition”,则必须有“else”。
关于android - ListView 内的按钮在滚动和单击按钮时产生问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33970670/
我在 Chrome 上做了一些测试,requestAnimationFrame 产生了 61 fps 而 setTimeOut( callback, 0 ) 产生了 233 fps。 如果一个人想要超
当我调试代码时,我发现 GCC 和 Clang 都为 0.0/0.0 产生 nan,这是我所期望的,但 GCC 产生的 nan 将符号位设置为 1,而Clang 将其设置为 0(如果我没记错的话,与
Closed. This question does not meet Stack Overflow guidelines。它当前不接受答案。 想改善这个问题吗?更新问题,以便将其作为on-topic
我在 R Studio 中有一个时间序列。现在我想计算这个系列的log()。我尝试了以下方法: i <- (x-y) ii <- log(i) 但是我得到以下信息:Warning message: I
我有兴趣了解 JavaScript 的内部结构.我试图阅读 SpiderMonkey 的来源和 Rhino但是绕过我的头是相当复杂的。 我问的原因是:为什么像 (![]+[])[+!![]+[]] 生
我们在 Delphi 中使用标准 TWebbrowser 组件,该组件在内部使用 mshtml.dll。另外,我们使用注册表来确保页面使用新的渲染引擎( Web-Browser-Control-Spe
我必须实现一个序列化/反序列化类,并且我正在使用 System.Xml.Serialization 。我有一些IList类型属性并希望在 IList 中序列化解码属于具有特定区域性信息的列表的所有十进
我有一个 Java 应用程序,它读取包含 SQL 查询的 JSON 文件,并使用 JDBC 在数据库上触发它们。 现在我有 5 万个这样的文件,我需要生成 5 万个独立线程来读取每个文件并将它们上传到
我正在尝试将 TensorFlow 入门页面上的示例线性回归程序调整为二次回归。为此,我只是添加了另一个变量并更改了函数。然而,这似乎会导致 NaN 值。这是我的代码: import numpy as
申请后KernelPCA到我的数据并将其传递给分类器 ( SVC ) 我收到以下错误: ValueError: Input contains NaN, infinity or a value too
这背后的想法是,如果我的数据库中存在登录名(正确的用户名+密码),我将重定向到一个页面,并且在进行此身份验证后,他们可以将消息存储在文本文件中。代码非常简单尽管我不确定为什么会收到 IllegalSt
我有一个返回 log10 值的函数。在将它们转换为正常数字时,出现溢出错误。 OverflowError: (34, 'Numerical result out of range') 我检查了日志值,
nosetests 抛出一个 ImportError,尽管我认为这是一个正确配置的 virtualenv。 ==============================================
我是这个网站的新手,所以如果我做错了什么,我提前道歉。当我尝试使用 kivy-garden 的 ScrollLabel 时,它给了我一个错误。基本上我正在尝试创建一个控制台日志,并且我需要能够在文本框
任何人都对 MDSJ 有任何经验?以下输入仅产生 NaN 结果,我不明白为什么。文档非常稀少。 import mdsj.Data; import mdsj.MDSJ; public class MDS
我有一个非常简单的 scala jcuda 程序,它添加了一个非常大的数组。一切都编译和运行得很好,直到我想从我的设备复制超过 4 个字节到主机。当我尝试复制超过 4 个字节时,我收到 CUDA_ER
我正在使用 Hero 组件在两个页面之间创建动画。Hero 组件用于包装一个 Image 小部件(没问题)和一个 Container 小部件(有问题)。 抛出以下溢出错误: ══╡ EXCEPTIO
我无法理解页面 https://developer.mozilla.org/en/JavaScript/Reference/Operators/Special/void 中的这一段: This ope
当在 Angular 中使用不立即触发事件的异步管道时(http 请求或任何有延迟的可观察对象),第一个值为 null为什么会这样?如何避免这种情况? 第一个变化: SimpleChange {
如果一个导入的库生成了一个会 panic 的 goroutine 怎么办?在这种情况下,开发人员无法阻止程序退出。 就像在这段代码中一样,使用延迟恢复调用一个错误的库没有帮助,因为该库正在生成一个 p
我是一名优秀的程序员,十分优秀!