我只在屏幕上显示的列表视图的第一项上有奇怪的行为;它坚持第二个。它是一个动态列表视图,用户可以在其中写入文本并添加/删除新行。请参阅以下代码:
添加新行的按钮代码:
add_task.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
job_task_array.add(new JobTask_Model("", 50));
job_task_update(rootView, item_job_task_height);
for (int i = 0; i < job_task_array.size(); i++)
Log.e("job_task", job_task_array.get(i).getJob_task_name());
}
});
单击上面的按钮后,此部分会对其进行动画处理:
// This is called whenever we click 'add task' or 'delete task', does the whole animation cycle
public void job_task_update(final View view, final int job_task_heightmove) {
add_task.setEnabled(false);
Animation animation = new Animation() {
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
job_task_listView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int) (job_task_height + (job_task_heightmove * interpolatedTime))));
}
};
animation.setDuration(CreatePostingActivity.animationTime);
view.startAnimation(animation); // Update job_task_height's value AFTER the shit animates because apparently it takes the current job_task_height
view.postOnAnimationDelayed(new Runnable() {
@Override
public void run() {
job_task_height += job_task_heightmove;
add_task.setEnabled(true);
}
}, CreatePostingActivity.animationTime * 2);
}
自定义适配器:
// Object model for the posting_2 job_task_listView
public class JobTask_Model {
private int job_task_seekbar_value;
private String job_task_name;
public JobTask_Model(String job_task_name, int job_task_seekbar_value) {
this.job_task_seekbar_value = job_task_seekbar_value;
this.job_task_name = job_task_name;
}
public String getJob_task_name() {
return job_task_name;
}
public int getJob_task_seekbar_value() {
return job_task_seekbar_value;
}
}
/**
* This class is the adapter for the tasks
**/
public class JobTasks_Adapter extends ArrayAdapter {
private final ArrayList<JobTask_Model> job_task_array;
private Context context;
public JobTasks_Adapter(Context context, ArrayList arrayList) {
super(context, R.layout.item_job_task, arrayList);
this.context = context;
job_task_array = arrayList;
}
// Create static class for our special viewholder model, references item_job_task layout
class JobTask_ViewHolder {
EditText job_task_name;
SeekBar job_task_seekbar;
Button job_task_delete;
}
// This gets the view
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater layoutInflater = ((Activity) context).getLayoutInflater();
view = layoutInflater.inflate(R.layout.item_job_task, parent, false);
// configure view holder
JobTask_ViewHolder viewHolder = new JobTask_ViewHolder();
viewHolder.job_task_name = (EditText) view.findViewById(R.id.job_task_name);
viewHolder.job_task_seekbar = (SeekBar) view.findViewById(R.id.job_task_seekbar);
viewHolder.job_task_delete = (Button) view.findViewById(R.id.job_task_delete);
view.setTag(viewHolder);
}
JobTask_ViewHolder viewHolder = (JobTask_ViewHolder) view.getTag();
// Set the bar value so it doesn't reset when we add another one in
viewHolder.job_task_seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
job_task_array.get(position).job_task_seekbar_value = progress;
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
viewHolder.job_task_name.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
job_task_array.get(position).job_task_name = s.toString();
}
@Override
public void afterTextChanged(Editable s) {
}
});
// Button to delete each task
viewHolder.job_task_delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
job_task_array.remove(position);
job_task_update(rootView, -item_job_task_height);
}
});
viewHolder.job_task_seekbar.setProgress(job_task_array.get(position).getJob_task_seekbar_value());
viewHolder.job_task_name.setText(job_task_array.get(position).getJob_task_name());
return view;
}
}
答案 0 :(得分:0)
在此方法中添加一些logcat语句,
这可以帮助您查看&#39;标记&#39;正确关联(创建/读取),因为相同的2个视图应该具有不同的标记&#39;对象。
{{1}}
答案 1 :(得分:0)
When position == 0, view == null.
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
JobTask_ViewHolder viewHolder
if (view == null) {
LayoutInflater layoutInflater = ((Activity) context).getLayoutInflater();
view = layoutInflater.inflate(R.layout.item_job_task, parent, false);
// configure view holder
viewHolder = new JobTask_ViewHolder();
viewHolder.job_task_name = (EditText) view.findViewById(R.id.job_task_name);
viewHolder.job_task_seekbar = (SeekBar) view.findViewById(R.id.job_task_seekbar);
viewHolder.job_task_delete = (Button) view.findViewById(R.id.job_task_delete);
view.setTag(viewHolder);
}else{
viewHolder = (JobTask_ViewHolder) view.getTag();
}
...
}
答案 2 :(得分:0)
您的static
不是静态
制作此:
// Create static class for our special viewholder model, references item_job_task layout
class JobTask_ViewHolder {
EditText job_task_name;
SeekBar job_task_seekbar;
Button job_task_delete;
}
INTO THIS:
// Create static class for our special viewholder model, references item_job_task layout
static class JobTask_ViewHolder {
EditText job_task_name;
SeekBar job_task_seekbar;
Button job_task_delete;
}
How can I make my ArrayAdapter follow the ViewHolder pattern?作为示例