解析数据,设置图片的圆角,popwindow弹框
来源:互联网 发布:报名网站源码 编辑:程序博客网 时间:2024/06/11 07:35
public class RequestBean { //请求网络接口需要用到的参数; public String url=""; public String value=""; public String method="GET";//GET请求 public RequestBean(String url) { this.url = url; }//post请求 public RequestBean(String url, String value, String method) { this.url = url; this.value = value; this.method = method; }}
//封装解析类
public class HttpUtils { //在activity中调用此方法,请求数据,并获取返回的数据; public void getDataFromServer(Context context, RequestBean bean, DataCallBack callBack) { MyHandler handler = new MyHandler(context, callBack); /***** 起子线程从网络开始获取数据******/ MyTask task = new MyTask(handler, bean); //获取CPU数量 int cpunum = Runtime.getRuntime().availableProcessors(); //线程池实例化 ExecutorService service = Executors.newScheduledThreadPool(cpunum + 1); //将子线程放入线程池执行; service.execute(task); } public abstract interface DataCallBack { public abstract void prosseData(String json); } //网络请求,开启子线程 class MyTask extends Thread { private MyHandler handler; private RequestBean requestBean; public MyTask(MyHandler handler, RequestBean bean) { this.handler = handler; requestBean = bean; } @Override public void run() { super.run(); try { //网络请求,HttpURLConnection URL url = new URL(requestBean.url); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); //判断是否是post请求; if (requestBean.method.equals("POST")) { //设置请求方法;默认是get请求; connection.setRequestMethod(requestBean.method); //允许写入数据; connection.setDoOutput(true); //获取输出流,写入value;添加请求接口时需要追加的参数; OutputStream os = connection.getOutputStream(); os.write(requestBean.value.getBytes()); } //根据请求结果,对请求回来的数据进行处理; StringBuilder builder = new StringBuilder(); //获取网络请求状态码; int code = connection.getResponseCode(); if (code == HttpURLConnection.HTTP_OK) {//返回成功、 //请求结果从输入流里获取; InputStream is = connection.getInputStream(); String str; BufferedReader reader = new BufferedReader(new InputStreamReader(is)); while ((str = reader.readLine()) != null) { builder.append(str); } Log.e("http-util:getdata", builder.toString()); } //使用handler发送请求回来的数据到主线程; Message msg = Message.obtain(); //请求结果放入object;如果请求成功,有数据;如果请求失败(状态码不为200,此时object为""); msg.obj = builder.toString(); //回传状态码,用于提醒用户; msg.what = code; handler.sendMessage(msg); } catch (Exception e) { e.printStackTrace(); } } } //更新UI回传数据; class MyHandler extends Handler { private Context context;//用于toast提示; private DataCallBack callBack; //如果没有toast提示,context可不传; public MyHandler(Context context, DataCallBack callBack) { this.context = context; this.callBack = callBack; } @Override public void handleMessage(Message msg) { super.handleMessage(msg); int what = msg.what;//获取网络连接状态码; if (what == 200) {//数据接口返回数据成功 String result = (String) msg.obj;//result是从服务器端获取的json字符串; callBack.prosseData(result);//调用接口内需要实现的方法,方法内的代码都在此处执行; } else { Toast.makeText(context, "请求失败!", Toast.LENGTH_SHORT).show(); } } }}
//主页面public class MainActivity extends AppCompatActivity implements XListView.IXListViewListener{ //全局变量 private XListView listview; private List<Bean.DataBean> list; private MyAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //找控件 listview = (XListView) findViewById(R.id.listview); //监听 listview.setXListViewListener(this); listview.setPullLoadEnable(true); listview.setPullRefreshEnable(true); adapter = new MyAdapter(MainActivity.this); listview.setAdapter(adapter); //点击条目实现跳转 listview.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Intent intent = new Intent(MainActivity.this, SecondActivity.class); Bean.DataBean bean = list.get(position); intent.putExtra("url",bean.getShare_url()); startActivity(intent); } }); getData(true); } private void getData(final boolean isadd){ String path="http://ic.snssdk.com/2/article/v25/stream/?category=news_car&count=20&bd_city=北京市&bd_latitude=40.049317&bd_longitude=116.296499&bd_loc_time=1455522784&loc_mode=5&lac=4527&cid=28883&iid=3642583580&device_id=11131669133&ac=wifi&channel=baidu&aid=13&app_name=news_article&version_code=460&device_platform=android&device_type=SCH-I919U&os_api=19&os_version=4.4.2&uuid=285592931621751&openudid=AC9E172CE2490000"; RequestBean requestBean = new RequestBean(path); new HttpUtils().getDataFromServer(MainActivity.this, requestBean, new HttpUtils.DataCallBack() { @Override public void prosseData(String json) { Gson gson=new Gson(); Bean bean = gson.fromJson(json, Bean.class); list = bean.getData(); if (isadd){ adapter.addData(list); }else { adapter.updateData(list); } } }); } @Override public void onRefresh() { //刷新 getData(false); listview.stopLoadMore(); listview.stopRefresh(); } @Override public void onLoadMore() { //加载更多 getData(true); listview.stopLoadMore(); listview.stopRefresh(); }}
//创建适配器类public class MyAdapter extends BaseAdapter { //全局变量 private final LayoutInflater inflater; Context context; private List<Bean.DataBean> list=new ArrayList<>(); private final ImageLoader loader; private ViewHolder holder; private final DisplayImageOptions options; private PopupWindow popupWindow; private ImageView btn_pop_close; //有参构造 public MyAdapter(Context context) { this.context = context; ImageLoaderConfiguration configuration=ImageLoaderConfiguration.createDefault(context); loader = ImageLoader.getInstance(); loader.init(configuration); //设置图片圆角 options = new DisplayImageOptions.Builder() .cacheOnDisk(true) .cacheInMemory(true) .displayer(new RoundedBitmapDisplayer(45)) .build(); inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); initPopWindow(); } private void initPopWindow() { View popView = inflater.inflate(R.layout.listview_pop, null); popupWindow = new PopupWindow(popView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); popupWindow.setBackgroundDrawable(new ColorDrawable(0)); btn_pop_close = (ImageView) popView.findViewById(R.id.btn_pop_close); } /** * 显示popWindow * */ public void showPop(View parent, int x, int y,int postion) { //设置popwindow显示位置 popupWindow.showAtLocation(parent, 0, x, y); //获取popwindow焦点 popupWindow.setFocusable(true); //设置popwindow如果点击外面区域,便关闭。 popupWindow.setOutsideTouchable(true); popupWindow.update(); if (popupWindow.isShowing()) { } //点击图片popupWindow消失 btn_pop_close.setOnClickListener(new View.OnClickListener() { public void onClick(View paramView) { popupWindow.dismiss(); } }); } /** * 每个ITEM中more按钮对应的点击动作 * */ public class popAction implements View.OnClickListener { int position; public popAction(int position){ this.position = position; } @Override public void onClick(View v) { int[] arrayOfInt = new int[2]; //获取点击按钮的坐标 v.getLocationOnScreen(arrayOfInt); int x = arrayOfInt[0]; int y = arrayOfInt[1]; showPop(v, x , y, position); } } //添加 public void addData(List<Bean.DataBean> list){ this.list.addAll(list); notifyDataSetChanged(); } //更新 public void updateData(List<Bean.DataBean> list){ this.list.clear(); addData(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 position; } @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView==null){ convertView=View.inflate(context,R.layout.item,null); holder = new ViewHolder(); holder.title= (TextView) convertView.findViewById(R.id.tv_title); holder.source= (TextView) convertView.findViewById(R.id.tv_source); holder.image= (ImageView) convertView.findViewById(R.id.iv_image); holder.img= (ImageView) convertView.findViewById(R.id.img); convertView.setTag(holder); }else { holder= (ViewHolder) convertView.getTag(); } Bean.DataBean dataBean = list.get(position); Bean.DataBean.MiddleImageBean middle_image = dataBean.getMiddle_image(); holder.title.setText(dataBean.getTitle()); holder.source.setText(dataBean.getSource()); loader.displayImage(middle_image.getUrl(),holder.image,options); holder.img .setOnClickListener(new popAction(position)); return convertView; } class ViewHolder{ TextView title,source; ImageView image,img; }}//当点击条目的时候跳转到第二个页面显示webviewpublic class SecondActivity extends AppCompatActivity { private WebView web; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); //找控件 web = (WebView) findViewById(R.id.web); Intent intent = getIntent(); String url = intent.getStringExtra("url"); web.loadUrl(url); //支持js WebSettings settings = web.getSettings(); settings.setJavaScriptEnabled(true); settings.setJavaScriptCanOpenWindowsAutomatically(true); //显示到Activity web.setWebViewClient(new WebViewClient(){ @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { //返回值是true的时候是控制网页在WebView中去打开,如果为false调用系统浏览器或第三方浏览器打开 view.loadUrl(url); return true; } }); }}
//主页面的布局文件只有一个XListview
//第二个页面布局文件<WebView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/web"></WebView>//adapter类的布局文件<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_marginLeft="10dp" android:layout_width="0dp" android:layout_height="150dp" android:layout_weight="2" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tv_title"/> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tv_source" /> <ImageView android:layout_width="20dp" android:layout_height="20dp" android:id="@+id/img" android:src="@mipmap/ic_launcher" android:layout_alignParentRight="true" android:layout_marginRight="10dp"/> </RelativeLayout> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_height="150dp" android:layout_weight="1" android:layout_marginRight="10dp" android:orientation="vertical"> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/iv_image" android:src="@mipmap/ic_launcher"/> </LinearLayout></LinearLayout>
//popwindow的布局文件<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#000"> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="读文章" android:layout_marginLeft="5dp" android:textColor="#fff"/> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="收藏" android:layout_marginLeft="5dp" android:textColor="#fff"/> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="不敢兴趣" android:layout_marginLeft="5dp" android:textColor="#fff"/> <ImageView android:id="@+id/btn_pop_close" android:layout_width="0dp" android:layout_height="20dp" android:layout_weight="1" android:src="@mipmap/ic_launcher" android:layout_marginRight="10dp"/></LinearLayout>
阅读全文
0 0
- 解析数据,设置图片的圆角,popwindow弹框
- 指示项,轮播图,圆角图片可拉伸,popwindow
- popWindow的一些弹出动画的设置
- android的popwindow控件,及控件设为圆角
- 设置popWindow背景变暗
- popwindow 灰屏设置
- popwindow 设置动画
- 设置popWindow背景变暗
- 设置popwindow高度
- 用setAnimationStyle来设置popwindow显示消失的动画效果
- 用setAnimationStyle来设置popwindow显示消失的动画效果
- 用setAnimationStyle来设置popwindow显示消失的动画效果
- popwindow 设置好了以后不显示的问题
- Popwindow 设置宽高无效的原因和解决方案
- 解析带图片的htlm数据
- 设置AFN的数据解析类型
- 当popwindow不铺满全屏时,怎么设置点击窗口外的屏幕部分时,该popwindow自动消失?
- 图片框架 三重更新 联网解析json设置默认图片以及图片圆角弧度 储存到sd卡
- struts2架构中核心对象的探索—— Dispatcher&ConfigurationProvider
- vue2 + router + vuex + vux + axios 开发的一点总计
- redis在工程中的使用
- caffe-pvanet
- Java设计模式--观察者模式【Observer Pattern】
- 解析数据,设置图片的圆角,popwindow弹框
- springmvc限流拦截器
- 设计模式7大原则
- 在Windows服务器上启用TLS 1.2及TLS 1.2基本原理
- Swift 析构过程
- C++---------之--------【虚析构函数】
- Jq实现表格隔行换色
- 不重启Tomcat 更新后台代码 JRebel 7.0.7 (IDEA)
- Vegas怎么添加字幕文字?