倒计时跳转
package com.bawei.jiejiaqi0119;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView tv1;
private Button bt1;
private int time;
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 0){
if (time <= 0){
as();
return;
}
time--;
tv1.setText(time + "s");
handler.sendEmptyMessageDelayed(0,1000);
}
}
};
private SharedPreferences sharedPreferences;
private SharedPreferences.Editor edit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1 = (TextView) findViewById(R.id.tv1);
bt1 = (Button) findViewById(R.id.bt1);
time = 5;
tv1.setText(time + "s");
sharedPreferences = getSharedPreferences("User", MODE_PRIVATE);
edit = sharedPreferences.edit();
boolean islogin = sharedPreferences.getBoolean("islogin", false);
if (islogin){
as();
}else{
handler.sendEmptyMessageDelayed(0,1000);
}
edit.putBoolean("islogin",true);
edit.commit();
bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
as();
}
});
}
private void as() {
handler.removeCallbacksAndMessages(null);
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
startActivity(intent);
finish();
}
}
MainActivity的抽基类
package com.bawei.jiejiaqi0119.base;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
public abstract class BaseMain extends AppCompatActivity{
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutID());
initView();
initData();
}
public abstract void initView();
public abstract void initData();
public abstract int getLayoutID();
}
FrameLayout+Fragment联动
package com.bawei.jiejiaqi0119;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.Toast;
import com.bawei.jiejiaqi0119.base.BaseMain;
import com.bawei.jiejiaqi0119.fragment.Fragment01;
import com.bawei.jiejiaqi0119.fragment.Fragment02;
import com.bawei.jiejiaqi0119.fragment.Fragment03;
import java.util.TreeMap;
public class Main2Activity extends BaseMain{
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
FragmentTransaction transaction = supportFragmentManager.beginTransaction();
switch (item.getItemId()) {
case R.id.navigation_home:
transaction.show(fragment01).hide(fragment02).hide(fragment03);
break;
case R.id.navigation_dashboard:
transaction.show(fragment02).hide(fragment01).hide(fragment03);
break;
case R.id.navigation_notifications:
transaction.show(fragment03).hide(fragment02).hide(fragment01);
break;
}
transaction.commit();
return true;
}
};
private BottomNavigationView navigation;
private FrameLayout fl1;
private Fragment01 fragment01;
private Fragment02 fragment02;
private Fragment03 fragment03;
private FragmentManager supportFragmentManager;
private DrawerLayout container;
@Override
public void initView() {
fl1 = (FrameLayout) findViewById(R.id.fl1);
container = (DrawerLayout) findViewById(R.id.container);
navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}
@Override
public void initData() {
fragment01 = new Fragment01();
fragment02 = new Fragment02();
fragment03 = new Fragment03();
supportFragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
fragmentTransaction.add(R.id.fl1,fragment01);
fragmentTransaction.add(R.id.fl1,fragment02);
fragmentTransaction.add(R.id.fl1,fragment03);
fragmentTransaction.show(fragment01).hide(fragment02).hide(fragment03);
fragmentTransaction.commit();
container.addDrawerListener(new DrawerLayout.DrawerListener() {
@Override
public void onDrawerSlide(@NonNull View view, float v) {
}
@Override
public void onDrawerOpened(@NonNull View view) {
Toast.makeText(Main2Activity.this,"我开启了",Toast.LENGTH_LONG).show();
}
@Override
public void onDrawerClosed(@NonNull View view) {
Toast.makeText(Main2Activity.this,"我关闭了",Toast.LENGTH_LONG).show();
}
@Override
public void onDrawerStateChanged(int i) {
}
});
}
@Override
public int getLayoutID() {
return R.layout.activity_main2;
}
}
Fragment的抽基类
package com.bawei.jiejiaqi0119.base;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public abstract class BaseFragment extends Fragment{
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(getLayoutID(), container, false);
return view;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
initView(view);
initData();
}
public abstract void initView(View view);
public abstract void initData();
public abstract int getLayoutID();
}
TabLayout+ViewPager联动
package com.bawei.jiejiaqi0119.fragment;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import com.bawei.jiejiaqi0119.R;
import com.bawei.jiejiaqi0119.base.BaseFragment;
import com.bawei.jiejiaqi0119.frag.Fragmenta;
import com.bawei.jiejiaqi0119.frag.Fragmentb;
import com.bawei.jiejiaqi0119.frag.Fragmentc;
import com.bawei.jiejiaqi0119.frag.Fragmentd;
import java.util.ArrayList;
import java.util.List;
public class Fragment01 extends BaseFragment{
private TabLayout tab;
private ViewPager pager;
private List<Fragment> list;
private List<String> slist;
private Msss msss;
@Override
public void initView(View view) {
tab = (TabLayout) view.findViewById(R.id.tab);
pager = (ViewPager) view.findViewById(R.id.pager);
}
@Override
public void initData() {
list = new ArrayList<>();
list.add(new Fragmenta());
list.add(new Fragmentb());
list.add(new Fragmentc());
list.add(new Fragmentd());
slist = new ArrayList<>();
slist.add(" 首页 ");
slist.add(" 视频 ");
slist.add(" 商品 ");
slist.add(" 音乐 ");
msss = new Msss(getChildFragmentManager());
pager.setAdapter(msss);
tab.setupWithViewPager(pager);
}
@Override
public int getLayoutID() {
return R.layout.fragment01;
}
class Msss extends FragmentPagerAdapter{
public Msss(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
return list.get(i);
}
@Override
public int getCount() {
return list.size();
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return slist.get(position);
}
}
}
数据显示+数据库+Pull上下拉刷新
package com.bawei.jiejiaqi0119.frag;
import android.content.ContentValues;
import android.database.Cursor;
import android.os.AsyncTask;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;
import com.bawei.jiejiaqi0119.R;
import com.bawei.jiejiaqi0119.Work;
import com.bawei.jiejiaqi0119.adapter.MyAdapter;
import com.bawei.jiejiaqi0119.base.BaseFragment;
import com.bawei.jiejiaqi0119.bean.MyBean;
import com.bawei.jiejiaqi0119.dao.Dao;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
public class Fragmenta extends BaseFragment{
private String urlString = "http://172.17.8.100/movieApi/movie/v1/findReleaseMovieList?";
int page = 1;
int count = 10;
private ArrayList<MyBean.ResultBean> list;
private PullToRefreshListView pull;
private MyAdapter myAdapter;
private Dao dao;
@Override
public void initView(View view) {
pull = (PullToRefreshListView) view.findViewById(R.id.pull);
list = new ArrayList<MyBean.ResultBean>();
myAdapter = new MyAdapter(getActivity(), list);
pull.setAdapter(myAdapter);
dao = new Dao(getActivity());
pull.setMode(PullToRefreshBase.Mode.BOTH);
pull.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ListView> pullToRefreshBase) {
page = 1;
list.clear();
initData();
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ListView> pullToRefreshBase) {
page++;
initData();
}
});
initData();
}
@Override
public void initData() {
if (Work.Assaas(getActivity())){
new Msd().execute(urlString+"page=1"+"&count=10");
}else{
Toast.makeText(getActivity(),"没网了",Toast.LENGTH_LONG).show();
Cursor query = dao.query("asas", null, null, null, null, null, null);
if (query.moveToFirst()){
do{
String name = query.getString(query.getColumnIndexOrThrow("name"));
String summary = query.getString(query.getColumnIndexOrThrow("summary"));
list.add(new MyBean.ResultBean(name,summary));
}while (query.moveToNext());
myAdapter.notifyDataSetChanged();
}
}
}
class Msd extends AsyncTask<String,Void,String>{
@Override
protected String doInBackground(String... strings) {
String s = Work.Asa(strings[0]);
return s;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Gson gson = new Gson();
MyBean myBean = gson.fromJson(s, MyBean.class);
List<MyBean.ResultBean> result = myBean.getResult();
for (int i = 0; i < result.size(); i++){
ContentValues values = new ContentValues();
values.put("name",result.get(i).getName());
values.put("summary",result.get(i).getSummary());
dao.insert("asas",null,values);
}
list.addAll(result);
myAdapter.notifyDataSetChanged();
pull.onRefreshComplete();
}
}
@Override
public int getLayoutID() {
return R.layout.fragmenta;
}
}
多条目加载的适配器
package com.bawei.jiejiaqi0119.adapter;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.bawei.jiejiaqi0119.R;
import com.bawei.jiejiaqi0119.bean.MyBean;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.ArrayList;
public class MyAdapter extends BaseAdapter{
public Context context;
public ArrayList<MyBean.ResultBean> list;
public MyAdapter(Context context, ArrayList<MyBean.ResultBean> list) {
this.context = context;
this.list = list;
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getItemViewType(int position) {
return position % 2;
}
@Override
public int getCount() {
return list.size();
}
@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) {
int itemViewType = getItemViewType(position);
switch (itemViewType){
case 0:
ViewHolder1 vh = new ViewHolder1();
if (convertView == null){
convertView = View.inflate(context, R.layout.name, null);
vh.iv1 =(ImageView) convertView.findViewById(R.id.iv1);
vh.tv1 =(TextView) convertView.findViewById(R.id.tv1);
vh.tv2 =(TextView) convertView.findViewById(R.id.tv2);
vh.tv3 =(TextView) convertView.findViewById(R.id.tv3);
convertView.setTag(vh);
}else{
vh = (ViewHolder1) convertView.getTag();
}
vh.tv1.setText(list.get(position).getName());
vh.tv2.setText(list.get(position).getFollowMovie()+"");
vh.tv3.setText(list.get(position).getSummary());
ImageLoader.getInstance().displayImage(list.get(position).getImageUrl(),vh.iv1);
break;
case 1:
ViewHolder2 vh1 = new ViewHolder2();
if (convertView == null){
convertView = View.inflate(context, R.layout.name, null);
vh1.tv1 =(TextView) convertView.findViewById(R.id.tv1);
vh1.tv2 =(TextView) convertView.findViewById(R.id.tv2);
vh1.tv3 =(TextView) convertView.findViewById(R.id.tv3);
convertView.setTag(vh1);
}else{
vh1 = (ViewHolder2) convertView.getTag();
}
vh1.tv1.setText(list.get(position).getName());
vh1.tv2.setText(list.get(position).getFollowMovie()+"");
vh1.tv3.setText(list.get(position).getSummary());
break;
}
return convertView;
}
class ViewHolder1{
ImageView iv1;
TextView tv1,tv2,tv3;
}
class ViewHolder2{
TextView tv1,tv2,tv3;
}
}
Bean类
package com.bawei.jiejiaqi0119.bean;
import java.util.List;
public class MyBean {
private String message;
private String status;
private List<ResultBean> result;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public List<ResultBean> getResult() {
return result;
}
public void setResult(List<ResultBean> result) {
this.result = result;
}
public static class ResultBean {
private int followMovie;
private int id;
private String imageUrl;
private String name;
private int rank;
private long releaseTime;
private String releaseTimeShow;
private String summary;
public int getFollowMovie() {
return followMovie;
}
public void setFollowMovie(int followMovie) {
this.followMovie = followMovie;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getImageUrl() {
return imageUrl;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getRank() {
return rank;
}
public void setRank(int rank) {
this.rank = rank;
}
public long getReleaseTime() {
return releaseTime;
}
public void setReleaseTime(long releaseTime) {
this.releaseTime = releaseTime;
}
public String getReleaseTimeShow() {
return releaseTimeShow;
}
public void setReleaseTimeShow(String releaseTimeShow) {
this.releaseTimeShow = releaseTimeShow;
}
public String getSummary() {
return summary;
}
public void setSummary(String summary) {
this.summary = summary;
}
public ResultBean(String name, String summary) {
this.name = name;
this.summary = summary;
}
}
}
数据库
package com.bawei.jiejiaqi0119.sql;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class MySql extends SQLiteOpenHelper{
public MySql(Context context) {
super(context, "Asas.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table asas(id INTEGER PRIMARY KEY AUTOINCREMENT,"+"name text,summary text)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
Dao类
package com.bawei.jiejiaqi0119.dao;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.bawei.jiejiaqi0119.sql.MySql;
public class Dao {
public Context context;
public SQLiteDatabase db;
public Dao(Context context) {
this.context = context;
MySql mySql = new MySql(context);
db = mySql.getWritableDatabase();
}
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);
}
}
网络请求+网络判断
package com.bawei.jiejiaqi0119;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class Work {
public static String Asa(String urlString){
try {
URL url = new URL(urlString);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
int code = urlConnection.getResponseCode();
if (code == 200){
InputStream inputStream = urlConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder stringBuilder = new StringBuilder();
String t;
while ((t = bufferedReader.readLine()) != null){
stringBuilder.append(t);
}
return stringBuilder.toString();
}
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
public static boolean Assaas(Context context){
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
if (activeNetworkInfo != null && activeNetworkInfo.isConnected()){
return true;
}else{
return false;
}
}
}
图片类
package com.bawei.jiejiaqi0119.image;
import android.app.Application;
import com.bawei.jiejiaqi0119.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
public class Image extends Application{
@Override
public void onCreate() {
super.onCreate();
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.ic_launcher_background)
.displayer(new RoundedBitmapDisplayer(360))
.build();
ImageLoaderConfiguration build = new ImageLoaderConfiguration.Builder(this)
.defaultDisplayImageOptions(options)
.build();
ImageLoader.getInstance().init(build);
}
}
XListView
package com.bawei.jiejiaqi0119.frag;
import android.os.AsyncTask;
import android.view.View;
import com.bawei.jiejiaqi0119.R;
import com.bawei.jiejiaqi0119.Work;
import com.bawei.jiejiaqi0119.adapter.MyAdapter;
import com.bawei.jiejiaqi0119.base.BaseFragment;
import com.bawei.jiejiaqi0119.bean.MyBean;
import com.bwie.xlistviewlibrary.view.XListView;
import com.google.gson.Gson;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.SimpleTimeZone;
import static android.view.View.X;
public class Fragmentb extends BaseFragment{
private XListView xlv;
private String urlString = "http://172.17.8.100/movieApi/movie/v1/findReleaseMovieList?";
int page = 1;
int count = 10;
private ArrayList<MyBean.ResultBean> list;
private MyAdapter myAdapter;
@Override
public void initView(View view) {
xlv = (XListView) view.findViewById(R.id.xlv);
xlv.setPullLoadEnable(true);
list = new ArrayList<MyBean.ResultBean>();
myAdapter = new MyAdapter(getActivity(), list);
xlv.setAdapter(myAdapter);
xlv.setXListViewListener(new XListView.IXListViewListener() {
@Override
public void onRefresh() {
page =1;
list.clear();
initData();
}
@Override
public void onLoadMore() {
page++;
initData();
}
});
initData();
}
@Override
public void initData() {
new Msds().execute(urlString+"page=1"+"&count=10");
}
@Override
public int getLayoutID() {
return R.layout.fragmentb;
}
class Msds extends AsyncTask<String,Void,String> {
@Override
protected String doInBackground(String... strings) {
String s = Work.Asa(strings[0]);
return s;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Gson gson = new Gson();
MyBean myBean = gson.fromJson(s, MyBean.class);
List<MyBean.ResultBean> result = myBean.getResult();
list.addAll(result);
myAdapter.notifyDataSetChanged();
urCmoplete();
}
private void urCmoplete() {
Date date = new Date(System.currentTimeMillis());
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String format = simpleDateFormat.format(date);
xlv.setRefreshTime(format);
xlv.stopLoadMore();
xlv.stopRefresh();
}
}
}
无限轮播
package com.bawei.jiejiaqi0119.fragment;
import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import com.bawei.jiejiaqi0119.R;
import com.bawei.jiejiaqi0119.base.BaseFragment;
import com.bawei.jiejiaqi0119.bean.MyBean;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.youth.banner.Banner;
import java.util.Arrays;
public class Fragment02 extends BaseFragment{
String[] urls = {"https://img-my.csdn.net/uploads/201407/26/1406383265_8550.jpg",
"http://img2.91.com/uploads/allimg/140331/32-1403311A009.jpg",
"http://img1.imgtn.bdimg.com/it/u=3055632223,2826867768&fm=214&gp=0.jpg",
"http://img.article.pchome.net/00/28/41/62/pic_lib/wm/hlfjkpbz_17.jpg",
"http://img0.imgtn.bdimg.com/it/u=2631448432,2817427988&fm=214&gp=0.jpg",
"http://img.ichemo.cn/model/5188ebc96c74dd.jpg"
};
private Banner ban;
@Override
public void initView(View view) {
ban = (Banner) view.findViewById(R.id.ban);
}
@Override
public void initData() {
final ImageLoader instance = ImageLoader.getInstance();
ban.setImageLoader(new com.youth.banner.loader.ImageLoader() {
@Override
public void displayImage(Context context, Object path, ImageView imageView) {
instance.displayImage((String) path,imageView);
}
});
ban.setImages(Arrays.asList(urls));
ban.start();
}
@Override
public int getLayoutID() {
return R.layout.fragment02;
}
}
##权限
<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"/>