OkHttp获取数据,xlistview显示popwod弹框

1,先一步设置xlistview类,布局,

2,导入依赖,价包注意版本问题

      1,Gson价包和imageloader价包必须加入,否则无法导包。

   compile files('libs/universal-image-loader-1.9.3-with-sources.jar')
   compile files('libs/gson-2.2.4.jar')
   compile 'com.squareup.okhttp3:okhttp:3.9.0'

    3.  封装类的实现

    4,MainActivity的构          

   public class MainActivity extends AppCompatActivity implements XListView.IXListViewListener {

    private XListView xListView;
    private MyAdapter adapter;
    private List<Bean.DataBean> data;
    private Bean.TipsBean tips;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        xListView = (XListView) findViewById(R.id.xlv);
        xListView.setPullLoadEnable(true);
        xListView.setPullRefreshEnable(true);
        xListView.setXListViewListener(this);
        //创建okHttpClient对象
        OkHttpClient mOkHttpClient = new OkHttpClient();
        //创建一个Request
        final Request request = new Request.Builder()
                .url("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")
                .build();
        //new call
        Call call = mOkHttpClient.newCall(request);
        //请求加入调度
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                final String string = response.body().string();
                //子线程操作ui
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Bean bean = new Gson().fromJson(string, Bean.class);
                        if (bean != null) {
                            data = bean.getData();
                            tips = bean.getTips();
                            Log.e("======", "===" + data);
                            adapter = new MyAdapter(MainActivity.this);
                            adapter.addData(data);
                            xListView.setAdapter(adapter);
                        }
                    }
                });

            }
        });
        xListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(MainActivity.this, InfoActivity.class);
                intent.putExtra("weburl", data.get(i-1).getShare_url());
                startActivity(intent);
            }
        });
    }
    @Override
    public void onRefresh() {
        adapter.updateData(data);
        xListView.stopRefresh();
    }

    @Override
    public void onLoadMore() {
        adapter.addData(data);
        xListView.stopLoadMore();
    }
}
  4.Adapter类实现。注意数组越界。    
public class MyAdapter extends BaseAdapter {
    List<Bean.DataBean> list = new ArrayList<>();
    Context context;
    private final ImageLoader loader;
    private final DisplayImageOptions options;
    private Button mButton;
    private PopupWindow mPopupWindow;

    public MyAdapter(Context context) {
        this.context = context;
        ImageLoaderConfiguration configuration = ImageLoaderConfiguration.createDefault(context);
        loader = ImageLoader.getInstance();
        loader.init(configuration);
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.ic_stub)
                .showImageForEmptyUri(R.drawable.error)
                .showImageOnFail(R.drawable.ic_error)
                .displayer(new RoundedBitmapDisplayer(150))
                .build();
    }

    //添加数据
    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 i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    //获取item布局
    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if (view == null) {
            view = View.inflate(context, R.layout.xlist_item, null);
            holder = new ViewHolder(view);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        Bean.DataBean dataBean = list.get(i);
        holder.tv_title.setText(dataBean.getTitle());
        holder.tv_source.setText(dataBean.getSource());
        holder.tv_x.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                View popupView = View.inflate(context, R.layout.pop_item, null);
                mPopupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
                mPopupWindow.setOutsideTouchable(true);
                mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
                mPopupWindow.showAsDropDown(view, 0, 0);
                popupView.findViewById(R.id.tv_dislike).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        list.remove(i);
                        notifyDataSetChanged();
                        mPopupWindow.dismiss();
                    }
                });
            }
        });
        loader.displayImage(dataBean.getUser_info().getAvatar_url(), holder.iv, options);
        return view;
    }

    //优化类
    public static class ViewHolder {
        View rootView;
        TextView tv_title, tv_source, tv_x;
        ImageView iv;

        public ViewHolder(View rootView) {
            this.rootView = rootView;
            this.tv_title = (TextView) rootView.findViewById(R.id.tv_title);
            this.tv_source = (TextView) rootView.findViewById(R.id.tv_source);
            this.tv_x = (TextView) rootView.findViewById(R.id.tv_x);
            this.iv = (ImageView) rootView.findViewById(R.id.iv);
        }
    }
}

5.跳转页面的显示webview 传值。
public class InfoActivity extends AppCompatActivity {

    private WebView wv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_info);
        wv = (WebView) findViewById(R.id.wv);
        wv.getSettings().setJavaScriptEnabled(true);
        wv.setWebChromeClient(new WebChromeClient());
        wv.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                wv.loadUrl(url);
                return true;
            }-
        });
        wv.loadUrl(getIntent().getStringExtra("weburl"));
    }
}
6.重要布局的格局创建pop_item。
<?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:background="#000"
    android:id="@+id/ll"
    android:orientation="horizontal"
    android:padding="16dp">

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginRight="20dp"
        android:layout_weight="1"
        android:gravity="center"
        android:text="读文章"
        android:textColor="#fff"
        android:textSize="14sp" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginRight="20dp"
        android:layout_weight="1"
        android:gravity="center"
        android:text="收藏"
        android:textColor="#fff"
        android:textSize="14sp" />

    <TextView
        android:id="@+id/tv_dislike"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginRight="20dp"
        android:layout_weight="1"
        android:gravity="center"
        android:text="不感兴趣"
        android:textColor="#fff"
        android:textSize="14sp" />
</LinearLayout>
7.xlist_item的布局布置。
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:layout_toLeftOf="@+id/iv"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center_vertical"
        android:text="标题1"
        android:textSize="24sp" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <TextView
            android:id="@+id/tv_source"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_vertical"
            android:text="标题1"
            android:textSize="22sp" />

        <TextView
            android:id="@+id/tv_x"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_margin="10dp"
            android:text="X" />
    </RelativeLayout>
</LinearLayout>

<ImageView
    android:id="@+id/iv"
    android:layout_width="150dp"
    android:layout_height="150dp"
    android:layout_alignParentRight="true"
    android:src="@mipmap/ic_launcher" />

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值