author | Medo <smaxein@googlemail.com> |
Sun, 12 Aug 2012 22:37:57 +0200 | |
changeset 7482 | d70a5b0d1190 |
parent 7455 | 8e86d8d2927d |
permissions | -rw-r--r-- |
7449 | 1 |
package org.hedgewars.hedgeroid.netplay; |
2 |
||
3 |
import org.hedgewars.hedgeroid.R; |
|
4 |
||
5 |
import android.os.Bundle; |
|
6 |
import android.support.v4.app.Fragment; |
|
7 |
import android.view.KeyEvent; |
|
8 |
import android.view.LayoutInflater; |
|
9 |
import android.view.View; |
|
10 |
import android.view.ViewGroup; |
|
11 |
import android.widget.EditText; |
|
12 |
import android.widget.ListView; |
|
13 |
import android.widget.TextView; |
|
14 |
import android.widget.TextView.OnEditorActionListener; |
|
15 |
||
16 |
public class ChatFragment extends Fragment { |
|
17 |
public static final String ARGUMENT_INROOM = "inRoom"; |
|
18 |
||
19 |
private ChatlogAdapter adapter; |
|
20 |
private Netplay netconn; |
|
21 |
private MessageLog messageLog; |
|
7455
8e86d8d2927d
Hedgeroid: Fix argument passing to ChatFragment, small layout fixes
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
22 |
private boolean inRoom; |
8e86d8d2927d
Hedgeroid: Fix argument passing to ChatFragment, small layout fixes
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
23 |
|
8e86d8d2927d
Hedgeroid: Fix argument passing to ChatFragment, small layout fixes
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
24 |
public void setInRoom(boolean inRoom) { |
8e86d8d2927d
Hedgeroid: Fix argument passing to ChatFragment, small layout fixes
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
25 |
this.inRoom = inRoom; |
8e86d8d2927d
Hedgeroid: Fix argument passing to ChatFragment, small layout fixes
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
26 |
} |
7449 | 27 |
|
28 |
@Override |
|
29 |
public void onCreate(Bundle savedInstanceState) { |
|
30 |
super.onCreate(savedInstanceState); |
|
31 |
netconn = Netplay.getAppInstance(getActivity().getApplicationContext()); |
|
32 |
adapter = new ChatlogAdapter(getActivity()); |
|
7455
8e86d8d2927d
Hedgeroid: Fix argument passing to ChatFragment, small layout fixes
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
33 |
} |
8e86d8d2927d
Hedgeroid: Fix argument passing to ChatFragment, small layout fixes
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
34 |
|
8e86d8d2927d
Hedgeroid: Fix argument passing to ChatFragment, small layout fixes
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
35 |
@Override |
8e86d8d2927d
Hedgeroid: Fix argument passing to ChatFragment, small layout fixes
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
36 |
public void onStart() { |
8e86d8d2927d
Hedgeroid: Fix argument passing to ChatFragment, small layout fixes
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
37 |
super.onStart(); |
8e86d8d2927d
Hedgeroid: Fix argument passing to ChatFragment, small layout fixes
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
38 |
messageLog = inRoom ? netconn.roomChatlog : netconn.lobbyChatlog; |
7449 | 39 |
adapter.setLog(messageLog.getLog()); |
40 |
messageLog.registerObserver(adapter); |
|
41 |
} |
|
42 |
||
43 |
@Override |
|
7455
8e86d8d2927d
Hedgeroid: Fix argument passing to ChatFragment, small layout fixes
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
44 |
public void onStop() { |
8e86d8d2927d
Hedgeroid: Fix argument passing to ChatFragment, small layout fixes
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
45 |
super.onStop(); |
8e86d8d2927d
Hedgeroid: Fix argument passing to ChatFragment, small layout fixes
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
46 |
messageLog.unregisterObserver(adapter); |
7449 | 47 |
} |
48 |
||
49 |
@Override |
|
50 |
public View onCreateView(LayoutInflater inflater, ViewGroup container, |
|
51 |
Bundle savedInstanceState) { |
|
52 |
View view = inflater.inflate(R.layout.fragment_chat, container, false); |
|
53 |
||
54 |
ListView listView = (ListView) view.findViewById(R.id.chatConsole); |
|
55 |
listView.setAdapter(adapter); |
|
56 |
listView.setDivider(null); |
|
57 |
listView.setDividerHeight(0); |
|
58 |
listView.setVerticalFadingEdgeEnabled(true); |
|
59 |
||
60 |
EditText editText = (EditText) view.findViewById(R.id.chatInput); |
|
61 |
editText.setOnEditorActionListener(new ChatSendListener()); |
|
62 |
||
63 |
return view; |
|
64 |
} |
|
65 |
||
66 |
private final class ChatSendListener implements OnEditorActionListener { |
|
67 |
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { |
|
68 |
String text = v.getText().toString(); |
|
69 |
if(text.length()>0) { |
|
70 |
v.setText(""); |
|
71 |
netconn.sendChat(text); |
|
72 |
} |
|
73 |
return true; |
|
74 |
} |
|
75 |
} |
|
76 |
} |