尝试突出显示GridView中的选定项目 [英] Trying to highlight a selected item from a GridView
问题描述
我有一个Gridview,当我按某个元素时,我想绘制背景。
我有一个适配器来加载gridview元素。我在该适配器上有一个监听器。
在那个监听器上,我把背景放在我想要的颜色上,但是它也画了列表中的另一个元素(我认为在重新加载视图之后,具有相同位置的元素)。
重要提示:我的最小API为9,我无法改变它。
以下是我的getView方法(从适配器):
@Override
public View getView(int position,View convertView,ViewGroup parent) {
viewHolder = null;
if(convertView == null){
viewHolder = new ViewHolder();
LayoutInflater layoutInflater =(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = layoutInflater.inflate(R.layout.highwayappselectionitem,parent,false);
viewHolder.appImageIcon =(ImageView)view.findViewById(R.id.highwayGridViewItemIcon);
viewHolder.appNameLabel =(TextView)view.findViewById(R.id.highwayGridViewItemName);
viewHolder.appInfoButton =(ImageView)view.findViewById(R.id.highwayGridViewItemInfoButton);
viewHolder.linearLayout =(LinearLayout)view.findViewById(R.id.HighwayGridViewItem);
} else {
view = convertView;
viewHolder =(ViewHolder)convertView.getTag();
}
viewHolder.appNameLabel.setText(appsList.get(position).getAppName());
viewHolder.appImageIcon.setImageDrawable(appsList.get(position).getImageIcon());
view.setTag(viewHolder);
viewHolder.linearLayout.setOnClickListener(new MyOnClickListener(position,0));
返回视图;
}
这里是linearlayout的监听器(代表每个项目):
public void onClick(View v){
if(selectedView == 0 || selectedView == 1){
appName = appsList.get(position).getAppName();
filePath = appsList.get(position).getFilePath();
appLogo = appsList.get(position).getImageIcon();
v.setBackgroundColor(activity.getResources()。getColor(R.color.light_grey));
System.out.println(刚按下应用程序...+ appName);
System.out.println(他的filePath是...+ filePath);
HighwayGridViewAppItem tmpSelectedItem = new HighwayGridViewAppItem(appLogo,appName,filePath);
selectedAppsList.add(tmpSelectedItem);
System.out.println(AppSelectionCUstomAdapter:添加了一个新的应用程序到选定的应用程序列表。!!!!!!!!!!!!!!! ....... ..);
System.out.println(AppselectioncustomAdaptaer:selectedAppsList的大小现在是:+ selectedAppsList.size());
} else {
//按下信息按钮
System.out.println(稍后将实现,需要访问web服务);
忽略if(选择的视图== 0或== 1它不再被使用,我可以删除它......)。
另外,appsLIst是我自定义类项目的列表。我有一个布尔属性来表示它是否被选中。
发生什么是,想象这个gridview:
ABC
DEF
GHI p>
JKL
灰线表示滚动条...如果滚动显示那些新元素。
如果我按B,它会选择B,但它也会选择H。
我试图做研究,但是我发现的问题并不相同。
有些东西可能会帮助.. gridview用于添加重复elemetns,所以当我添加元素到gridview我检查元素列表是否已经有类似元素,如果是的话,我不添加它。
解决方案
在你选择一些gridview项目后,你需要添加这行。
$ b ((BaseAdapter)gridView.getAdapter())。notifyDataSetChanged();
编辑我认为getView方法存在问题,重复值:
public View getView(int position,View convertView,ViewGroup parent){
View view = convertView;
if(view == null){//如果它没有被回收,则初始化一些属性
LayoutInflater inflater =(LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.highwayappselectionitem,parent,false);
}
}
I have a Gridview and when i press a certain element, i want paint the background.
I have an adapter to load the gridview elements dinamically. And i have a listener on that adapter.
On that listener, i put the background with the color that i want, but then it also paints another element down the list (i guess the one with the same position, after the view is reloaded).
Important : My minimum API is 9, and i can't really change it.
Here is the code for my getView method (from the adapter) :
@Override
public View getView(int position, View convertView, ViewGroup parent) {
viewHolder = null;
if (convertView == null) {
viewHolder=new ViewHolder();
LayoutInflater layoutInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view=layoutInflater.inflate(R.layout.highwayappselectionitem,parent, false);
viewHolder.appImageIcon=(ImageView) view.findViewById(R.id.highwayGridViewItemIcon);
viewHolder.appNameLabel=(TextView) view.findViewById(R.id.highwayGridViewItemName);
viewHolder.appInfoButton = (ImageView) view.findViewById(R.id.highwayGridViewItemInfoButton);
viewHolder.linearLayout = (LinearLayout) view.findViewById(R.id.HighwayGridViewItem);
}else{
view= convertView;
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.appNameLabel.setText(appsList.get(position).getAppName());
viewHolder.appImageIcon.setImageDrawable(appsList.get(position).getImageIcon());
view.setTag(viewHolder);
viewHolder.linearLayout.setOnClickListener(new MyOnClickListener(position,0));
return view;
}
and here is the listener for the linearlayout (that represents each item):
public void onClick(View v) {
if(selectedView==0 || selectedView==1){
appName=appsList.get(position).getAppName();
filePath=appsList.get(position).getFilePath();
appLogo=appsList.get(position).getImageIcon();
v.setBackgroundColor(activity.getResources().getColor(R.color.light_grey));
System.out.println("Just pressed the app ... "+appName);
System.out.println("His filePath is ... "+filePath);
HighwayGridViewAppItem tmpSelectedItem= new HighwayGridViewAppItem(appLogo, appName, filePath);
selectedAppsList.add(tmpSelectedItem);
System.out.println("AppSelectionCUstomAdapter : Added a new application to the list of selected apps .!!!!!!!!!!!!!!!.........");
System.out.println("AppselectioncustomAdaptaer : The size of the selectedAppsList is now : "+selectedAppsList.size());
}else{
//pressed the info button
System.out.println("This will be implemented later, need access to the webservices");
}
}
}
ignore the if(selected view ==0 or ==1 it is not being used anymore, i can delete it... ).
Also, appsLIst is a list my custom class of items. There i have a boolean attribute to represent if it is selected or not.
WHat is happening is , imagine this gridview:
A B C
D E F
G H I
J K L
The grey line represents the scroll... if scroll those new elements show up.
If i press B, it selects B, but it also selects H.
I tried to do research but the problems that I found weren't the same.
Something that might help.. the gridview used to add repeated elemetns, so when i add the elements to the gridview i check if the list of elements already has a similar element, if so i dont add it.
解决方案 You need to add this line when you want to keep selected some item of gridview after selection.
((BaseAdapter) gridView.getAdapter()).notifyDataSetChanged();
Edit I think there is problem in getView method that's why you are getting duplicate values:
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view== null) { // if it's not recycled, initialize some attributes
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
view = inflater.inflate(R.layout.highwayappselectionitem, parent, false);
}
}
这篇关于尝试突出显示GridView中的选定项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!