-
Notifications
You must be signed in to change notification settings - Fork 35
/
LineAdapter.java
121 lines (101 loc) · 3.39 KB
/
LineAdapter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package com.aspsine.edittextinlistview;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import java.util.List;
/**
* Created by aspsine on 15/10/11.
*/
public class LineAdapter extends BaseAdapter {
private List<Line> lines;
public LineAdapter(List<Line> lines) {
this.lines = lines;
}
@Override
public int getCount() {
return lines.size();
}
@Override
public Line getItem(int position) {
return lines.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_line, parent, false);
holder.etLine = (EditText) convertView.findViewById(R.id.etLine);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
final Line line = lines.get(position);
if (holder.etLine.getTag() instanceof TextWatcher) {
holder.etLine.removeTextChangedListener((TextWatcher) (holder.etLine.getTag()));
}
holder.etLine.setHint(position + ".");
if (TextUtils.isEmpty(line.getText())) {
holder.etLine.setText("");
} else {
holder.etLine.setText(line.getText());
}
if (line.isFocus()) {
if (!holder.etLine.isFocused()) {
holder.etLine.requestFocus();
}
CharSequence text = line.getText();
holder.etLine.setSelection(TextUtils.isEmpty(text) ? 0 : text.length());
} else {
if (holder.etLine.isFocused()) {
holder.etLine.clearFocus();
}
}
holder.etLine.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(final View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
final boolean focus = line.isFocus();
check(position);
if (!focus && !holder.etLine.isFocused()) {
holder.etLine.requestFocus();
holder.etLine.onWindowFocusChanged(true);
}
}
return false;
}
});
final TextWatcher watcher = new SimpeTextWather() {
@Override
public void afterTextChanged(Editable s) {
if (TextUtils.isEmpty(s)) {
line.setText(null);
} else {
line.setText(String.valueOf(s));
}
}
};
holder.etLine.addTextChangedListener(watcher);
holder.etLine.setTag(watcher);
return convertView;
}
private void check(int position) {
for (Line l : lines) {
l.setFocus(false);
}
lines.get(position).setFocus(true);
}
static class ViewHolder {
EditText etLine;
}
}