listView 多条目展示 的难点在于,如何通过getItemViewType(int position) 获取不同类型数据?
前期 定义几个常量,方便在getView() 方法中直接获取getItemViewType(int position) 的类型
方式
package adapter;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.hyl.mis.houyongliang20161024.R;
import com.lidroid.xutils.BitmapUtils;
import org.w3c.dom.Text;
import java.util.List;
import Bean.BeanDate;
import Bean.BeanList;
/**
* Created by mis on 2016/10/25.
*/
public class MyAdapter extends BaseAdapter {
private static final int typeOne = 0;
private static final int typeTwo = 1;
private Context context;
private List items;
public MyAdapter(Context context, List items) {
this.context = context;
this.items = items;
}
public MyAdapter() {
super();
}
@Override
public int getCount() {
return items.size();
}
@Override
public BeanDate getItem(int i) {
return items.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public int getItemViewType(int position) {
if (TextUtils.isEmpty(items.get(position).url)) {
return typeOne;
} else {
return typeTwo;
}
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder1 vh1 = null;
ViewHolder2 vh2 = null;
int type = getItemViewType(i);
if (view == null) {
switch (type) {
case typeOne:
vh1=new ViewHolder1();
view=View.inflate(context,R.layout.listview_item_nopic,null);
vh1.vh1_name= (TextView) view.findViewById(R.id.item1_nickname);
vh1.vh1_body= (TextView) view.findViewById(R.id.item1_body);
view.setTag(vh1);
break;
case typeTwo:
vh2=new ViewHolder2();
view=View.inflate(context,R.layout.listview_item_pic,null);
vh2.vh2_name= (TextView) view.findViewById(R.id.item2_nickname);
vh2.vh2_body= (TextView) view.findViewById(R.id.item2_body);
vh2.iv= (ImageView) view.findViewById(R.id.item2_iv);
view.setTag(vh2);
break;
default:
break;
}
}
switch (type) {
case typeOne:
vh1= (ViewHolder1) view.getTag();
vh1.vh1_name.setText(items.get(i).nickname);
vh1.vh1_body.setText(items.get(i).body);
break;
case typeTwo:
vh2= (ViewHolder2) view.getTag();
vh2.vh2_name.setText(items.get(i).nickname);
vh2.vh2_body.setText(items.get(i).body);
BitmapUtils bu=new BitmapUtils(context);
bu.display(vh2.iv,"http://res.dxycdn.com/upload"+items.get(i).url);
break;
default:
break;
}
return view;
}
class ViewHolder1 {
TextView vh1_name, vh1_body;
}
class ViewHolder2 {
TextView vh2_name, vh2_body;
ImageView iv;
}
}
package com.hyl.mis.houyongliang20161024;
import android.app.Activity;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import com.google.gson.Gson;
import com.lidroid.xutils.BitmapUtils;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import Bean.BeanDate;
import Bean.BeanList;
import Bean.BeanPic;
import Bean.Content;
import Bean.LVPic;
import adapter.MyAdapter;
import utils.MyHttp;
import static android.R.attr.tag;
import static android.R.id.list;
import static android.icu.lang.UCharacter.GraphemeClusterBreak.L;
import static android.view.View.inflate;
public class MainActivity extends Activity {
LinearLayout.LayoutParams params;
private ListView lv;
private ViewPager vp;
private View v;
private String path = "http://i.dxy.cn/snsapi/event/count/list/all";
private String tag = "MainActivity";
private TextView tv_title;
List list = null;
private LinearLayout ll;
private List listDot;
String path2 = "http://i.dxy.cn/snsapi/home/feeds/list/all?sid=4df0360f-2a20-4198-beb8-4dc5660c4f08&u=zhetianyishou&s=10&mc=0000000049029dcaffffffff99d603a9&token=TGT-13165-buaw5fHpqLlefw9bSOB0oF41fobaV4rMZmK-50&hardName=iToolsAVM_T0008098S&ac=4124c5f1-2029-4fda-b06f-a87ac5ad8d11&bv=2013&vc=6.0.6&tid=c25e673d-e82a-4e46-bd4e-c1e86d497126&vs=4.4.4&ref_tid=54720e1a-7eed-4993-9f51-3d760f3d0b2estart.firefoxchina.cn";
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
int what = msg.what;
if (what == 0) {
List items = (List) msg.obj;
lv.addHeaderView(v);//增加头布局
lv.setAdapter(new MyAdapter(MainActivity.this, items));
}
}
};
private List items;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化控件
initView();
initData();
initEvent();
}
private void initEvent() {
vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
if (list != null) {
int index = position % list.size();
tv_title.setText(list.get(index).title);
for (int i = 0; i < list.size(); i++) {
if (listDot != null) {
ImageView iv = listDot.get(i);
params = new LinearLayout.LayoutParams(10, 10);
params.leftMargin = 10;
iv.setLayoutParams(params);
if (index == i) {
iv.setBackgroundResource(R.drawable.shape_dot_selected);
} else {
iv.setBackgroundResource(R.drawable.shape_dot_nomal);
}
}
}
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
private void initData() {
MyTask myTask = new MyTask();
myTask.execute(path);
//通过线程进行处理,数据
new Thread() {
@Override
public void run() {
try {
HttpURLConnection conn = (HttpURLConnection) new URL(path2).openConnection();
int code = conn.getResponseCode();
if (code == 200) {
InputStream inputStream = conn.getInputStream();
String json = MyHttp.getJson(inputStream);
Gson gson = new Gson();
BeanList beanList = gson.fromJson(json, BeanList.class);
List items = beanList.getItems();
List dates = new ArrayList();
BeanDate beandate = null;
for (int i = 0; i < items.size(); i++) {
beandate = new BeanDate();
beandate.infoAvatar = items.get(i).getInfoAvatar();
String content = items.get(i).getContent();
Content content1 = gson.fromJson(content, Content.class);
beandate.bid = content1.getBid();
beandate.body = content1.getBody();
beandate.canVote = content1.getCanVote();
beandate.name = content1.getName();
beandate.nickname = content1.getNickname();
beandate.pid = content1.getPid();
beandate.rawLength = content1.getRawLength();
beandate.reply = content1.getReply();
beandate.subject = content1.getSubject();
beandate.url = content1.getUrl();
beandate.voted = content1.getVoted();
beandate.votes = content1.getVotes();
dates.add(beandate);
}
Message msg = Message.obtain();
msg.obj = dates;
msg.what = 0;
handler.sendMessage(msg);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}
private void intDots() {
listDot = new ArrayList();
for (int i = 0; i < list.size(); i++) {
ImageView iv = new ImageView(MainActivity.this);
if (i == 0) {
params = new LinearLayout.LayoutParams(10, 10);
params.leftMargin = 10;
iv.setLayoutParams(params);
iv.setBackgroundResource(R.drawable.shape_dot_selected);
} else {
params = new LinearLayout.LayoutParams(10, 10);
params.leftMargin = 10;
iv.setLayoutParams(params);
iv.setBackgroundResource(R.drawable.shape_dot_nomal);
}
listDot.add(iv);
ll.addView(iv);
}
}
private void initView() {
lv = (ListView) findViewById(R.id.lv);
v = View.inflate(this, R.layout.include_vp, null);
vp = (ViewPager) v.findViewById(R.id.vp);
tv_title = (TextView) v.findViewById(R.id.tv_title);
ll = (LinearLayout) v.findViewById(R.id.ll);
}
class MyTask extends AsyncTask> {
@Override
protected List doInBackground(String... strings) {
List list = new ArrayList();
try {
HttpURLConnection conn = (HttpURLConnection) new URL(strings[0]).openConnection();
conn.setConnectTimeout(5000);
conn.setReadTimeout(5000);
Log.e(tag, "strings[0]" + strings[0]);
Log.e(tag, "conn.getResponseCode()" + conn.getResponseCode());
if (conn.getResponseCode() == 200) {
InputStream inputStream = conn.getInputStream();
String json = MyHttp.getJson(inputStream);
Log.e(tag, "json" + json);
Gson gson = new Gson();
BeanPic beanPic = gson.fromJson(json, BeanPic.class);
List items = beanPic.getItems();
LVPic lvpic = null;
for (int i = 0; i < items.size(); i++) {
lvpic = new LVPic();
BeanPic.ItemsBean itemsBean = items.get(i);
lvpic.title = itemsBean.getTitle();
lvpic.picpath = itemsBean.getPath();
list.add(lvpic);
}
}
} catch (IOException e) {
e.printStackTrace();
}
return list;
}
@Override
protected void onPostExecute(List lvPics) {
super.onPostExecute(lvPics);
list = lvPics;
vp.setAdapter(new MyPagerAdapter(lvPics));
vp.setCurrentItem(10000 * lvPics.size());
intDots();
}
}
class MyPagerAdapter extends PagerAdapter {
private List lvPics;
MyPagerAdapter(List lvPics) {
this.lvPics = lvPics;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return Integer.MAX_VALUE;
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
Log.e(tag, "list.SIZE" + lvPics.size());
return arg0 == arg1;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView image = new ImageView(MainActivity.this);
int index = position % lvPics.size();
//设置图片源
BitmapUtils bit = new BitmapUtils(MainActivity.this);
bit.display(image, lvPics.get(index).picpath);
image.setScaleType(ImageView.ScaleType.CENTER_CROP);
//设置宽高
image.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
container.addView(image);
return image;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((ImageView) object);
}
}
}
public class MyHttp {
public static void getData(HttpRequest.HttpMethod method, String url, RequestParams params, RequestCallBack callBack){
HttpUtils http=new HttpUtils();
http.send(method,url,params,callBack);
}
public static String getJson(InputStream stream) {
ByteArrayOutputStream baos=null;
try {
baos=new ByteArrayOutputStream();
int len=0;
byte[] buffer=new byte[1024];
while((len=stream.read(buffer))!=-1){
baos.write(buffer,0,len);
}
} catch (IOException e) {
e.printStackTrace();
}
return baos.toString();
}
}
shape布局
`
`
`
` `
` `
其他布局
主布局