|
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 |
|
23 @Override |
|
24 public void onCreate(Bundle savedInstanceState) { |
|
25 super.onCreate(savedInstanceState); |
|
26 |
|
27 Bundle bundle = getArguments(); |
|
28 netconn = Netplay.getAppInstance(getActivity().getApplicationContext()); |
|
29 adapter = new ChatlogAdapter(getActivity()); |
|
30 messageLog = bundle.getBoolean(ARGUMENT_INROOM) ? netconn.roomChatlog : netconn.lobbyChatlog; |
|
31 adapter.setLog(messageLog.getLog()); |
|
32 messageLog.registerObserver(adapter); |
|
33 } |
|
34 |
|
35 @Override |
|
36 public void onStart() { |
|
37 super.onStart(); |
|
38 } |
|
39 |
|
40 @Override |
|
41 public View onCreateView(LayoutInflater inflater, ViewGroup container, |
|
42 Bundle savedInstanceState) { |
|
43 View view = inflater.inflate(R.layout.fragment_chat, container, false); |
|
44 |
|
45 ListView listView = (ListView) view.findViewById(R.id.chatConsole); |
|
46 listView.setAdapter(adapter); |
|
47 listView.setDivider(null); |
|
48 listView.setDividerHeight(0); |
|
49 listView.setVerticalFadingEdgeEnabled(true); |
|
50 |
|
51 EditText editText = (EditText) view.findViewById(R.id.chatInput); |
|
52 editText.setOnEditorActionListener(new ChatSendListener()); |
|
53 |
|
54 return view; |
|
55 } |
|
56 |
|
57 @Override |
|
58 public void onDestroy() { |
|
59 super.onDestroy(); |
|
60 messageLog.unregisterObserver(adapter); |
|
61 } |
|
62 |
|
63 private final class ChatSendListener implements OnEditorActionListener { |
|
64 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { |
|
65 String text = v.getText().toString(); |
|
66 if(text.length()>0) { |
|
67 v.setText(""); |
|
68 netconn.sendChat(text); |
|
69 } |
|
70 return true; |
|
71 } |
|
72 } |
|
73 } |