<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<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"/>
<application
android:name=".appliction"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
public class MainActivity extends AppCompatActivity implements XListView.IXListViewListener{
private List<String > imagelist=new ArrayList<>();
private ViewPager pager;
private Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
int what=msg.what;
pager.setCurrentItem(what);
}
};
private RadioGroup rg;
private int con=0;
private TextView night;
private XListView xlv;
private int page=1;
private boolean flag;
private List<Dao> dblist=new ArrayList<>();
private MyBaseadapter adapter;
private List<Bean.ListBean> list;
private SQLiteDatabase db;
private DbBaseadapter ada;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//实例化数据库
Helper helper = new Helper(MainActivity.this);
db = helper.getWritableDatabase();
//7 点击右上角的地图按钮,实现日夜间模式切换(10分)
night = (TextView) findViewById(R.id.night);
night.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
yejian();
}
});
// 实现图片无限轮播,并添加图片轮播指示器(各5分,10分)
pager = (ViewPager) findViewById(R.id.pager);
rg = (RadioGroup) findViewById(R.id.rg);
imagelist.add("https://img10.360buyimg.com/da/jfs/t4747/277/1368712300/170619/35098d7f/58f038e0N9b3a0ca5.jpg");
imagelist.add("https://img14.360buyimg.com/da/jfs/t4915/21/1427207714/81116/b005bb06/58f08963Ndb295b3c.jpg");
imagelist.add("https://img13.360buyimg.com/da/jfs/t4651/104/2867456043/68336/99da4c16/58f41eaeN5b614a63.jpg");
pager.setAdapter(new My());
jian();
new Thread() {
@Override
public void run() {
while (true) {
try {
Thread.sleep(1000);
con++;
handler.sendEmptyMessage(con);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}.start();
xlv = (XListView) findViewById(R.id.xlv);
xlv.setPullLoadEnable(true);
xlv.setXListViewListener(this);
//判断是否有网
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
Toast.makeText(MainActivity.this,"有网", Toast.LENGTH_SHORT).show();
data();
}else {
Toast.makeText(MainActivity.this,"没网", Toast.LENGTH_SHORT).show();
quary();
if (ada ==null) {
ada = new DbBaseadapter(MainActivity.this, dblist);
xlv.setAdapter(ada);
}else{
ada.sss(dblist,flag);
}
}
}
public void quary(){
Cursor query = db.query("user", null, null, null, null, null, null);
while(query.moveToNext()){
Dao d=new Dao();
d.title=query.getString(query.getColumnIndex("title"));
d.pic=query.getString(query.getColumnIndex("pic"));
d.type=query.getInt(query.getColumnIndex("type"));
dblist.add(d);
}
}
//1 发起网络请求获取数据并显示(网络请求5分,数据显示5分,共10分)
public void data(){
jiexi("http://qhb.2dyt.com/Bwei/news?postkey=9rff1d1AK&type=5");
}
public void jiexi(String path) {
new AsyncTask<String, Void, String>() {
@Override
protected void onPostExecute(String s) {
if (s != null) {
Gson gson = new Gson();
Bean bean = gson.fromJson(s, Bean.class);
list = bean.getList();
if (adapter==null) {
adapter = new MyBaseadapter(MainActivity.this, list);
xlv.setAdapter(adapter);
}else{
adapter.sss(list,flag);
}
insert();
}
}
@Override
protected String doInBackground(String... params) {
try {
URL url = new URL(params[0]);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setReadTimeout(3000);
connection.setConnectTimeout(3000);
int code = connection.getResponseCode();
if (code == 200) {
InputStream is = connection.getInputStream();
return info.readFromNetWork(is);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}.execute(path);
}
public void insert(){
for (int i=0;i<list.size();i++){
ContentValues values=new ContentValues();
values.put("title",list.get(i).getTitle());
values.put("pic",list.get(i).getPic());
values.put("type",list.get(i).getType());
db.insert("user",null,values);
}
}
@Override
public void onRefresh() {
flag=false;
++page;
data();
xlv.stopRefresh(true);
}
@Override
public void onLoadMore() {
flag=true;
++page;
data();
xlv.stopLoadMore();
}
//轮播适配器
class My extends PagerAdapter {
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view==object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView=new ImageView(MainActivity.this);
ImageLoader.getInstance().displayImage(imagelist.get(position%imagelist.size()),imageView,appliction.getImageOptions());
container.addView(imageView);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}
//pager和radiogroup结合使点和图片一起联动
public void jian(){
pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
switch (position%imagelist.size()){
case 0:
rg.check(R.id.rb1);
break;
case 1:
rg.check(R.id.rb2);
break;
case 2:
rg.check(R.id.rb3);
break;
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
switch (checkedId%imagelist.size()){
case R.id.rb1:
pager.setCurrentItem(0);
break;
case R.id.rb2:
pager.setCurrentItem(1);
break;
case R.id.rb3:
pager.setCurrentItem(2);
break;
}
}
});
}
//夜间模式
public void yejian(){
int mode = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
if(mode == Configuration.UI_MODE_NIGHT_YES) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
} else if(mode == Configuration.UI_MODE_NIGHT_NO) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
} else {
// blah blah
}
recreate();
}
public class MyBaseadapter extends BaseAdapter {
private List<Bean.ListBean> list;
private Context context;
public MyBaseadapter(Context context, List<Bean.ListBean> list) {
this.context=context;
this.list=list;
}
public void sss(List<Bean.ListBean> data,boolean flag){
for (Bean.ListBean s: data) {
if (flag){
list.add(s);
}else{
list.add(0,s);
}
}
notifyDataSetChanged();
}
@Override
public int getCount() {
return list!=null?list.size():0;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHoder hoder=null;
if (convertView==null){
hoder=new ViewHoder();
// 实现listview 多条目加载,总共两种类型(各5分,共10分)
if(list.get(position).getType()==4){//四中图片的类型
convertView=View.inflate(context,R.layout.item,null);
hoder.image1= (ImageView) convertView.findViewById(R.id.image1);
hoder.image2= (ImageView) convertView.findViewById(R.id.image2);
hoder.image3= (ImageView) convertView.findViewById(R.id.image3);
hoder.image4= (ImageView) convertView.findViewById(R.id.image4);
hoder.title1= (TextView) convertView.findViewById(R.id.title1);
}else if(list.get(position).getType()==2){//两种图片的类型
convertView=View.inflate(context,R.layout.item1,null);
hoder.image5= (ImageView) convertView.findViewById(R.id.image5);
hoder.image6= (ImageView) convertView.findViewById(R.id.image6);
hoder.title2= (TextView) convertView.findViewById(R.id.title2);
}
convertView.setTag(hoder);
}else{
hoder= (ViewHoder) convertView.getTag();
}
if(list.get(position).getType()==4){
String pic = list.get(position).getPic();
String [] temp = null;
temp = pic.split("\|");
ImageLoader.getInstance().displayImage(temp[0],hoder.image1,appliction.getImageOptions());
ImageLoader.getInstance().displayImage(temp[1],hoder.image2,appliction.getImageOptions());
ImageLoader.getInstance().displayImage(temp[2],hoder.image3,appliction.getImageOptions());
ImageLoader.getInstance().displayImage(temp[3],hoder.image4,appliction.getImageOptions());
hoder.title1.setText(list.get(position).getTitle());
}else if(list.get(position).getType()==2) {
String pic = list.get(position).getPic();
String[] split=null;
split = pic.split("\|");//接口pic是|这个隔开的所以要分割放入一个空的集合
String str1=split[0];
String str2=split[1];
ImageLoader.getInstance().displayImage(str1,hoder.image5,appliction.getImageOptions());
ImageLoader.getInstance().displayImage(str2,hoder.image6,appliction.getImageOptions());
hoder.title2.setText(list.get(position).getTitle());
}
return convertView;
}
class ViewHoder{
private TextView title1,title2;
private ImageView image1,image2,image3,image4,image5,image6;
}
}
public class DbBaseadapter extends BaseAdapter{
private List<Dao> dblist;
private Context context;
public DbBaseadapter(Context context, List<Dao> dblist) {
this.context=context;
this.dblist=dblist;
}
public void sss(List<Dao> data,boolean flag){
for ( Dao s: data) {
if (flag){
dblist.add(s);
}else{
dblist.add(0,s);
}
}
notifyDataSetChanged();
}
@Override
public int getCount() {
return dblist!=null?dblist.size():0;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHoder hoder=null;
if (convertView==null){
hoder=new ViewHoder();
// 实现listview 多条目加载,总共两种类型(各5分,共10分)
if(dblist.get(position).getType()==4){//四中图片的类型
convertView=View.inflate(context,R.layout.item,null);
hoder.image1= (ImageView) convertView.findViewById(R.id.image1);
hoder.image2= (ImageView) convertView.findViewById(R.id.image2);
hoder.image3= (ImageView) convertView.findViewById(R.id.image3);
hoder.image4= (ImageView) convertView.findViewById(R.id.image4);
hoder.title1= (TextView) convertView.findViewById(R.id.title1);
}else if(dblist.get(position).getType()==2){//两种图片的类型
convertView=View.inflate(context,R.layout.item1,null);
hoder.image5= (ImageView) convertView.findViewById(R.id.image5);
hoder.image6= (ImageView) convertView.findViewById(R.id.image6);
hoder.title2= (TextView) convertView.findViewById(R.id.title2);
}
convertView.setTag(hoder);
}else{
hoder= (ViewHoder) convertView.getTag();
}
if(dblist.get(position).getType()==4){
String pic = dblist.get(position).getPic();
String [] temp = null;
temp = pic.split("\|");
ImageLoader.getInstance().displayImage(temp[0],hoder.image1,appliction.getImageOptions());
ImageLoader.getInstance().displayImage(temp[1],hoder.image2,appliction.getImageOptions());
ImageLoader.getInstance().displayImage(temp[2],hoder.image3,appliction.getImageOptions());
ImageLoader.getInstance().displayImage(temp[3],hoder.image4,appliction.getImageOptions());
hoder.title1.setText(dblist.get(position).getTitle());
}else if(dblist.get(position).getType()==2) {
String pic = dblist.get(position).getPic();
String[] split=null;
split = pic.split("\|");//接口pic是|这个隔开的所以要分割放入一个空的集合
String str1=split[0];
String str2=split[1];
ImageLoader.getInstance().displayImage(str1,hoder.image5,appliction.getImageOptions());
ImageLoader.getInstance().displayImage(str2,hoder.image6,appliction.getImageOptions());
hoder.title2.setText(dblist.get(position).getTitle());
}
return convertView;
}
class ViewHoder{
private TextView title1,title2;
private ImageView image1,image2,image3,image4,image5,image6;
}
}
public class Dao {
public String title,pic;
public int type;
public void setTitle(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
public void setPic(String pic) {
this.pic = pic;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public String getPic() {
return pic;
}
}
public class Helper extends SQLiteOpenHelper {
public Helper(Context context) {
super(context, "user.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table user(_id integer primary key autoincrement,title text,pic text,type integer)");
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
<RelativeLayout 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:orientation="vertical"
tools:context="com.example.wangshuaibiao201709221507b.MainActivity">
<LinearLayout
android:id="@+id/linear"
android:background="#f00"
android:layout_width="match_parent"
android:layout_height="30dp"
android:orientation="horizontal">
<TextView
android:layout_margin="5dp"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="北京"/>
<EditText
android:layout_gravity="center"
android:background="@color/viewBackground"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:hint="国风悦府"
android:gravity="center"/>
<TextView
android:id="@+id/night"
android:layout_margin="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="地图"/>
</LinearLayout>
<android.support.v4.view.ViewPager
android:layout_below="@+id/linear"
android:layout_width="match_parent"
android:layout_height="100dp"
android:id="@+id/pager"></android.support.v4.view.ViewPager>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/ll"
android:layout_marginTop="80dp"
android:orientation="vertical">
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/rg"
android:orientation="horizontal"
android:gravity="center"
>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/rb1"
android:checked="true"/>
<RadioButton
android:id="@+id/rb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/rb3"/>
</RadioGroup>
</LinearLayout>
<com.limxing.xlistview.view.XListView
android:background="@color/viewBackground"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/xlv"
android:layout_below="@+id/pager"></com.limxing.xlistview.view.XListView>
</RelativeLayout>
<TextView
android:id="@+id/title1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_margin="8dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_margin="5dp"
android:layout_weight="1"
android:src="@mipmap/ic_launcher"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/image1"/>
<ImageView
android:layout_margin="5dp"
android:layout_weight="1"
android:src="@mipmap/ic_launcher"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/image2"/>
<ImageView
android:layout_margin="5dp"
android:layout_weight="1"
android:src="@mipmap/ic_launcher"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/image3"/>
<ImageView
android:layout_margin="5dp"
android:layout_weight="1"
android:src="@mipmap/ic_launcher"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/image4"/>
</LinearLayout>
<TextView
android:id="@+id/title2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_margin="8dp"/>
<LinearLayout
android:layout_margin="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_weight="1"
android:src="@mipmap/ic_launcher"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/image5"/>
<ImageView
android:layout_weight="1"
android:src="@mipmap/ic_launcher"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/image6"/>
</LinearLayout>