解析数据,设置图片的圆角,popwindow弹框

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;
    }
}
 
//当点击条目的时候跳转到第二个页面显示webview
public 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值