package com.qiangyu.test.areaselectdemo;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.google.gson.Gson;
import com.qiangyu.test.areaselectdemo.bean.AreaInfo;
import com.qiangyu.test.areaselectdemo.bean.JsonResult;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.FormEncodingBuilder;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import butterknife.Bind;
import butterknife.ButterKnife;
/**
* 地区Fragment
* @博客 http://blog.csdn.net/yissan
* demo讲解博客地址:https://www.yangqiangyu.club/2017/08/08/android-qing-song-shi-xian-fang-tao-bao-di-qu-xuan-ze/
* @author yangqiangyu
*/
public class AreaFragment extends Fragment implements AdapterView.OnItemClickListener {
private static final String ARG_PARAM1 = "parentCode";
@Bind(R.id.refresh_list_view)
ListView mRefreshListView;
@Bind(R.id.loadingBar)
ProgressBar mLoadingBar;
private String mParam1;
OkHttpClient mOkHttpClient = new OkHttpClient();
private OnFragmentInteractionListener mListener;
private AreaAdapter adapter;
public AreaFragment() {
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @return A new instance of fragment AreaFragment.
*/
public static AreaFragment newInstance(String param1) {
AreaFragment fragment = new AreaFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_area, container, false);
ButterKnife.bind(this, view);
mRefreshListView.setOnItemClickListener(this);
FormEncodingBuilder builder = new FormEncodingBuilder();
builder.add(ARG_PARAM1,mParam1);
final Request request = new Request.Builder()
.url("http://120.77.175.168:9000/area/list")
.post(builder.build())
.build();
mOkHttpClient.newCall(request).enqueue(new Callback(){
@Override
public void onFailure(Request request, IOException e) {
}
@Override
public void onResponse(Response response) throws IOException {
final String res = response.body().string();
if (res!=null){
Gson gson = new Gson();
JsonResult jsonResult = gson.fromJson(res, JsonResult.class);
if (jsonResult.isSuccess()){
List list = (List) jsonResult.getData();
List newList = new ArrayList();
Iterator iterator = list.iterator();
while (iterator.hasNext()){
Map map = (Map) iterator.next();
AreaInfo areaInfo = gson.fromJson(gson.toJson(map),AreaInfo.class);
newList.add(areaInfo);
}
adapter = new AreaAdapter(getContext(),newList);
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
mRefreshListView.setAdapter(adapter);
}
});
}
}
}
});
return view;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
@Override
public void onDestroyView() {
super.onDestroyView();
ButterKnife.unbind(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
AreaInfo areaInfo = (AreaInfo) parent.getAdapter().getItem(position);
if (areaInfo==null) return;
if (mListener!=null){
mListener.onFragmentInteraction(areaInfo);
}
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p/>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
void onFragmentInteraction(AreaInfo areaInfo);
}
class AreaAdapter extends BaseAdapter {
private List list;
private int lastPosition;
public AreaAdapter(Context context, List<AreaInfo> list) {
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView==null){
convertView = LayoutInflater.from(getContext()).inflate(R.layout.area_list_item,parent,false);
viewHolder = new ViewHolder();
viewHolder.textView = (TextView) convertView.findViewById(android.R.id.text1);
convertView.setTag(viewHolder);
}
viewHolder = (ViewHolder) convertView.getTag();
AreaInfo item = (AreaInfo) list.get(position);
viewHolder.textView.setText(item.getAreaName());
if (lastPosition<position&&lastPosition!=0){
ObjectAnimator.ofFloat(convertView,"translationY",convertView.getHeight()*2,0).setDuration(500).start();
}
lastPosition = position;
return convertView;
}
class ViewHolder{
TextView textView;
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
Android仿淘宝地区选择(新)
共70个文件
xml:31个
java:7个
ds_store:7个
2星 需积分: 31 47 下载量 30 浏览量
2017-08-08
23:17:47
上传
评论
收藏 799KB ZIP 举报
温馨提示
由于之前上传的地区选择,服务器地址失效了,所有重新上传一版本,demo讲解地址:https://www.yangqiangyu.club/2017/08/08/android-qing-song-shi-xian-fang-tao-bao-di-qu-xuan-ze/
资源推荐
资源详情
资源评论
收起资源包目录
AreaSelectDemo.zip (70个子文件)
AreaSelectDemo
.gitignore 97B
gradle
wrapper
gradle-wrapper.jar 52KB
gradle-wrapper.properties 231B
local.properties 448B
build.gradle 498B
AreaSelectDemo.iml 945B
gradlew.bat 2KB
gradlew 5KB
app
.gitignore 7B
build.gradle 773B
proguard-rules.pro 677B
app.iml 10KB
libs
src
test
java
com
qiangyu
test
areaselectdemo
ExampleUnitTest.java 324B
main
AndroidManifest.xml 814B
java
com
.DS_Store 6KB
qiangyu
test
areaselectdemo
bean
JsonResult.java 829B
AreaInfo.java 2KB
AreaSelectActivity.java 4KB
MainActivity.java 2KB
AreaFragment.java 7KB
.DS_Store 6KB
.DS_Store 6KB
res
values
styles.xml 383B
colors.xml 208B
strings.xml 77B
dimens.xml 211B
mipmap-xxhdpi
ic_launcher.png 8KB
layout
activity_main.xml 913B
activity_area_select.xml 457B
fragment_area.xml 710B
area_list_item.xml 984B
mipmap-xhdpi
ic_launcher.png 5KB
mipmap-hdpi
ic_launcher.png 3KB
drawable
mipmap-mdpi
ic_launcher.png 2KB
values-w820dp
dimens.xml 358B
mipmap-xxxhdpi
ic_launcher.png 10KB
.DS_Store 6KB
androidTest
java
com
qiangyu
test
areaselectdemo
ApplicationTest.java 362B
.DS_Store 6KB
.DS_Store 6KB
.gradle
2.10
taskArtifacts
cache.properties 30B
fileSnapshots.bin 3.98MB
outputFileStates.bin 21KB
cache.properties.lock 17B
fileHashes.bin 245KB
taskArtifacts.bin 107KB
gradle.properties 855B
.idea
libraries
appcompat_v7_23_3_0.xml 772B
gson_2_5.xml 487B
okhttp_2_4_0.xml 505B
animated_vector_drawable_23_3_0.xml 659B
support_v4_23_3_0.xml 913B
okio_1_4_0.xml 491B
hamcrest_core_1_3.xml 516B
junit_4_12.xml 467B
support_vector_drawable_23_3_0.xml 654B
support_annotations_23_3_0.xml 535B
butterknife_7_0_1.xml 522B
runConfigurations.xml 564B
workspace.xml 150KB
encodings.xml 159B
dictionaries
yangqiangyu.xml 90B
misc.xml 7KB
modules.xml 365B
copyright
profiles_settings.xml 74B
compiler.xml 686B
gradle.xml 626B
markdown-navigator
profiles_settings.xml 104B
.DS_Store 6KB
settings.gradle 15B
共 70 条
- 1
资源评论
- venqing2017-12-14接口失效了
- skyfishmm2018-11-13接口失效。不能使用
程序员yqy
- 粉丝: 1361
- 资源: 25
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 02-【管理流程】-15-员工调岗流程图.xlsx
- 02-【管理流程】-14-企业员工离职流程图.docx
- 02-【管理流程】-16-员工离职流程图.docx
- 02-【管理流程】-17-员工离职管理流程图.docx
- 02-【管理流程】-19-人力资源配置管理流程图.docx
- 02-【管理流程】-18-离职手续办理流程图.docx
- 02-【管理流程】-21-劳动合同管理流程.docx
- 02-【管理流程】-20-劳动合同管理工作流程图.docx
- 02-【管理流程】-22-公司考勤管理流程.docx
- 02-【管理流程】-23-培训管理流程.docx
- 02-【管理流程】-24-员工培训管理流程.docx
- 02-【管理流程】-25-培训管理流程.docx
- 02-【管理流程】-28-基于KPI的绩效考核流程.docx
- 02-【管理流程】-26-绩效考核流程.docx
- 02-【管理流程】-27-绩效考核流程图.docx
- 02-【管理流程】-29-薪酬管理流程.docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功