XML布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawerlayout"
android:fitsSystemWindows="true"
android:clickable="false"
android:focusable="false"
tools:openDrawer="start"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<com.handmark.pulltorefresh.library.PullToRefreshListView
android:id="@+id/pullToRefresh"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
<include
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#e8e8e8"
android:clickable="false"
android:focusable="false"
layout="@layout/layout_left"
/>
</android.support.v4.widget.DrawerLayout>
<?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="300dp"
android:layout_height="match_parent"
android:id="@+id/layout_left"
android:layout_gravity="left"
android:background="#e8e8e8"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
>
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:src="@mipmap/ic_launcher"
android:layout_centerInParent="true"
android:id="@+id/image"
android:clickable="true"
android:focusable="true"
android:layout_gravity="center"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="邮箱"
android:textSize="20sp"
android:layout_gravity="center"
android:layout_marginTop="15dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="邮箱"
android:textSize="20sp"
android:layout_gravity="center"
android:layout_marginTop="15dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="邮箱"
android:textSize="20sp"
android:layout_gravity="center"
android:layout_marginTop="15dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="邮箱"
android:textSize="20sp"
android:layout_gravity="center"
android:layout_marginTop="15dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="邮箱"
android:textSize="20sp"
android:layout_gravity="center"
android:layout_marginTop="15dp"
/>
</LinearLayout>
</LinearLayout>
MAinActivity页面:
package com.example.administrator.monthdemo;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;
import com.example.administrator.adapter.MyAdapter;
import com.example.administrator.bean.Bean;
import com.example.administrator.db.DBDao;
import com.example.administrator.utils.HttpHelper;
import com.example.administrator.utils.NetworkUtils;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity{
int page = 1;
private DrawerLayout mDrawerlayout;
private String urlPath = "http://www.xieast.com/api/news/news.php?page=";
private List<Bean.DataBean> list = new ArrayList<>();
private List<Bean.DataBean> arrayList = new ArrayList<>();
private MyAdapter adapter;
private PullToRefreshListView mPullToRefresh;
private DBDao dbDao;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_main );
initView();
mDrawerlayout.setDrawerListener( new DrawerLayout.DrawerListener() {
@Override
public void onDrawerSlide(@NonNull View drawerView, float slideOffset) {
}
@Override
public void onDrawerOpened(@NonNull View drawerView) {
toast( "侧滑菜单出来了" );
}
@Override
public void onDrawerClosed(@NonNull View drawerView) {
toast( "侧滑菜单回去了" );
}
@Override
public void onDrawerStateChanged(int newState) {
}
} );
/**
* 右侧网络请求
*/
adapter = new MyAdapter(MainActivity.this);
mPullToRefresh.setAdapter(adapter);
adapter.setList(arrayList);
dbDao = new DBDao( MainActivity.this );
boolean connected = NetworkUtils.isConnected( this );
if (connected){
doHttp( page );
mPullToRefresh.setMode( PullToRefreshBase.Mode.BOTH);
mPullToRefresh.setOnRefreshListener( new PullToRefreshBase.OnRefreshListener2<ListView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
page = 1;
arrayList.clear();
doHttp( page );
onStop();
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
page ++;
doHttp(page);
adapter.notifyDataSetChanged();
onStop();
if (page > 10){
toast("没有更多数据了......");
}
}
private void onStop() {
mPullToRefresh.postDelayed(new Runnable() {
@Override
public void run() {
mPullToRefresh.onRefreshComplete();
}
}, 1000);
}
} );
}else {
String data2 = dbDao.query();
Bean bean = new Gson().fromJson( data2, Bean.class );
List<Bean.DataBean> list = bean.getData();
arrayList.addAll( list );
adapter.setList( arrayList );
}
}
private void doHttp(int page) {
new HttpHelper().get(urlPath+page).result( new HttpHelper.HttpListener() {
@Override
public void success(String data) {
dbDao.add( data );
Gson gson = new Gson();
Bean bean = gson.fromJson( data, Bean.class );
list = bean.getData();
arrayList.addAll( list );
adapter.notifyDataSetChanged();
}
@Override
public void fail() {
}
} );
}
private void toast(String data) {
Toast.makeText( MainActivity.this, data, Toast.LENGTH_LONG ).show();
}
private void initView() {
mPullToRefresh = (PullToRefreshListView) findViewById( R.id.pullToRefresh );
mDrawerlayout = (DrawerLayout) findViewById( R.id.drawerlayout );
}
}
自定义适配器MyAdapter:
package com.example.administrator.adapter;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.administrator.bean.Bean;
import com.example.administrator.monthdemo.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.ArrayList;
import java.util.List;
public class MyAdapter extends BaseAdapter{
private List<Bean.DataBean> list = new ArrayList<>();
private Context context;
public MyAdapter(Context context) {
this.context = context;
}
public void setList(List<Bean.DataBean> list) {
this.list = list;
notifyDataSetChanged();
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getItemViewType(int position) {
if(list.get(position).getThumbnail_pic_s02()!=null&&list.get(position).getThumbnail_pic_s03()!=null){
return 0;//有三个图片以上的,展示规则是,上面是title下面是图片
}
return 1;//
}
@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;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder1 holder1;
ViewHolder2 holder2;
int type = getItemViewType( i );
DisplayImageOptions options = new DisplayImageOptions.Builder()
.cacheOnDisk( true )
.bitmapConfig( Bitmap.Config.RGB_565)
.build();
switch (type){
case 1:
if (view == null){
view = View.inflate( context, R.layout.layout_item1,null );
holder1 = new ViewHolder1();
holder1.title = view.findViewById( R.id.title);
holder1.imageView = view.findViewById( R.id.img_pic );
view.setTag( holder1 );
}else {
holder1 = (ViewHolder1) view.getTag();
}
holder1.title.setText( list.get( i ).getTitle() );
ImageLoader.getInstance().displayImage( list.get( i ).getThumbnail_pic_s(),holder1.imageView ,options);
break;
case 0:
if (view == null){
view = View.inflate( context, R.layout.layout_item2,null );
holder2 = new ViewHolder2();
holder2.title = view.findViewById( R.id.title);
holder2.img_pic1 = view.findViewById( R.id.img_pic1 );
holder2.img_pic2 = view.findViewById( R.id.img_pic2 );
holder2.img_pic3 = view.findViewById( R.id.img_pic3 );
view.setTag( holder2 );
}else {
holder2 = (ViewHolder2) view.getTag();
}
holder2.title.setText( list.get( i ).getTitle() );
ImageLoader.getInstance().displayImage( list.get( i ).getThumbnail_pic_s(),holder2.img_pic1,options );
ImageLoader.getInstance().displayImage( list.get( i ).getThumbnail_pic_s02(),holder2.img_pic2,options );
ImageLoader.getInstance().displayImage( list.get( i ).getThumbnail_pic_s03(),holder2.img_pic3 ,options);
break;
}
return view;
}
class ViewHolder1{
TextView title;
ImageView imageView;
}
class ViewHolder2{
TextView title;
ImageView img_pic1,img_pic2,img_pic3;
}
}
创建数据库并使用:
package com.example.administrator.db;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBHelper extends SQLiteOpenHelper {
public DBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL("create table why (id integer primary key autoincrement,data text)");
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
package com.example.administrator.db;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import java.util.ArrayList;
import java.util.List;
public class DBDao {
private final SQLiteDatabase db;
public DBDao(Context context) {
DBHelper dbHelper = new DBHelper(context, "why.db", null, 1);
db = dbHelper.getWritableDatabase();
}
public long add(String data){
ContentValues values = new ContentValues();
values.put("data",data);
return db.insert("why",null,values);
}
public String query(){
String data = "";
Cursor cursor = db.query("why", null, null, null, null, null, null);
while (cursor.moveToNext()){
data = cursor.getString(cursor.getColumnIndex("data"));
}
return data;
}
}