最近2个月一直忙着做项目,一个是挑战杯的互联网专项赛事,最终获得了国家三等奖。另一个是湖南省程序设计大赛应用开发类。终于忙完了。其中需要在listview中嵌入viewpager。于是网上学习了下。主要在第一个item中加入viewpager。
这里贴出适配器的代码
package zero.com.nweq.adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
import zero.com.nweq.R;
import zero.com.nweq.beans.House;
/**
* 首页适配器
* Created by 邓慧 on 15/7/1.
*/
public class HomeAdapter extends BaseAdapter {
private static final int TYPE_LISTVIEW = 0;
private static final int TYPE_VIEWPAGER = 1;
private static final int TYPE_COUNT = 2;
private Context context;
private List<House> list;
private View viewpager;
public HomeAdapter(Context c,List<House> list){
super();
this.context =c;
this.list =list;
}
/**
* 因为第一个不计算在内,估总数减1
* @return */
@Override
public int getCount() {
return list.size()-1;
}
/**
* 判断位置返回不同对象
* @param position
* @return
*/
@Override
public Object getItem(int position) {
if(position == 0){
return viewpager;
}else{
return list.get(position-1);
}
}
@Override
public long getItemId(int position) {
return position;
}
/**
* 主要方法
* @param position
* @param convertView
* @param parent
* @return
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder = null;
House house = null;
switch (getItemViewType(position)) {
case TYPE_LISTVIEW:
house = list.get(position);
if (convertView == null) {
holder = new Holder();
convertView = LayoutInflater.from(context).inflate(R.layout.theme_list_view_item, null);
holder.img = (ImageView) convertView.findViewById(R.id.theme_house_img);
holder.name = (TextView) convertView.findViewById(R.id.theme_house_name);
holder.place = (TextView) convertView.findViewById(R.id.theme_house_place);
holder.time = (TextView) convertView.findViewById(R.id.theme_house_time);
holder.theme = (TextView) convertView.findViewById(R.id.theme_house_theme);
convertView.setTag(holder);
} else {
holder = (Holder) convertView.getTag();
}
break;
case TYPE_VIEWPAGER:
if (convertView == null) {
convertView = viewpager;
}
return convertView;
}
//之所以要减一,是因为复用convertView时,要排除第一个位置
if (viewpager != null){
position--;
}
holder.img.setImageResource(house.getImageId());
holder.name.setText(house.getHouseName());
holder.place.setText(house.getHousePlace());
holder.time.setText(house.getTime());
holder.theme.setText(house.getTheme());
return convertView;
}
/**
* 提供方法从外部设置布局,这里可以设置不同的自定义Header布局,不仅仅是ViewPager
* @param view
*/
public void setViewpager(View view){
viewpager = view;
}
/**
* 获取不同的布局类型
* @param position
* @return
*/
@Override
public int getItemViewType(int position) {
if(viewpager != null){
return position > 0 ? TYPE_LISTVIEW : TYPE_VIEWPAGER;
}else{
return TYPE_LISTVIEW;
}
}
/**
* Item布局总样数
* @return
*/
@Override
public int getViewTypeCount() {
return TYPE_COUNT;
}
public class Holder{
ImageView img;
TextView name;
TextView theme;
TextView place;
TextView time;
}
}