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; |
|
22 private boolean inRoom; |
|
23 |
|
24 public void setInRoom(boolean inRoom) { |
|
25 this.inRoom = inRoom; |
|
26 } |
|
27 |
|
28 @Override |
|
29 public void onCreate(Bundle savedInstanceState) { |
|
30 super.onCreate(savedInstanceState); |
|
31 netconn = Netplay.getAppInstance(getActivity().getApplicationContext()); |
|
32 adapter = new ChatlogAdapter(getActivity()); |
|
33 } |
|
34 |
|
35 @Override |
|
36 public void onStart() { |
|
37 super.onStart(); |
|
38 messageLog = inRoom ? netconn.roomChatlog : netconn.lobbyChatlog; |
|
39 adapter.setLog(messageLog.getLog()); |
|
40 messageLog.registerObserver(adapter); |
|
41 } |
|
42 |
|
43 @Override |
|
44 public void onStop() { |
|
45 super.onStop(); |
|
46 messageLog.unregisterObserver(adapter); |
|
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 } |
|