public class InfoAdapter extends BaseAdapter {
private Context context;
private List<NewsResponse.ResultBean> mNewResponse;
private List<BannerResponse.DataBean> mBannerResponse;
public InfoAdapter(Context context) {
this.context = context;
mNewResponse = new ArrayList<>();
mBannerResponse = new ArrayList<>();
}
@Override
public int getCount() {
return hasBanner() ? mNewResponse.size() + 1 : mNewResponse.size();
}
//设置banner数据
public void setBannerDatas(List<BannerResponse.DataBean> datas) {
if (datas != null) {
mBannerResponse = datas;
}
notifyDataSetChanged();
}
//更新数据
public void setNewDatas(List<NewsResponse.ResultBean> datas) {
if (datas != null) {
mNewResponse = datas;
}
notifyDataSetChanged();
}
//加载数据
public void addNewDatas(List<NewsResponse.ResultBean> datas) {
if (datas != null) {
mNewResponse.addAll(datas);
}
notifyDataSetChanged();
}
private BannerViewHolder viewHolder;
public void stop() {
if (viewHolder != null) {
viewHolder.stop();
}
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getItemViewType(int position) {
if (hasBanner() && position == 0) {
return 0;
}
return 1;
}
//是否有banner
private boolean hasBanner() {
return mBannerResponse.size() > 0;
}
@Override
public NewsResponse.ResultBean getItem(int position) {
if (hasBanner()){
if (position == 0){
return null;
}
return mNewResponse.get(position - 1);
}
return mNewResponse.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (getItemViewType(position) == 0) {
//Banner
BannerViewHolder bannerViewHolder;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.item_banner, parent, false);
bannerViewHolder = new BannerViewHolder(convertView);
} else {
bannerViewHolder = (BannerViewHolder) convertView.getTag();
}
this.viewHolder = bannerViewHolder;
bannerViewHolder.bindData(mBannerResponse);
} else {
NewsViewHolder viewHolder;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.item_news, parent, false);
viewHolder = new NewsViewHolder(convertView);
} else {
viewHolder = (NewsViewHolder) convertView.getTag();
}
viewHolder.bindData(getItem(position));
}
return convertView;
}
class BannerViewHolder {
private final BannerAdapter adapter;
ViewPager viewPager;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
viewPager.setCurrentItem(viewPager.getCurrentItem() + 1);
startSwitchPage();
}
};
public BannerViewHolder(View itemView) {
this.viewPager = itemView.findViewById(R.id.banner);
viewPager.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
stop();
}
if (event.getAction() == MotionEvent.ACTION_UP) {
startSwitchPage();
}
return false;
}
});
adapter = new BannerAdapter();
this.viewPager.setAdapter(adapter);
itemView.setTag(this);
}
public void bindData(List<BannerResponse.DataBean> bean) {
adapter.setBannerDatas(bean);
stop();
if (bean.size() > 1) {
viewPager.setCurrentItem(bean.size() * 5000);
startSwitchPage();
}
}
public void stop() {
handler.removeMessages(0);
}
public void startSwitchPage() {
if (viewPager.getAdapter().getCount() > 1) {
handler.sendMessageDelayed(handler.obtainMessage(0), 2000);
}
}
}
class NewsViewHolder {
TextView name;
TextView summary;
ImageView icon;
public NewsViewHolder(View itemView) {
this.name = itemView.findViewById(R.id.name);
this.summary = itemView.findViewById(R.id.summary);
icon = itemView.findViewById(R.id.icon);
itemView.setTag(this);
}
public void bindData(NewsResponse.ResultBean bean) {
name.setText(bean.getName());
summary.setText(bean.getSummary());
ImageLoader.getInstance().displayImage(bean.getImageUrl(), icon);
}
}
}
在这里插入代码片
有关列表的视图优化
最新推荐文章于 2022-12-12 10:52:27 发布