1.判断网络状态
package com.bw.qgs.shipei;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
public class NewThread {
public boolean isNetWork(Context context){
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if(networkInfo!=null){
return true;
}
return false;
}
}
2.主页面中viewpager+drawerlayout
<?xml version="1.0" encoding="utf-8"?><android.support.v4.widget.DrawerLayout xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:tools=“http://schemas.android.com/tools”
android:id="@+id/draw"
android:layout_width=“match_parent”
android:layout_height=“match_parent”
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<RadioGroup
android:id="@+id/rg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/bt1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:button="@null"
android:checked="true"
android:drawableTop="@drawable/sel1"
android:gravity="center"
android:text="关注" />
<RadioButton
android:id="@+id/bt2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:button="@null"
android:drawableTop="@drawable/sel2"
android:gravity="center"
android:text="消息" />
<RadioButton
android:id="@+id/bt3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:button="@null"
android:drawableTop="@drawable/sel3"
android:gravity="center"
android:text="我的" />
</RadioGroup>
</LinearLayout>
<fragment
class="com.bw.qgs.shipei.fragment.BlankFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start" />
</android.support.v4.widget.DrawerLayout>
package com.bw.qgs.shipei;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.widget.RadioGroup;
import android.widget.Toast;
import com.bw.qgs.shipei.adapter.LAdapter;
import com.bw.qgs.shipei.fragment.OneFragment;
import com.bw.qgs.shipei.fragment.ThreeFragment;
import com.bw.qgs.shipei.fragment.TwoFragment;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private ViewPager pager;
private DrawerLayout draw;
private ActionBarDrawerToggle toggle;
private RadioGroup rg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
initTwoData();
}
private void initTwoData() {
ArrayList<Fragment> list = new ArrayList<>();
list.add(new OneFragment());
list.add(new TwoFragment());
list.add(new ThreeFragment());
LAdapter adapter = new LAdapter(getFragmentManager());
adapter.setList(list);
pager.setAdapter(adapter);
pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int i, float v, int i1) {
}
@Override
public void onPageSelected(int i) {
rg.check(rg.getChildAt(i).getId());
}
@Override
public void onPageScrollStateChanged(int i) {
}
});
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId){
case R.id.bt1:
pager.setCurrentItem(0);
break;
case R.id.bt2:
pager.setCurrentItem(1);
break;
case R.id.bt3:
pager.setCurrentItem(2);
break;
}
}
});
}
private void initData() {
android.support.v7.app.ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
toggle = new ActionBarDrawerToggle(this, draw, R.string.open, R.string.close);
toggle.syncState();
draw.addDrawerListener(toggle);
}
private void initView() {
pager = (ViewPager) findViewById(R.id.pager);
draw = (DrawerLayout) findViewById(R.id.draw);
rg = (RadioGroup) findViewById(R.id.rg);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(toggle.onOptionsItemSelected(item)){
return true;
}
return super.onOptionsItemSelected(item);
}
}
3.mysqlite
package com.bw.qgs.shipei.sqlitelite;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class MySqlite extends SQLiteOpenHelper {
public MySqlite(Context context) {
super(context, “bw.db”, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table student(id INTEGER PRIMARY KEY AUTOINCREMENT,author_name varchar(40))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
4.sqldao层
package com.bw.qgs.shipei.sqlitelite;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class SQLDao {
private Context context;
private SQLiteDatabase db;
public SQLDao(Context context) {
this.context = context;
MySqlite sqlite = new MySqlite(context);
db = sqlite.getReadableDatabase();
}
public long insert(String table, String nullColumnHack, ContentValues values){
return db.insert(table,nullColumnHack,values);
}
public Cursor query(String table, String[] columns, String selection,
String[] selectionArgs, String groupBy, String having,
String orderBy){
return db.query(table,columns,selection,selectionArgs,groupBy,having,orderBy);
}
}
5.onefragment
<com.handmark.pulltorefresh.library.PullToRefreshListView
android:id="@+id/pullToRefreshListView"
android:layout_width=“match_parent”
android:layout_height=“match_parent”
/>
public class OneFragment extends Fragment {
private int cc=1;
private String path = "http://www.xieast.com/api/news/news.php?page=";
private PullToRefreshListView listview;
private ArrayList<User.DataBean> list = new ArrayList<User.DataBean>();
private User user;
private ArrayList<Bean> list1 = new ArrayList<Bean>();
private OneAdapter adapter;
private ListView refreshableView;
private SQLDao mDao;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_one, container, false);
listview = view.findViewById(R.id.pullToRefreshListView);
listview.setMode(PullToRefreshListView.Mode.BOTH);
refreshableView = listview.getRefreshableView();
NewThread newThread = new NewThread();
mDao = new SQLDao(getActivity());
boolean netWork = newThread.isNetWork(getActivity());
if(netWork){
Toast.makeText(getActivity(),"连接成功",Toast.LENGTH_SHORT).show();
sta(cc);
Toast.makeText(getActivity(),"添加成功",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getActivity(),"连接失败",Toast.LENGTH_SHORT).show();
Cursor cursor = mDao.query("student", null, null, null, null, null, null);
if (cursor.moveToFirst()){
do{
String author_name = cursor.getString(cursor.getColumnIndex("author_name"));
list1.add(new Bean(author_name));
}while (cursor.moveToNext());
}
listview.setAdapter(adapter);
}
listview.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
sta(cc);
new GetDataTask().execute();
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
cc++;
sta(cc);
new GetDataTask().execute();
}
});
return view;
}
public void sta(int cc){
Gong gong = new Gong();
gong.get(path+cc).getGong(new Gong.HttpGong() {
@Override
public void getName(String name) {
Gson gson = new Gson();
user = gson.fromJson(name, User.class);
list.addAll(user.getData());
adapter = new OneAdapter(getActivity(),list);
listview.setAdapter(adapter);
for (int i = 0;i<list.size();i++){
ContentValues contentValues = new ContentValues();
contentValues.put("author_name",list.get(i).getAuthor_name());
mDao.insert("student",null,contentValues);
}
}
});
}
private class GetDataTask extends AsyncTask<Void,Void,String[]>{
@Override
protected String[] doInBackground(Void... voids) {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return new String[0];
}
@Override
protected void onPostExecute(String[] strings) {
list.addAll(user.getData());
adapter.notifyDataSetChanged();
listview.onRefreshComplete();
super.onPostExecute(strings);
}
}
}
侧页面
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_blank, container, false);
view.findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(list == null){
list = new ArrayList<>();
list.add(new ChannelBean("军事",true));
list.add(new ChannelBean("八卦",true));
list.add(new ChannelBean("游戏",true));
list.add(new ChannelBean("宠物",true));
list.add(new ChannelBean("汽车",false));
list.add(new ChannelBean("热卖",false));
list.add(new ChannelBean("外卖",false));
list.add(new ChannelBean("太阳花",false));
list.add(new ChannelBean("九三",false));
list.add(new ChannelBean("八嘎",false));
list.add(new ChannelBean("色昂",false));
ChannelActivity.startChannelActivity((AppCompatActivity) getActivity(),list);
}else if(jsonStr !=null){
ChannelActivity.startChannelActivity((AppCompatActivity) getActivity(),jsonStr);
}
}
});
return view;
}
/*public void button(View v){
if(list == null){
list = new ArrayList<>();
list.add(new ChannelBean("军事",true));
list.add(new ChannelBean("八卦",true));
list.add(new ChannelBean("游戏",true));
list.add(new ChannelBean("宠物",true));
list.add(new ChannelBean("汽车",false));
list.add(new ChannelBean("热卖",false));
list.add(new ChannelBean("外卖",false));
list.add(new ChannelBean("太阳花",false));
list.add(new ChannelBean("九三",false));
list.add(new ChannelBean("八嘎",false));
list.add(new ChannelBean("色昂",false));
ChannelActivity.startChannelActivity((AppCompatActivity) getActivity(),list);
}else if(jsonStr !=null){
ChannelActivity.startChannelActivity((AppCompatActivity) getActivity(),jsonStr);
}
}*/
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == ChannelActivity.RESULT_CODE && requestCode == ChannelActivity.REQUEST_CODE){
jsonStr = data.getStringExtra(ChannelActivity.RESULT_JSON_KEY);
}
}
}