我创建了一个带有Base Adapter的listView,它实现了ViewHolder Pattern。 listview中的每个项目都有一个countdownTimer。工作正常,但如果我想更新listview countDownTimer在新值和旧值之间闪烁。
如何解决此问题?
BaseAdapter getView()
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = LayoutInflater.from(context);
rowView = inflater.inflate(R.layout.row_item, null);
ViewHolder viewHolder = new ViewHolder();
rowView.setTag(viewHolder);
viewHolder.name = (TextView) rowView.findViewById(R.id.name);
viewHolder.startingAt = (TextView) rowView.findViewById(R.id.starting_at);
} else {
rowView = convertView;
}
ViewHolder holder = (ViewHolder) rowView.getTag();
Model current = list.get(position);
holder.name.setText(current.name);
return rowView;
}
每个项目的CountDownTimer
@Override
public void run() {
int j = 0;
for (int nTime : listOfTimes) {
listOfTimes.set(j, nTime - 1);
j++;
}
if (lv.getCount() <= 0) {
return;
}
int nAmountVisible = lv.getLastVisiblePosition() - lv.getFirstVisiblePosition();
for (int i = 0; i <= nAmountVisible; i++) {
long lTimeFromList = listOfTimes.get(lv.getFirstVisiblePosition() + i);
// transform timestamp to readable date
long weeks = TimeUnit.SECONDS.toDays(lTimeFromList) / 7;
long days = TimeUnit.SECONDS.toDays(lTimeFromList) - (7 * weeks);
long hours = TimeUnit.SECONDS.toHours(lTimeFromList) - TimeUnit.DAYS.toHours(days) - TimeUnit.DAYS.toHours(7 * weeks);
long minute = TimeUnit.SECONDS.toMinutes(lTimeFromList) - (TimeUnit.SECONDS.toHours(lTimeFromList) * 60);
long second = TimeUnit.SECONDS.toSeconds(lTimeFromList) - (TimeUnit.SECONDS.toMinutes(lTimeFromList) * 60);
View viewHolder = lv.getChildAt(i);
AuctionAdapter.ViewHolder localHolder = (AuctionAdapter.ViewHolder) viewHolder.getTag();
viewHolder.invalidate();
localHolder.startingAt.setText(weeks + "w " + days + "d " + hours + "h " + minute + "m " + second + "s ");
}
mHandler.postDelayed(this, 1000);
}
来自填充适配器的片段的代码和countdowntimer的时间列表
baseAdapter = new baseAdapter(getActivity(),list);
baseAdapter.notifyDataSetChanged();
runningLv.setAdapter(baseAdapter);
runningLv.invalidateViews();
ArrayList<Integer> listOfTimes = new ArrayList<Integer>();
listOfTimes.clear();
for (Model m : list) {
listOfTimes.add(Integer.parseInt(m.starting_at) - m.current_time);
}
CountDownTime countDownTimer = new CountDownTime(listOfTimes, runningLv);
countDownTimer.run();
baseAdapter.notifyDataSetChanged();
更新: 这是onCreateView,我想重用视图
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = null;
if (view != null) {
ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null) {
parent.removeView(view);
}
} else {
view = inflater.inflate(R.layout.fragment_running, null);
setupViewItems(view);
}
return view;
}
答案 0 :(得分:1)
使用持有人的正确方法应该是这样的:
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder = null;
if (convertView == null)
{
LayoutInflater inflater = LayoutInflater.from(context);
convertView = inflater.inflate(R.layout.row_item, null);
holder = new ViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.name);
holder.startingAt = (TextView) convertView.findViewById(R.id.starting_at);
convertView.setTag(viewHolder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
Model current = list.get(position);
holder.name.setText(current.name);
return convertView;
}
编辑:
您不应该从正在运行的线程中设置TextView
值,而应直接从getView
方法设置。{/ p>
在return convertView;
之前添加此行:
holder.startingAt.setText(current.timer);
在您的Model
课程中,添加一个新字段:
public String timer = null;
然后在你的运行线程(倒计时器)中:
@Override
public void run()
{
for (int i = 0; i <= listOfTimes.size(); i++)
{
long lTimeFromList = listOfTimes.get(i);
// transform timestamp to readable date
long weeks = TimeUnit.SECONDS.toDays(lTimeFromList) / 7;
long days = TimeUnit.SECONDS.toDays(lTimeFromList) - (7 * weeks);
long hours = TimeUnit.SECONDS.toHours(lTimeFromList) - TimeUnit.DAYS.toHours(days) - TimeUnit.DAYS.toHours(7 * weeks);
long minute = TimeUnit.SECONDS.toMinutes(lTimeFromList) - (TimeUnit.SECONDS.toHours(lTimeFromList) * 60);
long second = TimeUnit.SECONDS.toSeconds(lTimeFromList) - (TimeUnit.SECONDS.toMinutes(lTimeFromList) * 60);
listOfTimes.get(i).timer = weeks + "w " + days + "d " + hours + "h " + minute + "m " + second + "s ";
}
mHandler.postDelayed(this, 1000);
}
答案 1 :(得分:0)
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(context);
convertView = inflater.inflate(R.layout.row_item, null);
viewHolder = new ViewHolder();
viewHolder.name = (TextView) convertView.findViewById(R.id.name);
viewHolder.startingAt = (TextView) convertView.findViewById(R.id.starting_at);
convertView.setTag(viewHolder );
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
Model current = list.get(position);
viewHolder.name.setText(current.name);
return convertView;
}