|
1 package org.hedgewars.hedgeroid; |
|
2 |
|
3 import org.hedgewars.hedgeroid.R; |
|
4 import org.hedgewars.hedgeroid.Datastructures.Player; |
|
5 import org.hedgewars.hedgeroid.netplay.Netplay; |
|
6 |
|
7 import android.os.Bundle; |
|
8 import android.support.v4.app.ListFragment; |
|
9 import android.view.ContextMenu; |
|
10 import android.view.ContextMenu.ContextMenuInfo; |
|
11 import android.view.LayoutInflater; |
|
12 import android.view.MenuInflater; |
|
13 import android.view.MenuItem; |
|
14 import android.view.View; |
|
15 import android.view.ViewGroup; |
|
16 import android.widget.AdapterView.AdapterContextMenuInfo; |
|
17 |
|
18 public class LobbyPlayerlistFragment extends ListFragment { |
|
19 private Netplay netplay; |
|
20 private LobbyPlayerlistAdapter adapter; |
|
21 |
|
22 @Override |
|
23 public void onCreate(Bundle savedInstanceState) { |
|
24 super.onCreate(savedInstanceState); |
|
25 netplay = Netplay.getAppInstance(getActivity().getApplicationContext()); |
|
26 adapter = new LobbyPlayerlistAdapter(); |
|
27 adapter.setSource(netplay.lobbyPlayerlist); |
|
28 setListAdapter(adapter); |
|
29 } |
|
30 |
|
31 @Override |
|
32 public void onDestroy() { |
|
33 super.onDestroy(); |
|
34 adapter.invalidate(); |
|
35 } |
|
36 |
|
37 @Override |
|
38 public void onActivityCreated(Bundle savedInstanceState) { |
|
39 super.onActivityCreated(savedInstanceState); |
|
40 registerForContextMenu(getListView()); |
|
41 } |
|
42 |
|
43 @Override |
|
44 public void onCreateContextMenu(ContextMenu menu, View v, |
|
45 ContextMenuInfo menuInfo) { |
|
46 super.onCreateContextMenu(menu, v, menuInfo); |
|
47 AdapterContextMenuInfo info = (AdapterContextMenuInfo)menuInfo; |
|
48 MenuInflater inflater = getActivity().getMenuInflater(); |
|
49 inflater.inflate(R.menu.lobby_playerlist_context, menu); |
|
50 menu.setHeaderIcon(R.drawable.human); |
|
51 menu.setHeaderTitle(adapter.getItem(info.position).name); |
|
52 } |
|
53 |
|
54 @Override |
|
55 public boolean onContextItemSelected(MenuItem item) { |
|
56 AdapterContextMenuInfo info = (AdapterContextMenuInfo)item.getMenuInfo(); |
|
57 Player player = adapter.getItem(info.position); |
|
58 switch(item.getItemId()) { |
|
59 case R.id.player_info: |
|
60 netplay.sendPlayerInfoQuery(player.name); |
|
61 return true; |
|
62 case R.id.player_follow: |
|
63 netplay.sendFollowPlayer(player.name); |
|
64 return true; |
|
65 default: |
|
66 return super.onContextItemSelected(item); |
|
67 } |
|
68 } |
|
69 |
|
70 @Override |
|
71 public View onCreateView(LayoutInflater inflater, ViewGroup container, |
|
72 Bundle savedInstanceState) { |
|
73 return inflater.inflate(R.layout.fragment_playerlist, container, false); |
|
74 } |
|
75 } |