依赖Gradle://classpath 'com.android.tools.build:gradle:3.0.1'(最后导架宝)
权限://
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawer"
tools:context="com.example.com.zongfuxi.MainActivity">
<LinearLayout
android:layout_width="200dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_gravity="start"
android:id="@+id/lly">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@mipmap/ic_launcher"
android:id="@+id/iv"
android:layout_marginTop="10dp"/>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lv"
android:layout_marginTop="20dp"/>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
private String Url = "http://api.expoon.com/AppNews/getNewsList/type/1/p/5";
private String imageUrl = "http://b370.photo.store.qq.com/psb?/V10b76GM3PIW9e/k.qjVvliBboyrovmVbqlkA*BpEdT4tstJhpD3Ro3nBQ!/b/dHIBAAAAAAAA&bo=VgOAAgAAAAAFB*M!&rf=viewer_4";
@SuppressLint("StaticFieldLeak")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawer = findViewById(R.id.drawer);
lv = findViewById(R.id.lv);
lly = findViewById(R.id.lly);
iv = findViewById(R.id.iv);
//图片设置
imageLoaderInstance = ImageLoader.getInstance();
imageLoaderInstance.displayImage(imageUrl, iv);
new AsyncTask<String,Integer,String>(){
@Override
protected String doInBackground(String... strings) {
Util util = new Util();
String getjson = util.getjson(Url);
return getjson;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Gson gson = new Gson();
Bean bean = gson.fromJson(s, Bean.class);
List<Bean.DataBean> data = bean.getData();
Madapter madapter = new Madapter(MainActivity.this,data);
lv.setAdapter(madapter);
}
}.execute();
//点击关闭侧拉
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
drawer.closeDrawer(lly);
Toast.makeText(MainActivity.this,"点击",Toast.LENGTH_SHORT).show();
}
});
}
//数据请求工具类
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class Util {
public String getjson(String url){
URL url1 = null;
HttpURLConnection connection = null;
String str = "";
try {
url1 = new URL(url);
connection = (HttpURLConnection) url1.openConnection();
connection.setConnectTimeout(8000);
connection.setReadTimeout(8000);
int code = connection.getResponseCode();
if (code == 200){
InputStream inputStream = connection.getInputStream();
byte[] b =new byte[1024];
int len = 0;
while ((len = inputStream.read(b))!=-1){
str+=new String(b,0,len);
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
//适配器
public class Madapter extends BaseAdapter{
private Context context;
private List<Bean.DataBean> data;
private ViewHolder holder;
public Madapter(Context context, List<Bean.DataBean> data) {
this.context = context;
this.data = data;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int i) {
return data.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
if (view == null){
holder = new ViewHolder();
view = View.inflate(context, R.layout.item, null);
holder.text = view.findViewById(R.id.text);
holder.iv = view.findViewById(R.id.iv);
view.setTag(holder);
}else{
holder = (ViewHolder) view.getTag();
}
ImageLoader.getInstance().displayImage(lists.get(i).getPic_url(),holder.iv);
holder.text.setText(data.get(i).getNews_title());
return view;
}
class ViewHolder{
private ImageView iv;
private TextView text;
}
}
//app图片设置
/**
* <application
* android:name=".Mapp"
**/
public class Mapp extends Application {
File cacheFile = new File(Environment.getExternalStorageDirectory() + "/" + "image");
@Override
public void onCreate() {
super.onCreate();
//初始化组件,链式开发思想,整个框架的参数初始化配置
//imageLoader的全局配置
ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(this)
.memoryCacheExtraOptions(480, 800) // default = device screen dimensions 内存缓存文件的最大长宽
.diskCacheExtraOptions(480, 800, null) // 本地缓存的详细信息(缓存的最大长宽),最好不要设置这个
.tasksProcessingOrder(QueueProcessingType.FIFO) // default
.denyCacheImageMultipleSizesInMemory()
.memoryCache(new LruMemoryCache(2 * 1024 * 1024)) //可以通过自己的内存缓存实现
.memoryCacheSize(2 * 1024 * 1024) // 内存缓存的最大值
.memoryCacheSizePercentage(13) // default
.diskCacheSize(50 * 1024 * 1024) // 50 Mb sd卡(本地)缓存的最大值
.diskCacheFileCount(100) // 可以缓存的文件数量
.diskCache(new UnlimitedDiskCache(cacheFile))//自定义缓存目录
// default为使用HASHCODE对UIL进行加密命名, 还可以用MD5(new Md5FileNameGenerator())加密
.diskCacheFileNameGenerator(new HashCodeFileNameGenerator())
.defaultDisplayImageOptions(DisplayImageOptions.createSimple()) // default
.writeDebugLogs() // 打印debug log
.build();
ImageLoader.getInstance().init(configuration);
}
}