package com.partynetwork.iparty.iparty; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.drawable.Drawable; import android.location.Location; import android.os.Bundle; import android.os.Handler; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.ImageView.ScaleType; import android.widget.LinearLayout; import android.widget.RelativeLayout; import android.widget.TextView; import com.amap.api.location.AMapLocation; import com.amap.api.location.AMapLocationListener; import com.amap.api.location.LocationManagerProxy; import com.amap.api.location.LocationProviderProxy; import com.lidroid.xutils.BitmapUtils; import com.lidroid.xutils.bitmap.BitmapDisplayConfig; import com.lidroid.xutils.bitmap.callback.BitmapLoadCallBack; import com.lidroid.xutils.bitmap.callback.BitmapLoadFrom; import com.lidroid.xutils.util.LogUtils; import com.partynetwork.dataprovider.DataProvider.IJsonResultListener; import com.partynetwork.dataprovider.json.NetworkEntity; import com.partynetwork.dataprovider.json.struct.Iparty_getIpartyAreaInfoRequst; import com.partynetwork.dataprovider.json.struct.Iparty_getIpartyAreaInfoResponse; import com.partynetwork.dataprovider.json.struct.Iparty_getIpartyBannerRequst; import com.partynetwork.dataprovider.json.struct.Iparty_getIpartyBannerResponse; import com.partynetwork.dataprovider.util.AMapUtil; import com.partynetwork.dataprovider.util.T; import com.partynetwork.iparty.R; import com.partynetwork.iparty.app.AppContext; import com.partynetwork.iparty.app.common.UIHelper; import com.partynetwork.iparty.app.widget.crouton.Crouton; import com.partynetwork.iparty.app.widget.crouton.Style; import com.partynetwork.iparty.helper.IntentHelper; import com.partynetwork.iparty.info.CarouselPartyInfo; import com.partynetwork.iparty.info.IpartyArea; public class IpartyListHeaderView extends LinearLayout implements IJsonResultListener, AMapLocationListener, OnClickListener { /* 滚动控件 */ private ViewPager mViewPager = null; /* 滚动标题 */ private TextView mArticleTitle = null; /* 滚动小圆点 */ private LinearLayout mCustomSpace = null; private RelativeLayout cityChooseRl, sumPartyRl, todayPartyRl, tommorrowPartyRl, weekPartyRl; private TextView sumParty, todayParty, tomorrowParty, weekParty; private TextView cityName; private Activity mActivity; private List<CarouselPartyInfo> mCarouseList = null; private List<ImageView> mCarouseImageList = null; private List<View> mViewList = null; private MyPagerAdapter adapter = null; private boolean loopPlayState = false; private Handler mHandler = null; /** 定位使用 */ private LocationManagerProxy mAMapLocManager = null; /** 用于判断定位超时 */ private AMapLocation aMapLocation; private String cityNum = "0571"; private int thisHeight; public IpartyListHeaderView(Activity activity) { super(activity); mActivity = activity; View.inflate(mActivity, R.layout.iparty_list_header, this); initView(); initDate(); location(); } private void initDate() { mHandler = new Handler(); mCarouseList = new ArrayList<CarouselPartyInfo>(); mCarouseImageList = new ArrayList<ImageView>(); mViewList = new ArrayList<View>(); adapter = new MyPagerAdapter(); mViewPager.setAdapter(adapter); // 设置一个监听器,当ViewPager中的页面改变时调用 mViewPager.setOnPageChangeListener(new MyPageChangeListener()); } private void location() { /******************** 定位功能 */ mAMapLocManager = LocationManagerProxy.getInstance(mActivity); /* * mAMapLocManager.setGpsEnable(false);// * 1.0.2版本新增方法,设置true表示混合定位中包含gps定位,false表示纯网络定位,默认是true Location * API定位采用GPS和网络混合定位方式 * ,第一个参数是定位provider,第二个参数时间最短是5000毫秒,第三个参数距离间隔单位是米,第四个参数是定位监听者 */ mAMapLocManager.requestLocationUpdates( LocationProviderProxy.AMapNetwork, 5000, 10, this); mHandler.postDelayed(stopRun, 12000);// 设置超过12秒还没有定位到就停止定位 } private void initView() { mViewPager = (ViewPager) findViewById(R.id.viewpager); mArticleTitle = (TextView) findViewById(R.id.article_title); mCustomSpace = (LinearLayout) findViewById(R.id.custom_space); cityChooseRl = (RelativeLayout) findViewById(R.id.city_choose_rl); cityChooseRl.setOnClickListener(this); sumPartyRl = (RelativeLayout) findViewById(R.id.sum_party_rl); sumPartyRl.setOnClickListener(this); todayPartyRl = (RelativeLayout) findViewById(R.id.today_party_rl); todayPartyRl.setOnClickListener(this); tommorrowPartyRl = (RelativeLayout) findViewById(R.id.tomorrow_party_rl); tommorrowPartyRl.setOnClickListener(this); weekPartyRl = (RelativeLayout) findViewById(R.id.week_party_rl); weekPartyRl.setOnClickListener(this); cityName = (TextView) findViewById(R.id.city_name); sumParty = (TextView) findViewById(R.id.sum_party); todayParty = (TextView) findViewById(R.id.today_party); tomorrowParty = (TextView) findViewById(R.id.tomorrow_party); weekParty = (TextView) findViewById(R.id.week_party); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { super.onLayout(changed, l, t, r, b); thisHeight = b - l; } public int getThisHeight() { return thisHeight; } private void sendRequest() { AppContext context = AppContext.getInstance(); // 轮播图片请求 Iparty_getIpartyBannerRequst requst = new Iparty_getIpartyBannerRequst(); requst.setOs("android"); requst.setUserId(context.getLoginUid()); requst.setCityNum(cityNum); context.getmDataProvider().getJsonFromNetwork(requst, this); Iparty_getIpartyAreaInfoRequst requst2 = new Iparty_getIpartyAreaInfoRequst(); requst2.setUserId(context.getLoginUid()); requst2.setCityNum(cityNum); context.getmDataProvider().getJsonFromNetwork(requst2, this); } private final class MyPagerAdapter extends PagerAdapter { @Override public int getCount() { return mCarouseImageList.size(); } @Override public void destroyItem(ViewGroup container, int position, Object object) { ((ViewPager) container).removeView(mCarouseImageList.get(position)); } @Override public Object instantiateItem(ViewGroup container, int position) { ImageView temp = mCarouseImageList.get(position); BitmapLoadCallBack<ImageView> callback = new BitmapLoadCallBack<ImageView>() { @Override public void onLoadFailed(ImageView arg0, String arg1, Drawable arg2) { } @Override public void onLoadCompleted(ImageView arg0, String arg1, Bitmap arg2, BitmapDisplayConfig arg3, BitmapLoadFrom arg4) { arg0.setImageBitmap(arg2); if (!loopPlayState) { mArticleTitle.setText(mCarouseList.get(0) .getEventTitle()); // mViewPager.setCurrentItem(0); mHandler.postDelayed(loopPlay, 3000); loopPlayState = true; } } }; BitmapUtils bitmapUtils = new BitmapUtils(temp.getContext()); bitmapUtils.display(temp, mCarouseList.get(position) .getEventFrontCoverUrl(), callback); ((ViewPager) container).addView(temp); return temp; } @Override public boolean isViewFromObject(View arg0, Object arg1) { return arg0 == arg1; } } private final class ClickListener implements OnClickListener { private int mPosition; public ClickListener(int mPosition) { this.mPosition = mPosition; } @Override public void onClick(View v) { if (v instanceof ImageView) { int id = mCarouseList.get(mPosition).getIpartyId(); IntentHelper.goPartyInfoActivity(mActivity, id); } else { return; } } } /** * 当ViewPager中页面的状态发生改变时调用 * * @author Administrator * */ private class MyPageChangeListener implements OnPageChangeListener { private int historyPosition = 0; /** * 当ViewPager中页面的状态发生改变时调用 */ public void onPageSelected(int position) { mArticleTitle.setText(mCarouseList.get(position).getEventTitle()); mViewList.get(historyPosition).setBackgroundResource( R.drawable.dot_normal); mViewList.get(position).setBackgroundResource( R.drawable.dot_focused); historyPosition = position; } public void onPageScrollStateChanged(int arg0) { } public void onPageScrolled(int arg0, float arg1, int arg2) { } } Runnable loopPlay = new Runnable() { @Override public void run() { int position = mViewPager.getCurrentItem(); if (position == (mCarouseList.size() - 1)) { position = 0; mViewPager.setCurrentItem(position); } else { mViewPager.setCurrentItem(++position); } mHandler.postDelayed(loopPlay, 3000); } }; @Override public void onNetworkRequest() { } @Override public void onResultSuccess(NetworkEntity entity) { String aciton = entity.getRequest().getAction(); if (aciton.equals(new Iparty_getIpartyBannerRequst().getAction())) { Iparty_getIpartyBannerResponse response = (Iparty_getIpartyBannerResponse) entity .getResponse(); mCarouseList = Arrays.asList(response.getDetails()); mCarouseImageList.removeAll(mCarouseImageList); mCustomSpace.removeAllViews(); for (int i = 0; i < mCarouseList.size(); i++) { ImageView imageView = new ImageView(mActivity); imageView.setScaleType(ScaleType.CENTER_CROP); imageView.setLayoutParams(new LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); imageView.setOnClickListener(new ClickListener(i)); mCarouseImageList.add(imageView); View view = new View(mActivity); LayoutParams layoutParams = new LayoutParams(20, 6); layoutParams.setMargins(3, 0, 3, 0); view.setLayoutParams(layoutParams); view.setBackgroundResource(R.drawable.dot_normal); mCustomSpace.addView(view); mViewList.add(view); } adapter.notifyDataSetChanged(); } else if (aciton.equals(new Iparty_getIpartyAreaInfoRequst() .getAction())) { Iparty_getIpartyAreaInfoResponse response = (Iparty_getIpartyAreaInfoResponse) entity .getResponse(); IpartyArea area = response.getDetails(); if (area == null) { return; } sumParty.setText(area.getPartyNum() + ""); todayParty.setText(area.getTodayParty() + ""); tomorrowParty.setText(area.getTomorrowParty() + ""); weekParty.setText(area.getWeekParty() + ""); } } @Override public void onResultFail(String result) { if (AppContext.getInstance().isLogin()) { T.showShort(mActivity, result); } } Runnable stopRun = new Runnable() { @Override public void run() { if (aMapLocation == null) { stopLocation();// 销毁掉定位 } } }; /** * 销毁定位 */ private void stopLocation() { if (mAMapLocManager != null) { mAMapLocManager.removeUpdates(this); mAMapLocManager.destory(); } mAMapLocManager = null; } @Override public void onLocationChanged(Location location) { } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } @Override public void onProviderEnabled(String provider) { } @Override public void onProviderDisabled(String provider) { } @Override public void onLocationChanged(AMapLocation location) { if (location != null) { this.aMapLocation = location;// 判断超时机制 Double geoLat = location.getLatitude(); Double geoLng = location.getLongitude(); String cityCode = ""; String desc = ""; Bundle locBundle = location.getExtras(); if (locBundle != null) { cityCode = locBundle.getString("citycode"); desc = locBundle.getString("desc"); } String str = ("定位成功:(" + geoLng + "," + geoLat + ")" + "\n精 度 :" + location.getAccuracy() + "米" + "\n定位方式:" + location.getProvider() + "\n定位时间:" + AMapUtil.convertToTime(location.getTime()) + "\n城市编码:" + cityCode + "\n位置描述:" + desc + "\n省:" + location.getProvince() + "\n市:" + location.getCity() + "\n区(县):" + location.getDistrict() + "\n区域编码:" + location .getAdCode()); cityNum = cityCode; cityName.setText(location.getCity()); LogUtils.i(str); // 停止定位 stopLocation(); // 发送城市数据请求 sendRequest(); } } @Override public void onClick(View v) { switch (v.getId()) { case R.id.city_choose_rl: Intent intent = new Intent(mActivity, HotCityActivity.class); intent.putExtra(HotCityActivity.MY_CITY, cityName.getText().toString()); mActivity .startActivityForResult(intent, HotCityActivity.RESULTCODE); break; case R.id.sum_party_rl: Intent intent2 = new Intent(mActivity, CityIpartyMapActivity.class); intent2.putExtra("ipartyType", 0); intent2.putExtra("cityNum", cityNum); mActivity.startActivity(intent2); break; case R.id.today_party_rl: UIHelper.showParty4City(mActivity, 1, cityNum); break; case R.id.tomorrow_party_rl: UIHelper.showParty4City(mActivity, 2, cityNum); break; case R.id.week_party_rl: UIHelper.showParty4City(mActivity, 3, cityNum); break; default: break; } } public void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == HotCityActivity.RESULTCODE) { if (data != null) { String cityId = data.getStringExtra("cityId"); String name = data.getStringExtra("cityName"); cityName.setText(name); cityNum = cityId; sendRequest(); } } } }