仿京东android客户端收件地址选择
纯手写,可能有些问题,功能已实现 activity
package com.nico;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.gson.reflect.TypeToken;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
public class MainActivity extends Activity implements View.OnClickListener {
private View mPosView;
private TextView mProviceTv, mCityTv, mDistrictTv, mRoomTv;
private ListView mListView;
private ImageView mCloseIv;
private List<Project> projectList = new ArrayList<Project>();
private List<Project> mProviceList = new ArrayList<Project>();
private List<Project> mAddressList = new ArrayList<Project>();
private List<Project> mCityList = new ArrayList<Project>();
private Project mProvicePrj, mCityPrj, mAddressPrj;
/**
* 当前位置
*/
private int mPos = 0;
/**
* 上一次的位置
*/
private int mPreviousPos = 0;
/**
* 动画监听
*/
private CustomAnimationListener mListener = new CustomAnimationListener();
/**
*
*/
private AddressAdapter mAdapter;
private List<Project> mDataList = new ArrayList<Project>();
private ProgressDialog progressDialog = null;
private String mUuid = "";
String authToken = "74629342feccb3380f6e51dced22a061";
private int mSegmentPos = -1;
private int mBuildPos = -1;
private int mRoomPos = -1;
private TextView mAddressTv;
private LinearLayout mSelectLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAdapter = new AddressAdapter(this, mDataList);
initView();
mListView.setAdapter(mAdapter);
}
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
startAnimation();
switch (mPos) {
case 0:
if (mProviceList.isEmpty()) {
initData(mPos);
} else {
mDataList.clear();
mDataList.addAll(mProviceList);
mAdapter.notifyDataSetChanged();
}
break;
case 1:
if (mCityList.isEmpty()) {
initData(mPos);
} else {
mDataList.clear();
mDataList.addAll(mCityList);
mAdapter.notifyDataSetChanged();
}
break;
case 2:
if (mAddressList.isEmpty()) {
initData(mPos);
} else {
mDataList.clear();
mDataList.addAll(mAddressList);
mAdapter.notifyDataSetChanged();
}
break;
}
break;
case 2:
if (mPos == 0) {
RelativeLayout.LayoutParams params4 = new RelativeLayout.LayoutParams(getWidth() / 4,
2);
params4.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
params4.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
mPosView.setLayoutParams(params4);
mProviceTv.setSelected(true);
mCityTv.setSelected(false);
mDistrictTv.setSelected(false);
mRoomTv.setSelected(false);
} else if (mPos == 1) {
mProviceTv.setSelected(false);
mCityTv.setSelected(true);
mDistrictTv.setSelected(false);
mRoomTv.setSelected(false);
RelativeLayout.LayoutParams params4 = new RelativeLayout.LayoutParams(getWidth() / 4,
2);
params4.addRule(RelativeLayout.ALIGN_LEFT, mCityTv.getId());
params4.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
mPosView.setLayoutParams(params4);
} else if (mPos == 2) {
mProviceTv.setSelected(false);
mCityTv.setSelected(false);
mDistrictTv.setSelected(true);
mRoomTv.setSelected(false);
RelativeLayout.LayoutParams params4 = new RelativeLayout.LayoutParams(getWidth() / 4,
2);
params4.addRule(RelativeLayout.ALIGN_LEFT, mDistrictTv.getId());
params4.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
mPosView.setLayoutParams(params4);
} else if (mPos == 3) {
mProviceTv.setSelected(false);
mCityTv.setSelected(false);
mDistrictTv.setSelected(false);
mRoomTv.setSelected(true);
RelativeLayout.LayoutParams params4 = new RelativeLayout.LayoutParams(getWidth() / 4,
2);
params4.addRule(RelativeLayout.ALIGN_LEFT, mRoomTv.getId());
params4.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
mPosView.setLayoutParams(params4);
}
break;
case 3:
TranslateAnimation updownAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
1.0f);
updownAnimation.setDuration(500);
updownAnimation.setInterpolator(new AccelerateDecelerateInterpolator());
updownAnimation.setFillAfter(true);
mSelectLayout.startAnimation(updownAnimation);
String pStr = (mProvicePrj!=null)?mProvicePrj.getName():"省份:";
String cStr = (mCityPrj!=null)?mCityPrj.getName():"城市:";
String aStr = (mAddressPrj!=null)?mAddressPrj.getName():"县区:";
mAddressTv.setText(pStr+cStr+aStr);
break;
}
}
};
private void initView() {
mProviceTv = (TextView) findViewById(R.id.province_tv);
mCityTv = (TextView) findViewById(R.id.city_tv);
mDistrictTv = (TextView) findViewById(R.id.district_tv);
mRoomTv = (TextView) findViewById(R.id.room_tv);
mListView = (ListView) findViewById(R.id.content_listview);
mCloseIv = (ImageView) findViewById(R.id.close_iv);
mPosView = findViewById(R.id.pos_line);
mAddressTv = (TextView) findViewById(R.id.helloworld);
mSelectLayout = (LinearLayout) findViewById(R.id.select_layout);
mProviceTv.setOnClickListener(this);
mCityTv.setOnClickListener(this);
mDistrictTv.setOnClickListener(this);
mRoomTv.setOnClickListener(this);
mCloseIv.setOnClickListener(this);
initTextView();
handler.sendEmptyMessage(1);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (mPos) {
case 0:
mProviceTv.setVisibility(View.VISIBLE);
mCityTv.setVisibility(View.VISIBLE);
mDistrictTv.setVisibility(View.INVISIBLE);
mRoomTv.setVisibility(View.INVISIBLE);
mSegmentPos = position;
refreshSelect(position);
mProvicePrj = mDataList.get(position);
handler.sendEmptyMessage(1);
mPreviousPos = mPos;
mPos = 1;
break;
case 1:
mProviceTv.setVisibility(View.VISIBLE);
mCityTv.setVisibility(View.VISIBLE);
mDistrictTv.setVisibility(View.VISIBLE);
mRoomTv.setVisibility(View.INVISIBLE);
mBuildPos = position;
refreshSelect(position);
mCityPrj = mDataList.get(position);
handler.sendEmptyMessage(1);
mPreviousPos = mPos;
mPos = 2;
break;
case 2:
mProviceTv.setVisibility(View.VISIBLE);
mCityTv.setVisibility(View.VISIBLE);
mDistrictTv.setVisibility(View.VISIBLE);
mRoomTv.setVisibility(View.INVISIBLE);
mRoomPos = position;
refreshSelect(position);
mAddressPrj = mDataList.get(position);
mPreviousPos = mPos;
mPos = 3;
handler.sendEmptyMessage(3);
break;
case 3:
mProviceTv.setVisibility(View.VISIBLE);
mCityTv.setVisibility(View.VISIBLE);
mDistrictTv.setVisibility(View.VISIBLE);
mRoomTv.setVisibility(View.INVISIBLE);
break;
}
}
});
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.province_tv:
mPreviousPos = mPos;
mPos = 0;
handler.sendEmptyMessage(1);
break;
case R.id.city_tv:
mPreviousPos = mPos;
mPos = 1;
handler.sendEmptyMessage(1);
break;
case R.id.district_tv:
mPreviousPos = mPos;
mPos = 2;
handler.sendEmptyMessage(1);
break;
case R.id.room_tv:
mPreviousPos = mPos;
mPos = 3;
handler.sendEmptyMessage(1);
break;
case R.id.close_iv:
handler.sendEmptyMessage(3);
break;
}
}
// private void initSelectStatus() {
//
// Message msg = handler.obtainMessage();
// msg.what = 1;
// handler.sendMessage(msg);
// }
private int getWidth() {
DisplayMetrics dm = new DisplayMetrics();
WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
wm.getDefaultDisplay().getMetrics(dm);
return dm.widthPixels;
}
private void initTextView() {
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(getWidth() / 4,
RelativeLayout.LayoutParams.MATCH_PARENT);
params1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
mProviceTv.setLayoutParams(params1);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(getWidth() / 4,
RelativeLayout.LayoutParams.MATCH_PARENT);
params2.addRule(RelativeLayout.RIGHT_OF, mProviceTv.getId());
mCityTv.setLayoutParams(params2);
RelativeLayout.LayoutParams params3 = new RelativeLayout.LayoutParams(getWidth() / 4,
RelativeLayout.LayoutParams.MATCH_PARENT);
params3.addRule(RelativeLayout.RIGHT_OF, mCityTv.getId());
mDistrictTv.setLayoutParams(params3);
RelativeLayout.LayoutParams params4 = new RelativeLayout.LayoutParams(getWidth() / 4,
RelativeLayout.LayoutParams.MATCH_PARENT);
params4.addRule(RelativeLayout.RIGHT_OF, mDistrictTv.getId());
mRoomTv.setLayoutParams(params4);
RelativeLayout.LayoutParams params5 = new RelativeLayout.LayoutParams(getWidth() / 4,
2);
params5.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
params5.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
mPosView.setLayoutParams(params5);
mProviceTv.setSelected(true);
mCityTv.setSelected(false);
mDistrictTv.setSelected(false);
mRoomTv.setSelected(false);
mProviceTv.setVisibility(View.VISIBLE);
mCityTv.setVisibility(View.INVISIBLE);
mDistrictTv.setVisibility(View.INVISIBLE);
mRoomTv.setVisibility(View.INVISIBLE);
}
private void startAnimation() {
switch (mPos - mPreviousPos) {
case -1:
TranslateAnimation lefttrans = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, -1.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
0.0f);
lefttrans.setDuration(100);
lefttrans.setAnimationListener(mListener);
mPosView.startAnimation(lefttrans);
break;
case 1:
TranslateAnimation righttrans = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
0.0f);
righttrans.setDuration(100);
righttrans.setAnimationListener(mListener);
mPosView.startAnimation(righttrans);
break;
case 2:
TranslateAnimation right2trans = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 2.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
0.0f);
right2trans.setDuration(200);
right2trans.setAnimationListener(mListener);
mPosView.startAnimation(right2trans);
break;
case 3:
TranslateAnimation right3trans = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 3.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
0.0f);
right3trans.setDuration(300);
right3trans.setAnimationListener(mListener);
mPosView.startAnimation(right3trans);
break;
case -2:
TranslateAnimation left2trans = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, -2.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
0.0f);
left2trans.setDuration(200);
left2trans.setAnimationListener(mListener);
mPosView.startAnimation(left2trans);
break;
case -3:
TranslateAnimation left3trans = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, -3.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
0.0f);
left3trans.setDuration(300);
left3trans.setAnimationListener(mListener);
mPosView.startAnimation(left3trans);
break;
}
}
class CustomAnimationListener implements Animation.AnimationListener {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
Message msg = handler.obtainMessage();
msg.what = 2;
handler.sendMessage(msg);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
}
/**
*
*/
private void initData(int mStatus) {
mDataList.clear();
switch (mStatus) {
case 0:
requestProviceData();
break;
case 1:
requestCityData();
break;
case 2:
requestAddressData();
break;
}
// for (int i = 0; i < 20; i++) {
// switch (mStatus) {
// case 0:
// mDataList.add("测试省份数据" + i);
// break;
// case 1:
// mDataList.add("测试城市数据" + i);
// break;
// case 2:
// mDataList.add("测试县区数据" + i);
// break;
// case 3:
// mDataList.add("测试门牌数据" + i);
// break;
// }
// }
}
@SuppressLint("HandlerLeak")
private Handler handlerForProjectList = new Handler() {
public void handleMessage(Message msg) {
if (null != progressDialog && progressDialog.isShowing()) {
progressDialog.dismiss();
}
if (Constants.SUCCESS == msg.what) {
@SuppressWarnings("unchecked")
ArrayList<Project> projectTempList = (ArrayList<Project>) msg.obj;
switch (mPos) {
case 0:
mProviceList = projectTempList;
mDataList.clear();
mDataList.addAll(projectTempList);
mAdapter.notifyDataSetChanged();
break;
case 1:
mCityList = projectTempList;
mDataList.clear();
mDataList.addAll(projectTempList);
mAdapter.notifyDataSetChanged();
break;
case 2:
mAddressList = projectTempList;
mDataList.clear();
mDataList.addAll(projectTempList);
mAdapter.notifyDataSetChanged();
break;
}
} else if (Constants.FAILURE == msg.what) {
} else if (Constants.NET_FAILURE == msg.what) {
}
}
};
private void requestProviceData() {
progressDialog = ProgressDialog.show(this, "", "加载中..", true, true);
projectList.clear();
HashMap<String, String> params = new HashMap<String, String>();
params.put("Authorization", authToken);
params.put("name", "");
params.put("pageNum", "1");
params.put("pageSize", "256");
HttpUtils.HttpGetRequest_Asyn(this, Constants.HTTP_URL_SEGMENT, params, handlerForProjectList,
new TypeToken<ArrayList<Project>>() {
}.getType());
}
private void requestCityData() {
progressDialog = ProgressDialog.show(this, "", "加载中..", true, true);
HashMap<String, String> params = new HashMap<String, String>();
params.put("Authorization", authToken);
params.put("segmentUuid", mProvicePrj.getUuid());
HttpUtils.HttpGetRequest_Asyn(this, Constants.HTTP_URL_BUILDING, params, handlerForProjectList,
new TypeToken<ArrayList<Project>>() {
}.getType());
}
private void requestAddressData() {
progressDialog = ProgressDialog.show(this, "", "加载中..", true, true);
HashMap<String, String> params = new HashMap<String, String>();
params.put("Authorization", authToken);
params.put("buildingUuid", mCityPrj.getUuid());
// 获取数据用老接口
HttpUtils.HttpGetRequest_Asyn(this, Constants.HTTP_URL_UNIT, params, handlerForProjectList,
new TypeToken<ArrayList<Project>>() {
}.getType());
}
private void refreshSelect(int selectPos) {
switch (mPos) {
case 0:
for(Project prj : mProviceList)
{
prj.setSelect(false);
}
mProviceList.get(selectPos).setSelect(true);
mCityList.clear();
break;
case 1:
for(Project prj : mCityList)
{
prj.setSelect(false);
}
mCityList.get(selectPos).setSelect(true);
mAddressList.clear();
break;
case 2:
for(Project prj : mAddressList)
{
prj.setSelect(false);
}
mAddressList.get(selectPos).setSelect(true);
break;
}
mDataList.get(selectPos).setSelect(true);
mAdapter.notifyDataSetChanged();
}
}
主布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin">
<TextView
android:id="@+id/helloworld"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<include
layout="@layout/content"
android:layout_width="match_parent"
android:layout_height="400dp"
android:layout_alignParentBottom="true"
android:id="@+id/select_layout"/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:background="@drawable/et_bg_no_corners">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/title" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:src="@mipmap/gb"
android:id="@+id/close_iv"/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/guide_layout"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@drawable/et_bg_no_corners">
<TextView
android:id="@+id/province_tv"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#00000000"
android:gravity="center"
android:text="@string/segment"
android:textColor="@drawable/fragment_tab_textview_color_seletor" />
<TextView
android:id="@+id/city_tv"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#00000000"
android:gravity="center"
android:text="@string/building"
android:visibility="gone"
android:textColor="@drawable/fragment_tab_textview_color_seletor" />
<TextView
android:id="@+id/district_tv"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#00000000"
android:gravity="center"
android:text="@string/unit"
android:visibility="gone"
android:textColor="@drawable/fragment_tab_textview_color_seletor" />
<TextView
android:id="@+id/room_tv"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#00000000"
android:gravity="center"
android:visibility="gone"
android:text="@string/room"
android:textColor="@drawable/fragment_tab_textview_color_seletor" />
<View
android:id="@+id/pos_line"
android:layout_width="wrap_content"
android:layout_height="2dp"
android:layout_alignParentBottom="true"
android:background="#e84316" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#dddddd" />
<ListView
android:id="@+id/content_listview"
android:layout_width="wrap_content"
android:layout_height="match_parent"></ListView>
</LinearLayout>