project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/netplay/LobbyChatFragment.java
changeset 7449 2e63537b44f3
parent 7444 2e31f114f57e
child 7452 915f824039ba
equal deleted inserted replaced
7444:2e31f114f57e 7449:2e63537b44f3
     1 package org.hedgewars.hedgeroid.netplay;
       
     2 
       
     3 
       
     4 import org.hedgewars.hedgeroid.R;
       
     5 
       
     6 import android.os.Bundle;
       
     7 import android.support.v4.app.Fragment;
       
     8 import android.view.KeyEvent;
       
     9 import android.view.LayoutInflater;
       
    10 import android.view.View;
       
    11 import android.view.ViewGroup;
       
    12 import android.view.inputmethod.EditorInfo;
       
    13 import android.widget.EditText;
       
    14 import android.widget.ListView;
       
    15 import android.widget.TextView;
       
    16 import android.widget.TextView.OnEditorActionListener;
       
    17 
       
    18 public class LobbyChatFragment extends Fragment {
       
    19 	private ChatlogAdapter adapter;
       
    20 	private Netplay netconn;
       
    21 	
       
    22 	@Override
       
    23 	public void onCreate(Bundle savedInstanceState) {
       
    24 		super.onCreate(savedInstanceState);
       
    25 		netconn = Netplay.getAppInstance(getActivity().getApplicationContext());
       
    26 		adapter = new ChatlogAdapter(getActivity());
       
    27     	adapter.setLog(netconn.lobbyChatlog.getLog());
       
    28     	netconn.lobbyChatlog.registerObserver(adapter);
       
    29 	}
       
    30 	
       
    31 	@Override
       
    32 	public void onStart() {
       
    33 		super.onStart();
       
    34 	}
       
    35 	
       
    36 	@Override
       
    37 	public View onCreateView(LayoutInflater inflater, ViewGroup container,
       
    38 			Bundle savedInstanceState) {
       
    39 		View view = inflater.inflate(R.layout.lobby_chat_fragment, container, false);
       
    40 		
       
    41 		ListView listView = (ListView) view.findViewById(R.id.lobbyConsole);
       
    42 		listView.setAdapter(adapter);
       
    43 		listView.setDivider(null);
       
    44 		listView.setDividerHeight(0);
       
    45 		listView.setVerticalFadingEdgeEnabled(true);
       
    46 		
       
    47 		EditText editText = (EditText) view.findViewById(R.id.lobbyChatInput);
       
    48         editText.setOnEditorActionListener(new ChatSendListener());
       
    49         
       
    50 		return view;
       
    51 	}
       
    52 	
       
    53 	@Override
       
    54 	public void onDestroy() {
       
    55 		super.onDestroy();
       
    56 		netconn.lobbyChatlog.unregisterObserver(adapter);
       
    57 	}
       
    58 
       
    59 	private final class ChatSendListener implements OnEditorActionListener {
       
    60 		public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
       
    61 			String text = v.getText().toString();
       
    62 			if(text.length()>0) {
       
    63 				v.setText("");
       
    64 				netconn.sendChat(text);
       
    65 			}
       
    66 			return true;
       
    67 		}
       
    68 	}
       
    69 }