public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener { private TabLayout tablayout; private ViewPager viewpage; private Listlist=new ArrayList (); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //设置tablayout数据 settablayoutdata(); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } }); DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); ActionBarDrawerToggle toggle = new ActionBarDrawerToggle( this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); drawer.setDrawerListener(toggle); toggle.syncState(); NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); navigationView.setNavigationItemSelectedListener(this); } //设置tablayout数据 private void settablayoutdata() { tablayout = (TabLayout) findViewById(R.id.tablayout); viewpage = (ViewPager) findViewById(R.id.viewpage); list.add("头条"); list.add("社会"); list.add("国内"); list.add("国际"); list.add("娱乐"); list.add("体育"); list.add("军事"); list.add("科技"); list.add("财经"); list.add("时尚"); tablayout.setupWithViewPager(viewpage); viewpage.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) { @Override public CharSequence getPageTitle(int position) { return list.get(position); } @Override public Fragment getItem(int position) { NewFragment newFragment = new NewFragment(); Bundle bundle = new Bundle(); if (list.get(position).equals("头条")){ bundle.putString("name","top"); }else if (list.get(position).equals("社会")){ bundle.putString("name","shehui"); }else if (list.get(position).equals("国内")){ bundle.putString("name","guonei"); }else if (list.get(position).equals("国际")){ bundle.putString("name","guoji"); }else if (list.get(position).equals("娱乐")){ bundle.putString("name","yule"); }else if (list.get(position).equals("体育")){ bundle.putString("name","tiyu"); }else if (list.get(position).equals("军事")){ bundle.putString("name","junshi"); }else if (list.get(position).equals("科技")){ bundle.putString("name","keji"); }else if (list.get(position).equals("财经")){ bundle.putString("name","caijing"); }else if (list.get(position).equals("时尚")){ bundle.putString("name","shishang"); } //给fragment 加bundle 数据 //activity与fragment 1.getset,2.接口回调,3.setArguments ,getAraguments newFragment.setArguments(bundle); return newFragment; } @Override public int getCount() { return list.size(); } }); } //设置listview数据 private void setlistviewdata() { } @Override public void onBackPressed() { DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); if (drawer.isDrawerOpen(GravityCompat.START)) { drawer.closeDrawer(GravityCompat.START); } else { super.onBackPressed(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } @SuppressWarnings("StatementWithEmptyBody") @Override public boolean onNavigationItemSelected(MenuItem item) { // Handle navigation view item clicks here. int id = item.getItemId(); if (id == R.id.nav_camera) { // Handle the camera action } else if (id == R.id.nav_gallery) { } else if (id == R.id.nav_slideshow) { } else if (id == R.id.nav_manage) { } else if (id == R.id.nav_share) { } else if (id == R.id.nav_send) { } DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); drawer.closeDrawer(GravityCompat.START); return true; } }
==================================================================
public class NewFragment extends Fragment{ private ListView listview; private View view; private HttpURLConnection conne; private Listdata ; private PullToRefreshLayout pull; public String aa=""; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { //初始化布局 view = View.inflate(getActivity(), R.layout.listview,null); //初始化控件 pull = (PullToRefreshLayout) view.findViewById(R.id.pull); listview = view.findViewById(R.id.list); //pulltorefresh的点击事件 pull.setRefreshListener(new BaseRefreshListener() { @Override //刷新 public void refresh() { pull.finishRefresh(); } @Override //加载 public void loadMore() { pull.finishLoadMore(); } }); dianji(); return view; } private void dianji() { //listview的点击跳转 listview.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView> adapterView, View view, int i, long l) { Intent intent = new Intent(getActivity(),Main2Activity.class); intent.putExtra("name",aa); startActivity(intent); } }); } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); Bundle bundle = getArguments(); //接收传递过来的值 String string = bundle.getString("name", "top"); //调用解析方法 Jiexi(string); } private void Jiexi(final String string) { //异步加载 new AsyncTask ,String ,String >(){ String myurl="http://v.juhe.cn/toutiao/index?type="+string+"&key=8c6e9ca2058eaffcc04bcc4c0f5920c6"; protected String doInBackground(String... strings) { //网络请求数据 String str=""; try { URL url = new URL(myurl); conne = (HttpURLConnection) url.openConnection(); conne.setConnectTimeout(5000); conne.setReadTimeout(5000); int responseCode = conne.getResponseCode(); if (responseCode==200){ InputStream in = conne.getInputStream(); byte[] by=new byte[1024]; int len=0; while ((len=in.read(by))!=-1){ str+=new String(by,0,len); } } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return str; } @Override protected void onPostExecute(String s) { //gson解析 aa=s; Gson gson = new Gson(); NewDatas newDatas = gson.fromJson(s, NewDatas.class); data = newDatas.getResult().getData(); //listview适配器 MyAdapter myAdapter = new MyAdapter(getActivity(),data); listview.setAdapter(myAdapter); super.onPostExecute(s); } }.execute(); } }
=========================================================
public class App extends Application { @Override public void onCreate() { DisplayImageOptions options = new DisplayImageOptions.Builder() .showImageOnLoading(R.mipmap.ic_launcher) .displayer(new FadeInBitmapDisplayer(2000)).cacheOnDisk(true) .cacheInMemory(true).build(); ImageLoaderConfiguration configs = new ImageLoaderConfiguration.Builder( this).defaultDisplayImageOptions(options).threadPoolSize(5) .build(); ImageLoader.getInstance().init(configs); super.onCreate(); } }
===========================================================
public class Main2Activity extends AppCompatActivity { private GridView r; private ListView lsit; private List==========================================data; //轮播下面的小点(小圆点是本地的,自己导入的图片) private ConvenientBanner convenientBanner; //图片加载地址的集合 private List bean=new ArrayList (); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //初始化布局 setContentView(R.layout.activity_main2); // 初始化控件 r = (GridView) findViewById(R.id.r); lsit = (ListView) findViewById(R.id.list); //得到传递过来的数据 Intent intent = getIntent(); String name = intent.getStringExtra("name"); //解析从网络上获取到的数据 Gson gson = new Gson(); NewDatas newDatas = gson.fromJson(name, NewDatas.class); data = newDatas.getResult().getData(); //设置适配器 MyAdapter myAdapter = new MyAdapter(Main2Activity.this, data); r.setAdapter(myAdapter); lsit.setAdapter(myAdapter); String a = data.get(0).getThumbnail_pic_s(); String a1 = data.get(0).getThumbnail_pic_s02(); String a2 = data.get(0).getThumbnail_pic_s03(); bean.add(a); bean.add(a1); bean.add(a2); convenientBanner = (ConvenientBanner) findViewById(R.id.convenientBanner); // bean = Arrays.asList(im convenientBanner.setManualPageable(true); //设置自动轮播的时间 convenientBanner.startTurning(2000); //设置点击事件 //泛型为具体实现类ImageLoaderHolder convenientBanner.setPages(new CBViewHolderCreator () { @Override public NetImageLoadHolder createHolder() { return new NetImageLoadHolder(); } }, bean); //设置每个pager的点击事件 convenientBanner.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(int position) { Toast.makeText(Main2Activity.this, "点击了" + convenientBanner.getCurrentItem(), Toast.LENGTH_SHORT).show(); } }); } public class NetImageLoadHolder implements Holder { private ImageView image_lv; //可以是一个布局也可以是一个Imageview @Override public ImageView createView(Context context) { image_lv = new ImageView(context); image_lv.setScaleType(ImageView.ScaleType.FIT_XY); return image_lv; } @Override public void UpdateUI(Context context, int position, String data) { //Glide框架 Glide.with(context).load(data).into(image_lv); } } }
public class MyAdapter extends BaseAdapter { private Context context; private Listlist; public MyAdapter(Context context, List list) { this.context = context; this.list = list; } public static final int TYPE_ONE = 0; public static final int TYPE_TWO = 1; @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 int getItemViewType(int position) { //判断view类型 int p = position % 2; if (p == 0) { return TYPE_ONE; } else { return TYPE_TWO; } } @Override public int getViewTypeCount() { return 2; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; ViewHolder1 holder1 = null; int type = getItemViewType(position); if (convertView == null) { if (type==TYPE_ONE){ holder = new ViewHolder(); convertView = View.inflate(context, R.layout.list_item_one, null); holder.lv_iv = (ImageView) convertView.findViewById(R.id.lv_iv); holder.lv_tv = (TextView) convertView.findViewById(R.id.lv_tv); holder.lv_time = (TextView) convertView.findViewById(R.id.lv_time); convertView.setTag(holder); }else { holder1 = new ViewHolder1(); convertView = View.inflate(context, R.layout.list_view_two, null); holder1.lv2_iv1 = (ImageView) convertView.findViewById(R.id.lv2_iv1); holder1.lv2_iv2 = (ImageView) convertView.findViewById(R.id.lv2_iv2); convertView.setTag(holder1); } } else { if (type==TYPE_ONE){ holder = (ViewHolder) convertView.getTag(); } else { holder1 = (ViewHolder1) convertView.getTag(); } } if (type==TYPE_ONE){ ImageLoader.getInstance().displayImage(list.get(position).getThumbnail_pic_s(), holder.lv_iv); holder.lv_tv.setText(list.get(position).getTitle()); holder.lv_time.setText(list.get(position).getAuthor_name());} else { ImageLoader.getInstance().displayImage(list.get(position).getThumbnail_pic_s02(), holder1.lv2_iv1); ImageLoader.getInstance().displayImage(list.get(position).getThumbnail_pic_s03(), holder1.lv2_iv2); } return convertView; } class ViewHolder { ImageView lv_iv; TextView lv_tv; TextView lv_time; } class ViewHolder1{ ImageView lv2_iv1; ImageView lv2_iv2; } }
===============================================