一
<?xml version="1.0" encoding="utf-8"?> <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"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:textSize="30sp" android:text="欢迎来到八维"/> </RelativeLayout>
二
package com.example.newss; 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; public class MainActivity extends AppCompatActivity { private SharedPreferences sp; private Intent intent; Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); if (msg.what==123){ sp.edit().putBoolean("is_first",false).commit(); startActivity(intent); finish(); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sp = getSharedPreferences("flag",MODE_PRIVATE); intent = new Intent(MainActivity.this,TwoActivity.class); boolean isfirst=sp.getBoolean("is_first",true); if(isfirst){ handler.sendEmptyMessageDelayed(123,5000); }else{ startActivity(intent); finish(); } } }
三
package com.example.newss; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.IDN; import java.net.MalformedURLException; import java.net.URL; /** * Created by 。。。。 on 2018/9/13. */ public class HttpUtils { public static String getHttpFroment(String urlstring){ String result=""; try { URL url=new URL(urlstring); HttpURLConnection connection= (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.connect(); int code=connection.getResponseCode(); if(code==200){ InputStream is=connection.getInputStream(); result=jeixi(is); } } catch (Exception e) { e.printStackTrace(); } return result; } private static String jeixi(InputStream is) { String result=""; ByteArrayOutputStream baos=new ByteArrayOutputStream(); byte[] b=new byte[1024]; int count=-1; try { while ((count=is.read(b,0,b.length))!=-1){ baos.write(b,0,count); baos.flush(); } result=baos.toString(); baos.close(); is.close(); } catch (IOException e) { e.printStackTrace(); } return result; } }
四
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ListView android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="wrap_content"></ListView> </LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <com.bwie.xlistview.XListView android:id="@+id/xlistview" android:layout_width="match_parent" android:layout_height="wrap_content"></com.bwie.xlistview.XListView> </LinearLayout>
五
package com.example.newss; 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.nostra13.universalimageloader.core.ImageLoader; import java.util.List; /** * Created by 。。。。 on 2018/9/13. */ public class NewsAdapter extends BaseAdapter { private Context context; private List<News.ResultBean.DataBean> list; public NewsAdapter(Context context, List<News.ResultBean.DataBean> list) { this.context = context; this.list = list; } @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) { News.ResultBean.DataBean bean=list.get(position); if(bean.getType().equals("a")){ return 0; }else{ return 1; } } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder1 holder1=null; ViewHolder2 holder2=null; int type=getItemViewType(position); switch (type){ case 0: if(convertView==null){ holder1=new ViewHolder1(); convertView=View.inflate(context,R.layout.item_one,null); holder1.img=convertView.findViewById(R.id.img); holder1.title1=convertView.findViewById(R.id.title); convertView.setTag(holder1); }else{ holder1= (ViewHolder1) convertView.getTag(); } ImageLoader.getInstance().displayImage(list.get(position).getThumbnail_pic_s(),holder1.img); holder1.title1.setText(list.get(position).getTitle()); break; case 1: if(convertView==null){ holder2=new ViewHolder2(); convertView=View.inflate(context,R.layout.item_two,null); holder2.img1=convertView.findViewById(R.id.img1); holder2.img2=convertView.findViewById(R.id.img2); holder2.img3=convertView.findViewById(R.id.img3); holder2.title1=convertView.findViewById(R.id.title); convertView.setTag(holder2); }else{ holder2= (ViewHolder2) convertView.getTag(); } ImageLoader.getInstance().displayImage(list.get(position).getThumbnail_pic_s(),holder2.img1); ImageLoader.getInstance().displayImage(list.get(position).getThumbnail_pic_s02(),holder2.img2); ImageLoader.getInstance().displayImage(list.get(position).getThumbnail_pic_s03(),holder2.img3); holder2.title1.setText(list.get(position).getTitle()); break; } return convertView; } class ViewHolder1{ ImageView img; TextView title1; } class ViewHolder2{ ImageView img1; ImageView img2; ImageView img3; TextView title1; } }
package com.example.newss; 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.nostra13.universalimageloader.core.ImageLoader; import java.util.List; /** * Created by 。。。。 on 2018/9/14. */ class NewsTwoAdaptern extends BaseAdapter { private Context context; private List<NewsTwo.BodyBean.ResultBean> list; public NewsTwoAdaptern(Context context, List<NewsTwo.BodyBean.ResultBean> list) { this.context = context; this.list = list; } @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 View getView(int position, View convertView, ViewGroup parent) { ViewHolder1 holder1=null; if(convertView==null){ holder1=new ViewHolder1(); convertView=View.inflate(context,R.layout.item_one,null); holder1.img=convertView.findViewById(R.id.img); holder1.title1=convertView.findViewById(R.id.title); convertView.setTag(holder1); }else{ holder1= (ViewHolder1) convertView.getTag(); } ImageLoader.getInstance().displayImage(list.get(position).getBarnerPic(),holder1.img); holder1.title1.setText(list.get(position).getBarnerName()); return convertView; } class ViewHolder1{ ImageView img; TextView title1; } }
六
package com.example.newss; import android.content.Context; import android.content.Intent; import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ListView; import com.google.gson.Gson; import java.util.ArrayList; import java.util.List; public class OneFragment extends Fragment { private List<News.ResultBean.DataBean> list; private NewsAdapter adapter; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v=inflater.inflate(R.layout.fragment_one, container, false); ListView listView=v.findViewById(R.id.listview); list = new ArrayList<>(); adapter = new NewsAdapter(getActivity(),list); listView.setAdapter(adapter); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Intent intent=new Intent(getActivity(),ThreeActivity.class); String url = list.get(position).getUrl(); intent.putExtra("url",url); startActivity(intent); } }); return v; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); new AsyncTask<String, Integer, String>() { @Override protected String doInBackground(String... strings) { String result=HttpUtils.getHttpFroment(strings[0]); return result; } @Override protected void onPostExecute(String s) { super.onPostExecute(s); Gson gson=new Gson(); News news=gson.fromJson(s,News.class); if(news.getResult().getData()!=null){ list.clear(); list.addAll(news.getResult().getData()); adapter.notifyDataSetChanged(); } } }.execute("http://172.17.8.100/mobile/exam/findNewList"); } }
七
package com.example.newss; import android.content.Context; import android.content.Intent; import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; import android.os.Handler; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.text.TextUtils; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ListView; import com.bwie.xlistview.XListView; import com.google.gson.Gson; import java.util.ArrayList; import java.util.List; public class TwoFragment extends Fragment { private XListView xListView; private List<NewsTwo.BodyBean.ResultBean> list; private NewsTwoAdaptern adaptern; private int currentPage=1; Handler handler=new Handler(); @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View v=inflater.inflate(R.layout.fragment_two, container, false); xListView = v.findViewById(R.id.xlistview); list = new ArrayList<>(); adaptern = new NewsTwoAdaptern(getActivity(), list); xListView.setAdapter(adaptern); xListView.setPullRefreshEnable(true); xListView.setPullLoadEnable(true); xListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Intent intent=new Intent(getActivity(),ThreeActivity.class); String url = list.get((int) id).getUrl(); intent.putExtra("url",url); startActivity(intent); } }); xListView.setXListViewListener(new XListView.IXListViewListener() { @Override public void onRefresh() { currentPage=1; getData(currentPage,false); } @Override public void onLoadMore() { currentPage++; getData(currentPage,true); } }); return v; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); getData(1,false); } private void getData(int i, final boolean b) { new AsyncTask<String, Integer, String>() { @Override protected String doInBackground(String... strings) { String result= HttpUtils.getHttpFroment(strings[0]); return result; } @Override protected void onPostExecute(String s) { super.onPostExecute(s); if(!TextUtils.isEmpty(s)){ Gson gson=new Gson(); NewsTwo newsTwo=gson.fromJson(s,NewsTwo.class); if(newsTwo!=null){ if(newsTwo.getBody()!=null){ if(newsTwo.getBody().getResult()!=null){ if(!b) { list.clear(); } list.addAll(newsTwo.getBody().getResult()); adaptern.notifyDataSetChanged(); } } } handler.postDelayed(new Runnable() { @Override public void run() { if(b){ xListView.stopLoadMore(); }else{ xListView.stopRefresh(); xListView.setRefreshTime("刚刚"); } } },2000); } } }.execute("https://api.yunxuekeji.cn/yunxue_app_api/content/getIndexBanner/"+currentPage); } }
八
package com.example.newss; import android.os.AsyncTask; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.ListView; import android.widget.TextView; import com.bwie.xlistview.XListView; import com.google.gson.Gson; import java.util.ArrayList; import java.util.List; public class TwoActivity extends AppCompatActivity { private List<Fragment> list; private int page=0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_two); final TextView one=findViewById(R.id.one); final ViewPager pager=findViewById(R.id.pager); list = new ArrayList<>(); OneFragment oneFragment=new OneFragment(); TwoFragment twoFragment=new TwoFragment(); list.add(oneFragment); list.add(twoFragment); one.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (page==0){ page=1; pager.setCurrentItem(1); }else { page=0; pager.setCurrentItem(0); } } }); pager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) { @Override public Fragment getItem(int position) { return list.get(position); } @Override public int getCount() { return list.size(); } }); } }
九
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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"> <android.support.v4.view.ViewPager android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="300dp"></android.support.v4.view.ViewPager> <WebView android:id="@+id/wv" android:layout_width="match_parent" android:layout_height="match_parent"></WebView> </LinearLayout>
十
package com.example.newss; import android.content.Intent; import android.nfc.tech.NfcB; import android.os.Handler; import android.os.Message; import android.support.annotation.NonNull; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.ImageView; import java.util.ArrayList; import java.util.List; public class ThreeActivity extends AppCompatActivity { private ViewPager pager; private List<View> list; Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); if(msg.what==123){ int index=pager.getCurrentItem(); if(index<list.size()-1){ index++; }else{ index=0; } handler.sendEmptyMessageDelayed(123,3000); pager.setCurrentItem(index); } } }; private WebView wv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_three); pager = findViewById(R.id.pager); wv = findViewById(R.id.wv); Intent intent=getIntent(); String urls = intent.getStringExtra("url"); wv.loadUrl(urls); wv.setWebViewClient(new WebViewClient()); list = new ArrayList<>(); View v1=View.inflate(this,R.layout.item_image,null); ImageView img1=v1.findViewById(R.id.img); img1.setImageResource(R.mipmap.a1); list.add(v1); View v2=View.inflate(this, R.layout.item_image, null); ImageView img2=(ImageView) v2.findViewById(R.id.img); img2.setImageResource(R.mipmap.a2); list.add(v2); View v3=View.inflate(this, R.layout.item_image, null); ImageView img3=(ImageView) v3.findViewById(R.id.img); img3.setImageResource(R.mipmap.a3); list.add(v3); View v4=View.inflate(this, R.layout.item_image, null); ImageView img4=(ImageView) v4.findViewById(R.id.img); img4.setImageResource(R.mipmap.a4); list.add(v4); pager.setAdapter(new PagerAdapter() { @NonNull @Override public Object instantiateItem(@NonNull ViewGroup container, int position) { View v=list.get(position); container.addView(v); return v; } @Override public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) { View v=list.get(position); container.removeView(v); } @Override public int getCount() { return list.size(); } @Override public boolean isViewFromObject(@NonNull View view, @NonNull Object object) { return view==object; } }); } @Override protected void onResume() { super.onResume(); handler.sendEmptyMessageDelayed(123,3000); } @Override protected void onPause() { super.onPause(); handler.removeCallbacksAndMessages(null); } }
封装
package com.example.newss; import android.content.Context; import android.content.Intent; import android.os.Build; import android.os.Bundle; import android.os.PersistableBundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; /** * Created by 。。。。 on 2018/9/14. */ public abstract class BaseActivity extends AppCompatActivity { protected Context context; protected Intent intent; protected Bundle bundle; @Override public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) { super.onCreate(savedInstanceState, persistentState); previewAction(); setContentView(getcontentview()); context = this; intent = getIntent(); if(intent!=null){ bundle = intent.getExtras(); } initview(); initdata(); setlisttener(); setmoreaction(); } private void setmoreaction() { } private void setlisttener() { } private void initdata() { } protected abstract void initview(); protected abstract int getcontentview(); private void previewAction() { } }