动画
public class MainActivity extends AppCompatActivity { private ImageView img; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Intent intent=new Intent(MainActivity.this,Main2Activity.class); // startActivity(intent); img = (ImageView) findViewById(R.id.img); Animation animation=new AlphaAnimation(0.1f,1.0f); animation.setDuration(5000); img.startAnimation(animation); animation.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { Intent intent=new Intent(MainActivity.this,Main2Activity.class); startActivity(intent); } @Override public void onAnimationRepeat(Animation animation) { } }); } }
主MainActivity
public class Main2Activity extends BaseAcvitity implements View.OnClickListener { private String[] strList = new String[]{"推荐", "热点", "北京", "科技", "军事", "视频", "社会"}; private DrawerLayout drawerLayout; private TabLayout tabLayout; private ViewPager vp; private ListView lv; private ActionBarDrawerToggle toggle; private List<String> list = new ArrayList<String>(); private int mode; private List<ChannelBean> channelist; private String jsonStr; private List<Fragment>frlist=new ArrayList<Fragment>(); private ImageView imgLogin; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); //极光推送 JPushInterface.setDebugMode(true); JPushInterface.init(this); //频道管理的点击事件 findViewById(R.id.tvpindao).setOnClickListener(this); //夜间模式 mode = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK; // if(isOnline()){ initView(); initData(); }else{ showDialog(); } } //左侧滑显示listview数量 private void initData() { Main2Adapter main2Adapter=new Main2Adapter(this,list); lv.setAdapter(main2Adapter); //左侧滑Listview lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { } }); list.add("好友动态"); list.add("与我相关"); list.add("我的头条"); list.add("我的话题"); list.add("收藏"); list.add("活动"); //显示Fragment frlist.add(new Fragment1()); frlist.add(new Fragment2()); frlist.add(new Fragment3()); frlist.add(new Fragment4()); frlist.add(new Fragment5()); frlist.add(new Fragment6()); frlist.add(new Fragment7()); } //查询控件 private void initView() { lv = (ListView) findViewById(R.id.lv); drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout); tabLayout = (TabLayout) findViewById(R.id.tabLayout); vp = (ViewPager) findViewById(R.id.vp); //底布局 findViewById(R.id.img4).setOnClickListener(this); findViewById(R.id.img5).setOnClickListener(this); newsView(); //头布局变化 toggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.app_name, R.string.app_name); //同步侧滑 toggle.syncState(); drawerLayout.addDrawerListener(toggle); for (String str : strList) { tabLayout.addTab(tabLayout.newTab().setText(str)); } vp.setAdapter(new MyPageAdapter(getSupportFragmentManager())); tabLayout.setupWithViewPager(vp); } @Override public boolean onOptionsItemSelected(MenuItem item) { return toggle.onOptionsItemSelected(item) | super.onOptionsItemSelected(item); } //FragmentPager适配器 class MyPageAdapter extends FragmentPagerAdapter { public MyPageAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { return frlist.get(position); } @Override public int getCount() { return strList.length; } @Override public CharSequence getPageTitle(int position) { return strList[position]; } } public void newsView() { //头布局 View view = View.inflate(this, R.layout.leftsideslip, null); lv.addHeaderView(view); //登陆 imgLogin = (ImageView) view.findViewById(img1); imgLogin.setOnClickListener(this); //短信 ImageView imgnote = (ImageView) view.findViewById(R.id.img2); imgnote.setOnClickListener(this); //夜间模式 ImageView imgnight = (ImageView) view.findViewById(R.id.img3); imgnight.setOnClickListener(this); //更多 TextView tvgengduo= (TextView) view.findViewById(R.id.tv); tvgengduo.setOnClickListener(this); } //点击事件 @Override public void onClick(View v) { switch (v.getId()) { //频道 case R.id.tvpindao:{ Intent intent=new Intent(Main2Activity.this,Main5Activity.class); startActivity(intent); } break; //更多登陆方式 case R.id.tv:{ Intent intent=new Intent(Main2Activity.this,Main3Activity.class); intent.putExtra("qq","https://sso.toutiao.com/login/"); startActivity(intent); } break; //第三方登录 case R.id.img1: { UMShareAPI.get(Main2Activity.this).getPlatformInfo(Main2Activity.this, SHARE_MEDIA.QQ, umAuthListener); PlatformConfig.setQQZone("1106197903", "Vfdwhxp8yhlKPv4h"); } break; //短信验证 case R.id.img2: { RegisterPage registerPage = new RegisterPage(); //回调函数 registerPage.setRegisterCallback(new EventHandler() { public void afterEvent(int event, int result, Object data) { // 解析结果 if (result == SMSSDK.RESULT_COMPLETE) { //提交验证码成功 if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) { } //提交验证码成功,此时已经验证成功了 else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) { } } } }); registerPage.show(Main2Activity.this); } break; //夜间模式 case R.id.img3: { 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); } recreate(); } break; //设置 case R.id.img4:{ Intent intent=new Intent(Main2Activity.this, Main4Activity.class); startActivity(intent); } break; //城市列表 case R.id.img5:{ CityListActivity.startCityActivityForResult(this); } break; } } //第三方需要重写的方法 private UMAuthListener umAuthListener = new UMAuthListener() { @Override public void onStart(SHARE_MEDIA platform) { //授权开始的回调 } @Override public void onComplete(SHARE_MEDIA platform, int action, Map<String, String> data) { Toast.makeText(getApplicationContext(), "Authorize succeed", Toast.LENGTH_SHORT).show(); // String name = data.get("name"); // String gender = data.get("gender"); String photoUrl = data.get("iconurl"); //tv.setText(name + " " +gender); ImageLoader.getInstance().displayImage(photoUrl,imgLogin); } @Override public void onError(SHARE_MEDIA platform, int action, Throwable t) { Toast.makeText(getApplicationContext(), "Authorize fail", Toast.LENGTH_SHORT).show(); } @Override public void onCancel(SHARE_MEDIA platform, int action) { Toast.makeText(getApplicationContext(), "Authorize cancel", Toast.LENGTH_SHORT).show(); } }; @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data); } //语音识别 public void discern(View view){ //1.创建RecognizerDialog对象,第二个参数就是一个初始化的监听器,我们用不上就设置为null RecognizerDialog mDialog = new RecognizerDialog(this, null); //2.设置accent、language等参数 mDialog.setParameter(SpeechConstant.LANGUAGE, "zh_cn");//设置为中文模式 mDialog.setParameter(SpeechConstant.ACCENT, "mandarin");//设置普通话模式 //若要将UI控件用于语义理解,必须添加以下参数设置,设置之后onResult回调返回将是语义理解 //mDialog.setParameter("asr_sch", "1"); //mDialog.setParameter("nlp_version", "2.0"); //创建一个装每次解析数据的容器 final StringBuilder mStringBuilder = new StringBuilder(); //3.设置回调接口 mDialog.setListener(new RecognizerDialogListener() { @Override//识别成功执行,参数recognizerResult 识别的结果,Json格式的字符串 //第二参数 b:等于true时会话结束.方法才不会继续回调 //一般情况下通过onResult接口多次返回结果,完整识别内容是多次结果累加的 public void onResult(RecognizerResult recognizerResult, boolean b) { //拿到讯飞识别的结果 String resultString = recognizerResult.getResultString(); System.out.println("讯飞识别的结果 " + resultString); System.out.println("b参数是什么 " + b); //自定义解析bean数据的方法,得到解析数据 String content = parseData(resultString); System.out.println("解析后的数据 "+ content); mStringBuilder.append(content); //对参数2b进行判断,如果为true,代表这个方法不会对调,那么我们容器的数据转为字符串,拿来使用即可 if (b) { String result = mStringBuilder.toString(); System.out.println(result); //回答对象,在没有匹配到用户说的话,默认输出语句 String anwser = "不好意思,你说的我没有听清楚!!!"; if (result.contains("你好")) { anwser = "你好,我是你的智能语音助手,很高兴为你服务"; } else if (result.contains("小明")) { anwser = "和小明一块玩"; } else if (result.contains("王少良")) { //定义一个String数组,智能语音根据美女这个数据,可能说的话 String [] answerList=new String[]{"500元,妹子陪你打一晚上游戏","你是坏人不和你玩了","小助手很纯洁,不要带坏我了"}; //小助手随机回答,所以使用生成随机数的类.random(因为此数是小数,乘以集合长度,再做int类型强转,得到的数是0到集合长度-1) int random = (int) (Math.random()*answerList.length); anwser=answerList[random]; }else if (result.contains("打开权限信息")) { Intent intent = new Intent(); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); if (Build.VERSION.SDK_INT >= 9) { intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS"); intent.setData(Uri.fromParts("package", getPackageName(), null)); } else if (Build.VERSION.SDK_INT <= 8) { intent.setAction(Intent.ACTION_VIEW); intent.setClassName("com.android.settings", "com.android.settings.InstalledAppDetails"); intent.putExtra("com.android.settings.ApplicationPkgName", getPackageName()); } startActivity(intent); } show(anwser); } } @Override//识别失败执行的方法,speechError错误码 public void onError(SpeechError speechError) { System.out.println("错误码 " + speechError); } }); //4.显示dialog,接收语音输入 mDialog.show(); } public void compound(View view){ show("说话的是个大涮锅"); } //语音合成 public void show(String result) { //1.创建 SpeechSynthesizer 对象, 第二个参数:本地合成时传 InitListener SpeechSynthesizer mTts = SpeechSynthesizer.createSynthesizer(this, null); //2.合成参数设置,详见《MSC Reference Manual》SpeechSynthesizer 类 // 设置发音人(更多在线发音人,用户可参见 附录13.2 mTts.setParameter(SpeechConstant.VOICE_NAME, "xiaoyan"); //设置发音人 mTts.setParameter(SpeechConstant.SPEED, "50");//设置语速 mTts.setParameter(SpeechConstant.VOLUME, "80");//设置音量,范围 0~100 mTts.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_CLOUD); //设置云端,这些功能用到了讯飞服务器,所以要有网络 //设置合成音频保存位置(可自定义保存位置),保存在“./sdcard/iflytek.pcm” //保存在 SD 卡需要在 AndroidManifest.xml 添加写 SD 卡权限 //仅支持保存为 pcm 和 wav 格式,如果不需要保存合成音频,注释该行代码 // mTts.setParameter(SpeechConstant.TTS_AUDIO_PATH, "./sdcard/iflytek.pcm"); // 3.开始合成,第一个参数就是转换成声音的文字,自定义,第二个参数就是合成监听器对象,我们不需要对声音有什么特殊处理,就传null mTts.startSpeaking(result, null); } private String parseData(String resultString) { //创建gson对象.记得要关联一下gson.jar包,方可以使用 Gson gson = new Gson(); //参数1 String类型的json数据 参数2.存放json数据对应的bean类 XFBean xfBean = gson.fromJson(resultString, XFBean.class); //创建集合,用来存放bean类里的对象 ArrayList<XFBean.WS> ws = xfBean.ws; //创建一个容器,用来存放从每个集合里拿到的数据,使用StringBUndle效率高 StringBuilder stringBuilder = new StringBuilder(); for (XFBean.WS w : ws) { String text = w.cw.get(0).w; stringBuilder.append(text); } //把容器内的数据转换为字符串返回出去 return stringBuilder.toString(); } /** * 提示用户的一个联网对话框 */ private void showDialog(){ AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage("是否开网"); builder.setNegativeButton("否",null); builder.setPositiveButton("是", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //设置跳转网络界面 startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS)); } }); builder.create().show(); } /** * 判断网络是否连接 * @return true 网络已经连接 false 网络没有连接 */ public boolean isOnline() { //得到网络连接的管理者 ConnectivityManager connMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); //通过网络管理者得到网络信息 NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); //判断网络是否连接 return (networkInfo != null && networkInfo.isConnected()); } }
Fragment
public class Fragment1 extends Fragment implements XListView.IXListViewListener{ private Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); vp.setCurrentItem(intdex); } }; private int intdex=1; private boolean flog; private XListView xlv; private View view; private MyAdapter adapter; private ViewPager vp; private List<String>imgList; private List<InifBean.ResultBean.DataBean> apklist; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { view = inflater.inflate(R.layout.f1,container,false); xlv = (XListView) view.findViewById(R.id.xlv); xlv.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Intent intent=new Intent(getActivity(), Main3Activity.class); intent.putExtra("qq",apklist.get(position-1).getUrl()); startActivity(intent); } }); xlv.setPullLoadEnable(true); xlv.setXListViewListener(this); vp = (ViewPager) view.findViewById(R.id.vp); imgList= Arrays.asList("http://www.microvirt.com/new_market/img/zhuxian_logo.png","http://www.microvirt.com/new_market/img/zhuxian_logo.png","http://www.microvirt.com/new_market/img/zhuxian_logo.png"); myinitData(); vp.setAdapter(new MyPageAdapter()); auto(); return view; } public void auto(){ new Thread(){ @Override public void run() { super.run(); while (true){ try { sleep(1000); } catch (Exception e) { e.printStackTrace(); } intdex++; handler.sendEmptyMessage(0); } } }.start(); } //解析 private void myinitData() { RequestParams parms=new RequestParams("http://v.juhe.cn/toutiao/index?type=junshi&key=54e3d5f4ee64f51bef570ce8505d37b5"); x.http().get(parms, new Callback.CommonCallback<String>() { //请求数据成功 @Override public void onSuccess(String result) { Gson gson=new Gson(); InifBean bean = gson.fromJson(result, InifBean.class); apklist = bean.getResult().getData(); if(adapter==null){ adapter=new MyAdapter(getActivity(), apklist); xlv.setAdapter(adapter); }else{ adapter.loadmore(apklist,flog); } } //请求数据失败要做的 @Override public void onError(Throwable ex, boolean isOnCallback) { } //取消 @Override public void onCancelled(CancelledException cex) { } //最后要做的 @Override public void onFinished() { } }); } @Override public void onRefresh() { flog=false; ++intdex; myinitData(); xlv.stopRefresh(true); } @Override public void onLoadMore() { flog=true; ++intdex; myinitData(); xlv.stopLoadMore(); } class MyPageAdapter 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(getActivity()); container.addView(imageView); ImageLoader.getInstance().displayImage(imgList.get(position%imgList.size()),imageView, App.getDisp()); return imageView; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View) object); } } }
适配器
public class MyAdapter extends BaseAdapter { private Context context; private List<InifBean.ResultBean.DataBean> apklist; public MyAdapter(Context context,List<InifBean.ResultBean.DataBean> apklist) { this.context=context; this.apklist=apklist; } public void loadmore(List<InifBean.ResultBean.DataBean> list,boolean flog){ for (InifBean.ResultBean.DataBean apkBean:list){ if(flog){ list.add(apkBean); }else{ list.add(0,apkBean); } } notifyDataSetChanged(); } @Override public int getCount() { return apklist.size(); } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public int getViewTypeCount() { return 2; } @Override public int getItemViewType(int position) { if(position%2==0){ return 0; } return 1; } @Override public View getView(int position, View convertView, ViewGroup parent) { if(convertView==null){ convertView = View.inflate(context, R.layout.item,null); TextView tv1= (TextView) convertView.findViewById(R.id.tv1); TextView tv2= (TextView) convertView.findViewById(R.id.tv2); TextView tv3= (TextView) convertView.findViewById(R.id.tv3); ImageView img1= (ImageView) convertView.findViewById(R.id.img); ImageView img2= (ImageView) convertView.findViewById(R.id.img1); ImageView img3= (ImageView) convertView.findViewById(R.id.img2); tv3.setText(apklist.get(position).getTitle()); tv1.setText(apklist.get(position).getAuthor_name()); //tv2.setText(apklist.get(position).getAuthor_name()); loadImage(apklist.get(position).getUrl(),img1); loadImage(apklist.get(position).getThumbnail_pic_s(),img2); loadImage(apklist.get(position).getThumbnail_pic_s02(),img3); } return convertView; } public void loadImage(String url, final ImageView imggeView){ new AsyncTask<String,Void,Bitmap>(){ @Override protected void onPostExecute(Bitmap bitmap) { super.onPostExecute(bitmap); if(bitmap==null){ imggeView.setImageResource(R.mipmap.ic_empty); }else{ imggeView.setImageBitmap(bitmap); } } @Override protected Bitmap doInBackground(String... params) { try { URL url=new URL(params[0]); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setConnectTimeout(5000); connection.setReadTimeout(5000); int code = connection.getResponseCode(); if(code==200){ InputStream is = connection.getInputStream(); return BitmapFactory.decodeStream(is); } } catch (Exception e) { e.printStackTrace(); } return null; } }.execute(url); }
来源:http://www.cnblogs.com/yu12/p/7566875.html