一个完整的工程。
代码。
AndroidManifest.xml中
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bwei.czx.chengzexiang1507d0918">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity"></activity>
</application>
</manifest>
activity_main.xml中:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.bwei.czx.chengzexiang1507d0918.MainActivity">
<com.limxing.xlistview.view.XListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/xlistview">
</com.limxing.xlistview.view.XListView>
</LinearLayout>
activity_second中:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context="com.bwei.czx.chengzexiang1507d0918.SecondActivity">
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/web">
</WebView>
</LinearLayout>
item_layout中:
<?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">
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:orientation="vertical"
android:layout_height="150dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:id="@+id/item_title"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/item_source"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="40dp"
android:id="@+id/item_remove"/>
</LinearLayout>
<ImageView
android:layout_width="200dp"
android:layout_height="150dp"
android:id="@+id/item_img"/>
</LinearLayout>
mypopwindow_layout.xml中:
<?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="#0ff">
<TextView
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="match_parent"
android:textSize="30sp"
android:gravity="center"
android:text="不感兴趣"
android:id="@+id/popname"/>
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@mipmap/abc_ic_clear_search_api_holo_light"
android:id="@+id/popguan"/>
</LinearLayout>
MainActivity中:
package com.bwei.czx.chengzexiang1507d0918;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import com.google.gson.Gson;
import com.limxing.xlistview.view.XListView;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
/**
* 主页面
*/
public class MainActivity extends AppCompatActivity implements XListView.IXListViewListener {
private XListView xListView;
private boolean flag;
private MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//找控件
xListView = (XListView) findViewById(R.id.xlistview);
xListView.setPullLoadEnable(true);
xListView.setXListViewListener(this);
ForData();
}
public void ForData(){
getData("http://ic.snssdk.com/2/article/v25/stream/?category=news_car&count=20&bd_city=%E5%8C%97%E4%BA%AC%E5%B8%82&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");
}
//请求数据
public void getData(String url){
new AsyncTask<String,Void,String>(){
@Override
protected void onPostExecute(String s) {
if(s == null){
return;
}
Gson gson = new Gson();
MyDataBean myDataBean = gson.fromJson(s, MyDataBean.class);
final List<MyDataBean.DataBean> beanList = myDataBean.getData();
//适配
if(adapter == null){
adapter = new MyAdapter(MainActivity.this,beanList);
xListView.setAdapter(adapter);
}else{
adapter.loadMore(beanList,flag);
}
xListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
intent.putExtra("url",beanList.get(position-1).getUrl());
startActivity(intent);
}
});
}
@Override
protected String doInBackground(String... params) {
try {
URL url1 = new URL(params[0]);
HttpURLConnection connection = (HttpURLConnection) url1.openConnection();
connection.setRequestMethod("GET");
connection.setReadTimeout(5000);
connection.setConnectTimeout(5000);
int code = connection.getResponseCode();
if(code == 200){
InputStream is = connection.getInputStream();
return StreamTools.getstr(is);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}.execute(url);
}
//上拉加载,下拉刷新
@Override
public void onRefresh() {
ForData();
flag = true;
xListView.stopRefresh(true);
}
@Override
public void onLoadMore() {
ForData();
flag = false;
xListView.stopLoadMore();
}
}
SecondActivity中:
package com.bwei.czx.chengzexiang1507d0918;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
/**
* webview界面
*/
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
WebView web = (WebView) findViewById(R.id.web);
Intent intent = getIntent();
//Webview展示
String url = intent.getStringExtra("url");
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl(url);
}
}
MyAdapter中:
package com.bwei.czx.chengzexiang1507d0918;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.List;
/**适配器
* Created by czx on 2017/9/18.
*/
public class MyAdapter extends BaseAdapter {
private Context context;
private List<MyDataBean.DataBean> list;
public MyAdapter(Context context, List<MyDataBean.DataBean> list) {
this.context = context;
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 position;
}
public void loadMore(List<MyDataBean.DataBean> beanList,boolean flag){
if(flag){
list.addAll(0,beanList);
}else{
list.addAll(beanList);
}
notifyDataSetChanged();
}
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
ViewHolder viewHolder;
if(convertView == null){
convertView = View.inflate(context,R.layout.item_layout,null);
viewHolder = new ViewHolder();
viewHolder.img = (ImageView) convertView.findViewById(R.id.item_img);
viewHolder.title = (TextView) convertView.findViewById(R.id.item_title);
viewHolder.source = (TextView) convertView.findViewById(R.id.item_source);
viewHolder.remove = (ImageView) convertView.findViewById(R.id.item_remove);
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder) convertView.getTag();
}
ImageLoader.getInstance().displayImage(list.get(position).getMiddle_image().getUrl(),viewHolder.img,StreamTools.getOptions());
viewHolder.title.setText(list.get(position).getTitle());
viewHolder.source.setText(list.get(position).getSource());
viewHolder.remove.setImageResource(R.mipmap.abc_ic_clear_search_api_holo_light);
viewHolder.remove.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LayoutInflater inflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View vPopWindow=inflater.inflate(R.layout.mypopwindow_layout, null);
final PopupWindow popWindow = new PopupWindow(vPopWindow,300,300,true);
TextView name = (TextView) vPopWindow.findViewById(R.id.popname);
ImageView guan = (ImageView) vPopWindow.findViewById(R.id.popguan);
name.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
list.remove(list.get(position));
notifyDataSetChanged();
popWindow.dismiss();
}
});
guan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popWindow.dismiss();
}
});
popWindow.showAtLocation(parent,Gravity.CENTER,0,0);
}
});
return convertView;
}
class ViewHolder{
TextView title;
TextView source;
ImageView img;
ImageView remove;
}
}
MyApplication中:
package com.bwei.czx.chengzexiang1507d0918;
import android.app.Application;
import android.os.Environment;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import java.io.File;
/**
* Created by czx on 2017/9/18.
*/
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
String path = Environment.getExternalStorageDirectory()+"1507D0918";
File file = new File(path);
ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(this)
.threadPriority(100)
.threadPoolSize(3)
.memoryCacheSize(2*1024*1024)
.memoryCacheExtraOptions(400,800)
.diskCacheSize(50*1024*1024)
.diskCacheFileNameGenerator(new Md5FileNameGenerator())
.diskCache(new UnlimitedDiskCache(file))
.build();
ImageLoader.getInstance().init(configuration);
}
}
写一个Bean:
StreamTools中:
package com.bwei.czx.chengzexiang1507d0918;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.display.CircleBitmapDisplayer;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
/**解析
* Created by czx on 2017/9/18.
*/
public class StreamTools {
public static String getstr(InputStream is){
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int len;
byte[] arr = new byte[1024];
while((len = is.read(arr)) != -1){
bos.write(arr,0,len);
}
return bos.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static DisplayImageOptions getOptions(){
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showImageOnFail(R.mipmap.ic_error)
.showImageOnLoading(R.mipmap.loading)
.displayer(new CircleBitmapDisplayer())
.build();
return options;
}
}