在现实的项目开发中,listView往往会加载不同类型的数据,这就要求我们要对listView进行多条目的加载,对数据类型进行判断,怎么判断呢?主要在适配器里面进行类型的判断。
代码如下:在listView适配器重写的四个方法的基础上,再手动重写两个方法,分别是:getViewTypeCount(),getItemViewType(int position),这两个方法,第一个是确定加载条木有几种数据类型,第二个是对数据进行判断和分类的。
package com.example.wangrx.wangruixin1509c20170909; import android.app.Activity; import android.os.Bundle; import android.widget.ListView; public class MainActivity extends Activity { String path = "http://v.juhe.cn/toutiao/index?type=top&key=2f092bd9ce76c0257052d6d3c93c11b4"; private ListView lv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); initGetData(path); } private void init(){ lv = findViewById(R.id.lv); } private void initGetData(String path){ MyTask myTask = new MyTask(MainActivity.this,path,"GET",lv); myTask.execute(); } }
AsyncTask请求:
package com.example.wangrx.wangruixin1509c20170909; import android.content.Context; import android.content.Intent; import android.os.AsyncTask; import android.view.View; import android.widget.AdapterView; import android.widget.ListView; import com.google.gson.Gson; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.util.List; import bean.JavaBean; /** * Created by Wangrx on 2017/9/8. */ class MyTask extends AsyncTask<String,Integer,String>{ private String path; private Context context; private String get; private ListView lv; private List<JavaBean.ResultBean.DataBean> list; public MyTask(MainActivity mainActivity, String path, String get, ListView lv) { this.context = mainActivity; this.path = path; this.get = get; this.lv = lv; } @Override protected String doInBackground(String... strings) { try { URL url = new URL(path); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod(get); connection.setReadTimeout(5000); connection.setConnectTimeout(5000); int responseCode = connection.getResponseCode(); if (responseCode == 200){ InputStream inputStream = connection.getInputStream(); String s = steamToString(inputStream, "utf-8"); return s; } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(String s) { super.onPostExecute(s); Gson gson = new Gson(); JavaBean bean = gson.fromJson(s, JavaBean.class); list = bean.getResult().getData(); MyAdapter myAdapter = new MyAdapter(context, list); lv.setAdapter(myAdapter); lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { String url = list.get(i).getUrl(); Intent intent = new Intent(context, Main2Activity.class); intent.putExtra("url",url); context.startActivity(intent); } }); } private String steamToString(InputStream inputStream,String charset){ try { InputStreamReader inputStreamReader = new InputStreamReader(inputStream, charset); BufferedReader reader = new BufferedReader(inputStreamReader); String s = null; StringBuilder builder = new StringBuilder(); while ((s=reader.readLine())!=null) { builder.append(s); } reader.close(); return builder.toString(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } }设置适配器:
package com.example.wangrx.wangruixin1509c20170909; 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; import bean.JavaBean; /** * Created by Wangrx on 2017/9/8. */ class MyAdapter extends BaseAdapter{ private Context context; private List<JavaBean.ResultBean.DataBean> list; private static final int typeone = 0; private static final int typetwo = 1; private static final int typethree = 2; public MyAdapter(Context context, List<JavaBean.ResultBean.DataBean> list) { this.context = context; this.list = list; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int i) { return list.get(i); } @Override public long getItemId(int i) { return i; } @Override public View getView(int i, View view, ViewGroup viewGroup) { ViewHolder1 holder1 = null; ViewHolder2 holder2 = null; ViewHolder3 holder3 = null; int type = getItemViewType(i); if (view == null){ switch (type){ case typeone: holder1 = new ViewHolder1(); view = View.inflate(context,R.layout.item1,null); holder1.item1_title = view.findViewById(R.id.item1_title); holder1.item1_img1 = view.findViewById(R.id.item1_img1); holder1.item1_img2 = view.findViewById(R.id.item1_img2); holder1.item1_img3 = view.findViewById(R.id.item1_img3); view.setTag(holder1); break; case typetwo: holder2 = new ViewHolder2(); view = View.inflate(context,R.layout.item2,null); holder2.item2_title = view.findViewById(R.id.item2_title); holder2.item2_img1 = view.findViewById(R.id.item2_img1); holder2.item2_img2 = view.findViewById(R.id.item2_img2); view.setTag(holder2); break; case typethree: holder3 = new ViewHolder3(); view = View.inflate(context,R.layout.item3,null); holder3.item3_title = view.findViewById(R.id.item3_title); holder3.item3_img1 = view.findViewById(R.id.item3_img1); view.setTag(holder3); break; } }else { switch (type){ case typeone: holder1 = (ViewHolder1) view.getTag(); holder1.item1_title.setText(list.get(i).getTitle()); ImageLoader.getInstance().displayImage(list.get(i).getThumbnail_pic_s(),holder1.item1_img1); ImageLoader.getInstance().displayImage(list.get(i).getThumbnail_pic_s02(),holder1.item1_img2); ImageLoader.getInstance().displayImage(list.get(i).getThumbnail_pic_s03(),holder1.item1_img3); break; case typetwo: holder2 = (ViewHolder2) view.getTag(); holder2.item2_title.setText(list.get(i).getTitle()); ImageLoader.getInstance().displayImage(list.get(i).getThumbnail_pic_s(),holder2.item2_img1); ImageLoader.getInstance().displayImage(list.get(i).getThumbnail_pic_s02(),holder2.item2_img2); break; case typethree: holder3 = (ViewHolder3) view.getTag(); holder3.item3_title.setText(list.get(i).getTitle()); ImageLoader.getInstance().displayImage(list.get(i).getThumbnail_pic_s(),holder3.item3_img1); break; } } return view; } @Override public int getItemViewType(int position) { if (!(list.get(position).getThumbnail_pic_s()==null)){ if (!(list.get(position).getThumbnail_pic_s02()==null)){ if (!(list.get(position).getThumbnail_pic_s03()==null)){ return typeone; }else { return typetwo; } }else{ return typethree; } }else { return -1; } } @Override public int getViewTypeCount() { return 3; } class ViewHolder1{ TextView item1_title; ImageView item1_img1; ImageView item1_img2; ImageView item1_img3; } class ViewHolder2{ TextView item2_title; ImageView item2_img1; ImageView item2_img2; } class ViewHolder3{ TextView item3_title; ImageView item3_img1; } }第二个页面 WebView:
package com.example.wangrx.wangruixin1509c20170909; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; public class Main2Activity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); Intent intent = getIntent(); String url = intent.getStringExtra("url"); WebView web = findViewById(R.id.web); web.loadUrl(url); web.setWebViewClient(new WebViewClient()); WebSettings settings = web.getSettings(); settings.setJavaScriptCanOpenWindowsAutomatically(true); settings.setJavaScriptEnabled(true); } }
图片加载:
package util; import android.app.Application; import com.example.wangrx.wangruixin1509c20170909.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.FadeInBitmapDisplayer; /** * Created by Wangrx on 2017/9/8. */ public class MyApplication extends Application{ @Override public void onCreate() { super.onCreate(); DisplayImageOptions options = new DisplayImageOptions.Builder() .showImageOnLoading(R.mipmap.ic_launcher) .displayer(new FadeInBitmapDisplayer(2000)).cacheOnDisc(true) .cacheInMemory(true).build(); ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder( this).defaultDisplayImageOptions(options).threadPoolSize(5) .build(); ImageLoader.getInstance().init(config); } }
布局文件:
main1:
main2:<?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" tools:context="com.example.wangrx.wangruixin1509c20170909.MainActivity"> <ListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="match_parent"></ListView> </RelativeLayout>
<?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" tools:context="com.example.wangrx.wangruixin1509c20170909.Main2Activity"> <WebView android:id="@+id/web" android:layout_width="match_parent" android:layout_height="match_parent"></WebView> </RelativeLayout>
item1:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/item1_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="25sp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <ImageView android:id="@+id/item1_img1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" /> <ImageView android:id="@+id/item1_img2" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" /> <ImageView android:id="@+id/item1_img3" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" /> </LinearLayout> </LinearLayout>item2:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/item2_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="25sp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <ImageView android:id="@+id/item2_img1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" /> <ImageView android:id="@+id/item2_img2" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" /> </LinearLayout> </LinearLayout>item3:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/item3_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="25sp" /> <ImageView android:id="@+id/item3_img1" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>