//适配器
package com.example.mn;
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.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.ArrayList;
public class XListAdapder extends BaseAdapter {
private Context mContext;
private ArrayList<User.DataBean> list;
public XListAdapder(Context mContext, ArrayList<User.DataBean> list) {
this.mContext = mContext;
this.list = list;
}
DisplayImageOptions options = new DisplayImageOptions.Builder()
.bitmapConfig(Bitmap.Config.RGB_565)
.build();
@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 int getViewTypeCount() {
return 2;
}
@Override
public int getItemViewType(int position) {
int type = 0;
if (list.get(position).getThumbnail_pic_s() != null && list.get(position).getThumbnail_pic_s02() != null && list.get(position).getThumbnail_pic_s03() != null) {
type = 1;
} else if (list.get(position).getThumbnail_pic_s03() == null) {
type = 0;
}
return type;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
int type = getItemViewType(position);
switch (type) {
case 0:
ViewHolder holder = null;
if (convertView == null) {
convertView = View.inflate(mContext, R.layout.item, null);
holder = new ViewHolder();
holder.name = convertView.findViewById(R.id.tv);
holder.image2 = convertView.findViewById(R.id.image2);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.name.setText(list.get(position).getAuthor_name());
ImageLoader.getInstance().displayImage(list.get(position).getThumbnail_pic_s(),holder.image2,options);
break;
case 1:
ViewHolder2 holder2 = null;
if (convertView == null) {
convertView = View.inflate(mContext, R.layout.item_one, null);
holder2 = new ViewHolder2();
holder2.aaa = convertView.findViewById(R.id.aaa);
holder2.a = convertView.findViewById(R.id.a);
holder2.b = convertView.findViewById(R.id.b);
holder2.c = convertView.findViewById(R.id.c);
convertView.setTag(holder2);
} else {
holder2 = (ViewHolder2) convertView.getTag();
}
holder2.aaa.setText(list.get(position).getAuthor_name());
ImageLoader.getInstance().displayImage(list.get(position).getThumbnail_pic_s(),holder2.a,options);
ImageLoader.getInstance().displayImage(list.get(position).getThumbnail_pic_s02(),holder2.b,options);
ImageLoader.getInstance().displayImage(list.get(position).getThumbnail_pic_s03(),holder2.b,options);
break;
}
return convertView;
}
class ViewHolder {
TextView name;
ImageView image2;
}
class ViewHolder2 {
TextView aaa;
ImageView a, b, c;
}
}
//设置数据库
package com.example.mn;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class MySQL extends SQLiteOpenHelper {
public MySQL( Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, “baidu.db”, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE GOOD(id integer primary key AUTOINCREMENT,name varchar(20))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
//进行侧滑和网络判断,viewpager+fragemnt联动
package com.example.mn;
import android.app.Fragment;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
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 java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
public static DrawerLayout drawerLayout;
private ViewPager viewPager;
private RadioGroup radioGroup;
private ActionBarDrawerToggle toggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawerLayout = findViewById(R.id.drawerLayout);
viewPager = findViewById(R.id.vp);
radioGroup = findViewById(R.id.rg);
boolean b = initDot(MainActivity.this);
if (b) {
initDrawe();
initViewpagerFragenmt();
} else {
Toast.makeText(MainActivity.this, "请检查网络是否连接", Toast.LENGTH_SHORT).show();
}
}
private void initViewpagerFragenmt() {
ArrayList<Fragment> list = new ArrayList<>();
list.add(new viewpager_one_Fragment());
list.add(new viewpager_two_Fragment());
list.add(new viewpager_three_Fragment());
MyFragemnt myFragemnt = new MyFragemnt(getFragmentManager());
myFragemnt.MyA(list);
viewPager.setAdapter(myFragemnt);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int i, float v, int i1) {
}
@Override
public void onPageSelected(int i) {
switch (i){
case 0:
radioGroup.check(1);
break;
case 1:
radioGroup.check(2);
break;
case 2:
radioGroup.check(3);
break;
}
}
@Override
public void onPageScrollStateChanged(int i) {
}
});
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId){
case 1:
viewPager.setCurrentItem(0);
break;
case 2:
viewPager.setCurrentItem(1);
break;
case 3:
viewPager.setCurrentItem(2);
break;
}
}
});
}
//判断是否有网
private boolean initDot(Context context) {
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo info = manager.getActiveNetworkInfo();
if (info != null && info.getType() == manager.TYPE_WIFI) {
return true;
}
return false;
}
private void initDrawe() {
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
toggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.open, R.string.colse);
toggle.syncState();
drawerLayout.addDrawerListener(toggle);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (toggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
//xlistview列表展示并添加到数据库
package com.example.mn;
import android.app.Fragment;
import android.content.ContentValues;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.bawei.xlistviewlibrary.XListView;
import com.google.gson.Gson;
import java.util.ArrayList;
/**
-
A simple {@link Fragment} subclass.
*/
public class viewpager_one_Fragment extends Fragment {private XListView xListView;
private String poath=“http://www.xieast.com/api/news/news.php?page=1”;
private ArrayList<User.DataBean> list = new ArrayList<>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = View.inflate(getActivity(),R.layout.fragment_viewpager_one_,null);
xListView = view.findViewById(R.id.xlv);
initXlistview();
Mysqlite mysqlite = new Mysqlite(getActivity());
for(int a=0;a<list.size();a++){
ContentValues values = new ContentValues();
values.put(“name”,list.get(a).getAuthor_name());
mysqlite.insert(“GOOD”,null,values);
}
return view;
}private void initXlistview() {
helpListener helpListener = new helpListener(new helpListener.GetData() {
@Override
public void Data(String str) {
Gson gson = new Gson();
User user = gson.fromJson(str,User.class);
list.addAll(user.getData());
xListView.setAdapter(new XListAdapder(getActivity(),list));} }); helpListener.execute(poath);
}
}