还要导入xlistview的model:
然后再AndroidManifest.xml中application中注册
android:name=".BaseApplication"
这是自己写的工具类
---------------------------------------
public class BaseApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
x.Ext.init(this);
x.Ext.setDebug(BuildConfig.DEBUG);
}
import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; /** * 类描述:sqlite的工具类,创建数据库和表,存储数据 * 姓名 : */ public class MySQLiteOpenHelper extends SQLiteOpenHelper { public MySQLiteOpenHelper(Context context) { super(context, "news.db", null, 1); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("create table news(id Integer primary key autoincrement,content text)"); } @Override public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { } }
}
--------------------------------------------------------------------------
import android.os.Bundle; import android.support.design.widget.TabLayout; import android.support.v4.app.Fragment; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private TabLayout tab; private ViewPager vp; private List-----------------------------------------------------------------------------listTitle; private List fragmentList = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initData(); } private void initData() { listTitle = TitleBean.getAllTitle(); for (int i = 0; i < listTitle.size(); i++) { MyFragment myFragment = new MyFragment(); fragmentList.add(myFragment); } MyViewPagerAdapter adapter = new MyViewPagerAdapter(getSupportFragmentManager(), fragmentList, listTitle); vp.setAdapter(adapter); tab.setupWithViewPager(vp); } private void initView() { tab = (TabLayout) findViewById(R.id.tab); vp = (ViewPager) findViewById(R.id.vp); } }
main关联的布局:
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
android:layout_height="50dp"
android:background="#EA2000">
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="8dp"
android:text="今日头条"
android:textColor="#ffffff"
android:textSize="30sp" />
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="scrollable"
app:tabIndicatorColor="#EA2000"
app:tabSelectedTextColor="#EA2000"
>
android:layout_height="match_parent"
android:id="@+id/vp"
>
import android.content.ComponentName; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.support.v7.app.AlertDialog; public class ConnectionUtil { /* * 判断网络连接是否已开 *true 已打开 false 未打开 * */ public static boolean isConn(Context context){ boolean bisConnFlag=false; ConnectivityManager conManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo network = conManager.getActiveNetworkInfo(); if(network!=null){ bisConnFlag=conManager.getActiveNetworkInfo().isAvailable(); } return bisConnFlag; } /* * 打开设置网络界面 * */ public static void setNetworkMethod(final Context context){ //提示对话框 AlertDialog.Builder builder=new AlertDialog.Builder(context); builder.setTitle("网络设置提示").setMessage("网络连接不可用,是否进行设置?").setPositiveButton("设置", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub Intent intent=null; //判断手机系统的版本 即API大于10 就是3.0或以上版本 if(android.os.Build.VERSION.SDK_INT>10){ intent = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS); }else{ intent = new Intent(); ComponentName component = new ComponentName("com.android.settings","com.android.settings.WirelessSettings"); intent.setComponent(component); intent.setAction("android.intent.action.VIEW"); } context.startActivity(intent); } }).setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub dialog.dismiss(); } }).show(); } }--------------------------------------------------
fragment类,继承Fragment,关联布局,用来切换显示,同时用来将数据存入数据库和在判断没有网络的情况下查询显示数据库中存储的数据
import android.content.ContentValues; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.google.gson.Gson; import org.xutils.common.Callback; import org.xutils.http.RequestParams; import org.xutils.x; import java.util.ArrayList; import java.util.List; import me.maxwin.view.XListView; public class MyFragment extends Fragment{ private SQLiteDatabase db; private String json; private List--------------------------------------------------------------------------------------------list = new ArrayList<>(); String url = "http://api.expoon.com/AppNews/getNewsList/type/1/p/1"; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return inflater.inflate(R.layout.item_layout, container, false); } private XListView xLv; //private List list=new ArrayList<>(); @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); xLv = (XListView) getView().findViewById(R.id.xLv); getDataFromServer(); // if (!ConnectionUtil.isConn(getContext())) { // // //没网就进行网络设置 // ConnectionUtil.setNetworkMethod(getContext()); // //从缓存读取 // // queryData(); // // } else { // //请求数据的方法 // getDataFromServer(); // } } //查询数据库里面的数据 private void queryData() { Cursor cursor = db.query("news", null, null, null, null, null, null,null); if (cursor != null) { while (cursor.moveToNext()) { json = cursor.getString(cursor.getColumnIndex("content")); } Gson gson = new Gson(); Bean bean = gson.fromJson(json, Bean.class); list.addAll(bean.getData()); MyListViewAdapter adapter=new MyListViewAdapter(getContext(),list); xLv.setAdapter(adapter); adapter.notifyDataSetChanged(); } } //插入数据到数据库 private void insertData(String str){ MySQLiteOpenHelper mySQLiteOpenHelper=new MySQLiteOpenHelper(getContext()); db = mySQLiteOpenHelper.getWritableDatabase(); ContentValues values=new ContentValues(); values.put("content",str); db.insert("news",null,values); } private void getDataFromServer() { RequestParams params = new RequestParams(); params.setUri(url); x.http().get(params, new Callback.CommonCallback() { @Override public void onSuccess(String result) { // insertData(result); Gson gson = new Gson(); Bean bean = gson.fromJson(result, Bean.class); list.addAll(bean.getData()); } @Override public void onError(Throwable ex, boolean isOnCallback) { } @Override public void onCancelled(CancelledException cex) { } @Override public void onFinished() { MyListViewAdapter adapter = new MyListViewAdapter(getContext(), list); xLv.setAdapter(adapter); adapter.notifyDataSetChanged(); } }); } }
//fragment的布局
android:layout_height="match_parent">
android:layout_width="match_parent"
android:layout_height="match_parent">
---------------------------------------------------------------------------------------------------------
viewpager的//FragmentPagerAdapter 适配器
import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import java.util.List; /** * 类描述:/FragmentPagerAdapter 适配器 * 姓名 : */ public class MyViewPagerAdapter extends FragmentPagerAdapter { private List-------------------------------------------------------------------------------------------------------fragmentList; private List titleList; public MyViewPagerAdapter(FragmentManager fm, List fragmentList, List titleList) { super(fm); this.fragmentList = fragmentList; this.titleList = titleList; } @Override public Fragment getItem(int position) { return fragmentList.get(position); } @Override public int getCount() { return fragmentList.size(); } @Override public CharSequence getPageTitle(int position) { return titleList.get(position); } }
listview显示数据的适配器
import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; import org.xutils.x; import java.util.List; /** * 类描述: * 姓名 :刘希鑫 */ public class MyListViewAdapter extends BaseAdapter { private Context context; private List-------------------------------------------------------------------------------------------list; public MyListViewAdapter(Context context, List 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; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; if (convertView == null) { convertView = LayoutInflater.from(context).inflate(R.layout.base_layout, null); holder = new ViewHolder(); holder.tvTitle = (TextView) convertView.findViewById(R.id.tvTitle); holder.tvContent = (TextView) convertView.findViewById(R.id.tvContent); holder.ivIcon = (ImageView) convertView.findViewById(R.id.ivIcon); convertView.setTag(holder); } else { holder= (ViewHolder) convertView.getTag(); } holder.tvTitle.setText(list.get(position).getNews_title()); holder.tvContent.setText(list.get(position).getNews_summary()); x.image().bind(holder.ivIcon,list.get(position).getPic_url()); return convertView; } static class ViewHolder { private TextView tvTitle, tvContent; private ImageView ivIcon; }}
//BaseAdapter的布局
android:layout_height="match_parent"
android:orientation="vertical">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp" />
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
android:layout_width="match_parent"
android:layout_height="200dp" />
-----------------=----------------------------------------------------------------
import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; /** * 类描述:sqlite的工具类,创建数据库和表,存储数据 * 姓名 : */ public class MySQLiteOpenHelper extends SQLiteOpenHelper { public MySQLiteOpenHelper(Context context) { super(context, "news.db", null, 1); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("create table news(id Integer primary key autoincrement,content text)"); } @Override public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { } }----------------------------------------------------------------------------------------
//Tablayout的标题工具类
public class TitleBean {
private int id;
private String name;
public static List
List
listTitle.add("推荐");
listTitle.add("科技");
listTitle.add("电影");
listTitle.add("娱乐");
listTitle.add("头条");
listTitle.add("游戏");
listTitle.add("逃狱");
listTitle.add("历史");
listTitle.add("民俗");
return listTitle;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "TitleBean{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
用来解析json串的bean类自己根据获得数据的接口创建即可