Java tutorial
/** * Copyright (C) 2013-2014 EaseMob Technologies. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * http://www.apache.org/licenses/LICENSE-2.0 * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.imalu.alyou.activity; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; import android.app.ProgressDialog; import android.content.Context; import android.content.Intent; import android.graphics.Color; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentTransaction; import android.view.ContextMenu; import android.view.ContextMenu.ContextMenuInfo; import android.view.LayoutInflater; import android.view.MenuItem; import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnTouchListener; import android.view.ViewGroup; import android.view.WindowManager; import android.view.inputmethod.InputMethodManager; import android.widget.AdapterView; import android.widget.AdapterView.AdapterContextMenuInfo; import android.widget.AdapterView.OnItemClickListener; import android.widget.Button; import android.widget.ImageView; import android.widget.ListView; import android.widget.RelativeLayout; import android.widget.TextView; import android.widget.Toast; //import com.easemob.chat.EMContactManager; import com.imalu.alyou.R; import com.easemob.exceptions.EaseMobException; import com.imalu.alyou.Constant; import com.imalu.alyou.AlUApplication; import com.imalu.alyou.adapter.ContactAdapter; import com.imalu.alyou.db.InviteMessgeDao; import com.imalu.alyou.domain.HXUser; import com.imalu.alyou.domain.User; import com.imalu.alyou.widget.Sidebar; /** * ? * */ public class ContactlistFragment extends Fragment { private ContactAdapter adapter; private List<HXUser> contactList; private ListView listView; private boolean hidden; private Sidebar sidebar; private InputMethodManager inputMethodManager; private List<String> blackList; protected static final String TAG = "ContractFragment"; private Button[] mTabs; private ImageView[] mImages; private ImageView imageView; private FriendlistFragment friendListFragment;//? private ConcernlistFragment concernlistFragment;//? private ConsortialistFragment consortialistFragment;//? private FanslistFragment fanslistFragment;//?? private Fragment[] fragments; private int index; private RelativeLayout[] tab_containers; // ?fragmentindex private int currentTabIndex; //private NewMessageBroadcastReceiver msgReceiver; // ? public boolean isConflict = false; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_contact_list, container, false); } private void initView() { mTabs = new Button[4]; mTabs[0] = (Button) this.getView().findViewById(R.id.btn_friend_list); mTabs[1] = (Button) this.getView().findViewById(R.id.btn_club_list); mTabs[2] = (Button) this.getView().findViewById(R.id.btn_fans_list); mTabs[3] = (Button) this.getView().findViewById(R.id.btn_tweet_list); mImages = new ImageView[4]; mImages[0] = (ImageView) this.getView().findViewById(R.id.image_guangbiao0); mImages[1] = (ImageView) this.getView().findViewById(R.id.image_guangbiao1); mImages[2] = (ImageView) this.getView().findViewById(R.id.image_guangbiao2); mImages[3] = (ImageView) this.getView().findViewById(R.id.image_guangbiao3); //imageView=(ImageView) this.getView().findViewById(R.id.contact_image); // tab? mTabs[0].setSelected(true); mTabs[0].setTextColor(Color.GREEN); mImages[0].setVisibility(View.VISIBLE); // imageView.bringToFront(); } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); //T??home???appcrash if (savedInstanceState != null && savedInstanceState.getBoolean("isConflict", false)) return; super.onCreate(savedInstanceState); initView(); friendListFragment = new FriendlistFragment(); consortialistFragment = new ConsortialistFragment(); concernlistFragment = new ConcernlistFragment(); fanslistFragment = new FanslistFragment(); fragments = new Fragment[] { friendListFragment, consortialistFragment, concernlistFragment, fanslistFragment }; // fragment FragmentTransaction transaction = getChildFragmentManager().beginTransaction(); transaction.add(R.id.fragment_layout, friendListFragment).add(R.id.fragment_layout, consortialistFragment) .add(R.id.fragment_layout, concernlistFragment).add(R.id.fragment_layout, fanslistFragment) .hide(consortialistFragment).hide(concernlistFragment).hide(fanslistFragment) .show(friendListFragment).commit(); onContractTabClicked(); } public void onContractTabClicked() { mTabs[0].setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub index = 0; mTabs[0].setTextColor(Color.GREEN); mTabs[1].setTextColor(Color.WHITE); mTabs[2].setTextColor(Color.WHITE); mTabs[3].setTextColor(Color.WHITE); mImages[0].setVisibility(View.VISIBLE); mImages[1].setVisibility(View.GONE); mImages[2].setVisibility(View.GONE); mImages[3].setVisibility(View.GONE); /* mImages[0].bringToFront(); mImages[1].bringToFront(); mImages[2].bringToFront(); mImages[3].bringToFront();*/ // imageView.bringToFront(); util(index); } }); mTabs[1].setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub index = 1; mTabs[0].setTextColor(Color.WHITE); mTabs[1].setTextColor(Color.GREEN); mTabs[2].setTextColor(Color.WHITE); mTabs[3].setTextColor(Color.WHITE); mImages[0].setVisibility(View.GONE); mImages[1].setVisibility(View.VISIBLE); mImages[2].setVisibility(View.GONE); mImages[3].setVisibility(View.GONE); //imageView.bringToFront(); util(index); } }); mTabs[2].setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub index = 2; mTabs[0].setTextColor(Color.WHITE); mTabs[1].setTextColor(Color.WHITE); mTabs[2].setTextColor(Color.GREEN); mTabs[3].setTextColor(Color.WHITE); mImages[0].setVisibility(View.GONE); mImages[1].setVisibility(View.GONE); mImages[2].setVisibility(View.VISIBLE); mImages[3].setVisibility(View.GONE); //imageView.bringToFront(); util(index); } }); mTabs[3].setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub index = 3; mTabs[0].setTextColor(Color.WHITE); mTabs[1].setTextColor(Color.WHITE); mTabs[2].setTextColor(Color.WHITE); mTabs[3].setTextColor(Color.GREEN); mImages[0].setVisibility(View.GONE); mImages[1].setVisibility(View.GONE); mImages[2].setVisibility(View.GONE); mImages[3].setVisibility(View.VISIBLE); //imageView.bringToFront(); util(index); } }); /* if (currentTabIndex != index) { transaction.hide(fragments[currentTabIndex]); if (!fragments[index].isAdded()) { transaction.add(R.id.fragment_container, fragments[index]); } transaction.show(fragments[index]).commit(); } mTabs[currentTabIndex].setSelected(false); mImages[currentTabIndex].setVisibility(View.INVISIBLE); // ?tab? mTabs[index].setSelected(true); mImages[index].setVisibility(View.VISIBLE); currentTabIndex = index;*/ } public void util(int index) { if (currentTabIndex != index) { FragmentTransaction ft = getChildFragmentManager().beginTransaction(); ft.hide(fragments[currentTabIndex]); if (!fragments[index].isAdded()) { ft.add(R.id.fragment_container, fragments[index]); } ft.show(fragments[index]).commit(); } mTabs[currentTabIndex].setSelected(false); mImages[currentTabIndex].setVisibility(View.INVISIBLE); // ?tab? mTabs[index].setSelected(true); mImages[index].setVisibility(View.VISIBLE); currentTabIndex = index; } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); // ??menu if (((AdapterContextMenuInfo) menuInfo).position > 2) { getActivity().getMenuInflater().inflate(R.menu.context_contact_list, menu); } } @Override public boolean onContextItemSelected(MenuItem item) { if (item.getItemId() == R.id.delete_contact) { HXUser tobeDeleteUser = adapter.getItem(((AdapterContextMenuInfo) item.getMenuInfo()).position); // ? deleteContact(tobeDeleteUser); // ? InviteMessgeDao dao = new InviteMessgeDao(getActivity()); dao.deleteMessage(tobeDeleteUser.getUsername()); return true; } else if (item.getItemId() == R.id.add_to_blacklist) { HXUser user = adapter.getItem(((AdapterContextMenuInfo) item.getMenuInfo()).position); moveToBlacklist(user.getUsername()); return true; } return super.onContextItemSelected(item); } @Override public void onHiddenChanged(boolean hidden) { super.onHiddenChanged(hidden); this.hidden = hidden; if (!hidden) { //refresh(); } } @Override public void onResume() { super.onResume(); if (!hidden) { //refresh(); } } /** * ? * * @param toDeleteUser */ public void deleteContact(final HXUser tobeDeleteUser) { final ProgressDialog pd = new ProgressDialog(getActivity()); pd.setMessage("..."); pd.setCanceledOnTouchOutside(false); pd.show(); // new Thread(new Runnable() { // public void run() { // try { // //EMContactManager.getInstance().deleteContact(tobeDeleteUser.getUsername()); // // db? // UserDao dao = new UserDao(getActivity()); // dao.deleteContact(tobeDeleteUser.getUsername()); // AlUApplication.getInstance().getContactList().remove(tobeDeleteUser.getUsername()); // getActivity().runOnUiThread(new Runnable() { // public void run() { // pd.dismiss(); // adapter.remove(tobeDeleteUser); // adapter.notifyDataSetChanged(); // // } // }); // } catch (final Exception e) { // getActivity().runOnUiThread(new Runnable() { // public void run() { // pd.dismiss(); // Toast.makeText(getActivity(), ": " + e.getMessage(), 1).show(); // } // }); // // } // // } // }).start(); } /** * user??? */ private void moveToBlacklist(final String username) { final ProgressDialog pd = new ProgressDialog(getActivity()); pd.setMessage("???..."); pd.setCanceledOnTouchOutside(false); pd.show(); new Thread(new Runnable() { public void run() { //try { //??? //EMContactManager.getInstance().addUserToBlackList(username,false); getActivity().runOnUiThread(new Runnable() { public void run() { pd.dismiss(); Toast.makeText(getActivity(), "????", 0).show(); //refresh(); } }); //} // catch (EaseMobException e) { // e.printStackTrace(); // getActivity().runOnUiThread(new Runnable() { // public void run() { // pd.dismiss(); // Toast.makeText(getActivity(), "???", 0).show(); // } // }); // } } }).start(); } // ui // public void refresh() { // try { // // ?? // getActivity().runOnUiThread(new Runnable() { // public void run() { // getContactList(); // adapter.notifyDataSetChanged(); // // } // }); // } catch (Exception e) { // e.printStackTrace(); // } // } /** * ?????? */ private void getContactList() { contactList.clear(); //?? Map<String, HXUser> users = AlUApplication.getInstance().getContactList(); Iterator<Entry<String, HXUser>> iterator = users.entrySet().iterator(); while (iterator.hasNext()) { Entry<String, HXUser> entry = iterator.next(); if (!entry.getKey().equals(Constant.NEW_FRIENDS_USERNAME) && !entry.getKey().equals(Constant.GROUP_USERNAME) && !blackList.contains(entry.getKey())) contactList.add(entry.getValue()); } // ? Collections.sort(contactList, new Comparator<HXUser>() { @Override public int compare(HXUser lhs, HXUser rhs) { return lhs.getUsername().compareTo(rhs.getUsername()); } }); // """?" contactList.add(0, users.get(Constant.GROUP_USERNAME)); // ""? contactList.add(0, users.get(Constant.NEW_FRIENDS_USERNAME)); } @Override public void onSaveInstanceState(Bundle outState) { if (((MainActivity) getActivity()).isConflict) outState.putBoolean("isConflict", true); super.onSaveInstanceState(outState); } }