6 |
6 |
7 import org.hedgewars.hedgeroid.netplay.MessageLog.Observer; |
7 import org.hedgewars.hedgeroid.netplay.MessageLog.Observer; |
8 |
8 |
9 import android.content.Context; |
9 import android.content.Context; |
10 import android.text.method.LinkMovementMethod; |
10 import android.text.method.LinkMovementMethod; |
11 import android.util.Log; |
|
12 import android.view.View; |
11 import android.view.View; |
13 import android.view.ViewGroup; |
12 import android.view.ViewGroup; |
14 import android.widget.AbsListView.LayoutParams; |
13 import android.widget.AbsListView.LayoutParams; |
15 import android.widget.BaseAdapter; |
14 import android.widget.BaseAdapter; |
16 import android.widget.TextView; |
15 import android.widget.TextView; |
17 |
16 |
18 /** |
17 /** |
19 * A simple TextView that remembers its contents to avoid having to |
18 * Optimization: ListView is smart enough to try re-using the same view for an item |
20 * re-layout them. |
19 * with the same ID, but it still calls getView for those items when the list changes. |
|
20 * Since lines with a given ID never change in our chatlog, we can avoid the effort |
|
21 * of TextView.setText in many cases by checking if the view is already set up for the |
|
22 * line with the right ID - but to do that, the view needs to remember the ID it's |
|
23 * holding the text for. That's what the LoglineView does. |
21 */ |
24 */ |
22 class LoglineView extends TextView { |
25 class LoglineView extends TextView { |
23 CharSequence chatlogAdapterText; |
26 long chatlogId = -1; |
24 |
27 |
25 public LoglineView(Context context) { |
28 public LoglineView(Context context) { |
26 super(context); |
29 super(context); |
27 } |
30 } |
28 } |
31 } |
75 notifyDataSetChanged(); |
78 notifyDataSetChanged(); |
76 } |
79 } |
77 |
80 |
78 public View getView(int position, View convertView, ViewGroup parent) { |
81 public View getView(int position, View convertView, ViewGroup parent) { |
79 LoglineView v = (LoglineView)convertView; |
82 LoglineView v = (LoglineView)convertView; |
80 CharSequence line = log.get(position); |
|
81 if (v == null) { |
83 if (v == null) { |
82 v = new LoglineView(context); |
84 v = new LoglineView(context); |
83 v.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); |
85 v.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); |
84 v.setMovementMethod(LinkMovementMethod.getInstance()); |
86 v.setMovementMethod(LinkMovementMethod.getInstance()); |
85 } |
87 } |
86 if(line != v.chatlogAdapterText) { |
88 long id = getItemId(position); |
87 v.setText(line); |
89 if(id != v.chatlogId) { |
88 v.chatlogAdapterText = line; |
90 v.setText(log.get(position)); |
|
91 v.chatlogId = id; |
89 } |
92 } |
90 return v; |
93 return v; |
91 } |
94 } |
92 } |
95 } |