【发布时间】:2016-08-30 09:51:01
【问题描述】:
我有一个 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));
return view;
}
这里是线性布局的监听器(代表每个项目):
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");
}
}
}
忽略 if(selected view ==0 or ==1 它不再被使用,我可以删除它...)。
另外,appsLIst 是我的自定义项目类别的列表。我有一个布尔属性来表示它是否被选中。
发生了什么,想象一下这个网格视图:
A B C
D E F
G H 我
JKL
灰线代表滚动...如果滚动显示那些新元素。 如果我按 B,它会选择 B,但它也会选择 H。
我试图做研究,但我发现的问题并不相同。
可能有帮助的东西.. gridview 用于添加重复的元素,所以当我将元素添加到 gridview 时,我会检查元素列表是否已经有类似的元素,如果是,我不添加它。
【问题讨论】:
标签: android android-layout gridview android-gridview