- 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/
我之前在论坛上发布过这个,但我想我不太清楚。我有一个侧边栏 ListView 和一个包含我的控件的主面板。我想在 oncreate() 中突出显示 listview 中的 Activity 项,因为每
这里有没有人知道我如何通过 onTap() 扩展 ListView 项(在本例中为卡片)的内容 - 以显示更多选项?您知道那里有可以提供帮助的酒吧吗? 我已经看过了,但我不知道要搜索什么?我相信你们都
我的 ListView 控件有问题。我希望我的奇数行和偶数行具有不同的颜色,但我想通过代码而不是 FXML 来实现。例如: 第一行 - 绿色 第二行 - 红色 第三行 - 绿色 第四行 - 红色 现在
我有一个 ListView 。我想让单元格背景透明。目前,我正在做以下事情: .list-cell { -fx-background-color: transparent; } 但是,细胞的颜
我想创建一个几乎无限的元素列表,但我想将列表的初始位置设置为某个特定元素。像这张图片: 其中索引 0 将是初始位置,并且此列表可能会也可能不会在两个方向上延伸很长。 我可以像这样创建我的元素: W
有没有办法在JavaFX中获取ListView的可见项?我想确定 JavaFX 应用程序中 ListView 显示的第一个可见项。 以下代码found here不适合我(仅适用于 TableView)
开发人员。 我尝试在水平方向拉伸(stretch) ListView 项目。我确实重置了 ItemContainerStyle ,并且水平对齐是拉伸(stretch)的。这是 MainPage.xam
有没有办法在JavaFX中获取ListView的可见项?我想确定 JavaFX 应用程序中 ListView 显示的第一个可见项。 以下代码found here不适合我(仅适用于 TableView)
我想问一下: 我有一个预定义顺序的数组。 var order=new Array(); order[0]="Tesco"; order[1]="Interspar"; order[2]="
我希望创建以下内容:当到达内部 Listview 的顶部或底部时,我想继续在顶部 Listview 中滚动。见动图: Gif of what I got so far 一个选项是在到达底部时将内部 L
我正在尝试在 ajax 发布后刷新 jQuery 移动 ListView ,我一直在尝试使用 .trigger("create") 来执行此操作,如下所示: Most Played
我真的不明白是什么导致了错误我检查了文档,这里有一个非常相似的例子是我的 views.py,我使用的应用程序下的 urls.py 包含,以及模板 View .py class SchoolListVi
我有这个父布局 parent_listview 的列表项具有这种布局 child_listview 的项目有这个布局
我在单击列表项时获取 listview 项 时遇到问题。我得到了 simple listview(Arrayadapter) 的 listview item,但我遇到了 custom listview
我有一个工作正常的 DropdownMenu,但我需要一个 ListView,但我无法转换它。 DropdownMenu 如下所示: // Create the List of devices t
我想实现一个可滚动(垂直)且其中包含元素的屏幕。所以我把它放在 listview.builder 上。问题是,其中一个元素是另一个水平滚动的 listview.builder。当我实现它时,horiz
帮助!我想不通! 为了帮助我学习 SwiftUI,我正在使用 ListView 制作一个简单的杂货 list 应用程序。点击列表中的每个项目时,有些按钮会变成绿色或红色。 在我向列表数组中添加太多项目
所以我知道我们可以等待 ListView 加载,然后显示它。但是,如果我们动态加载正在下载的图像,ListView 将加载但图像尚未加载,因此它们不会出现在 ListView 中。 可接受的等待 Li
我是 Android 的新手,正在努力提高编程技能。 在这里,我有自定义 ListView 适配器类,我曾在其中显示 ListView 项目,如 TextView 、图像等。 我需要做的是,我在 Li
So, what I want is to individually disable a button for the concerned item in the ListView when clic
我是一名优秀的程序员,十分优秀!