我有一个自定义列表适配器,我正在尝试使用带有进度条的布局。
我的问题是,当视图第一次膨胀时,我将进度条可见性设置为false。单击该项目时,我将进度条可见性设置为true。但是,如果向下滚动列表,随机项的进度条将变为可见(由于视图的回收?)。
有什么简单的方法可以解决这个问题吗?
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
convertView = inflater.inflate(R.layout.imagegriditem, null);
holder = new ViewHolder();
holder.heroName = (TextView) convertView.findViewById(R.id.heroName);
holder.heroImage = (ImageView)convertView.findViewById(R.id.heroImage);
holder.progressBar = (ProgressBar) convertView.findViewById(R.id.progressBar);
holder.progressBar.setIndeterminate(false);
holder.progressBar.setVisibility(View.INVISIBLE);
holder.progressBar.setIndeterminate(false);
holder.progressVisible = View.INVISIBLE;
convertView.setTag(holder);
}
else{
holder = (ViewHolder) convertView.getTag();
}
if(holder != null){
holder.heroName.setText(heroes[position]);
holder.heroImage.setImageResource(android.R.drawable.ic_dialog_email);
holder.progressBar.setProgress(10);
}
final int pos = position;
final ViewHolder hold = holder;
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
hold.progressBar.setProgress(hold.progressBar.getProgress() + 10);
hold.progressVisible = View.VISIBLE;
hold.progressBar.setVisibility(View.VISIBLE);
}
});
return convertView;
}
这基本上是概念代码的证明..所以它并不完全漂亮。但是,只是试图让这个想法失效,以便我能正确使用它。
答案 0 :(得分:0)
public class AdapterItem
{
public Boolean IsInProgress;
}
...
Public class CustomAdapter extends BaseAdapter
{
ArrayList<AdapterItem> _list = new ArrayList<AdapterItem>()
...
public View getView(int position, View convertView, ViewGroup parent)
{
...
if (holder != null && _list.size() > position)
{
AdapterItem item = _list.get(position);
holder.progressVisible = Item.IsInProgress ? View. VISIBLE : View.INVISIBLE
}
}
public void onItemClick(int position)
{
AdapterItem item = _list.get(position);
item.IsInProgress = !item.IsInProgress;
}
}
...
listView.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
CustomAdapter adpt = (CustomAdapter)parent.getAdapter();
adpt.onItemClick(position);
}
}