您好,登錄后才能下訂單哦!
原生android,高仿大眾點評商家列表;
首先歡迎大家關注我 http://weibo.com/u/2841033197
廢話不多說了,上代碼,效果圖
適配器
class MyAdapter extends BaseAdapter {
protected final int mItemLayoutId;
List<Businesses> data = new ArrayList<Businesses>();
private Context context;
private LayoutInflater inflater;
private ImageScaleType p_w_picpathScaleType;
private DisplayImageOptions options;
public MyAdapter(List<Businesses> data, Context cxt, int itemLayoutId, ImageScaleType p_w_picpathScaleType) {
this.data = data;
this.context = cxt;
this.mItemLayoutId = itemLayoutId;
this.inflater = LayoutInflater.from(context);
this.p_w_picpathScaleType = p_w_picpathScaleType;
init();
}
/** 初始化圖片緩存 options **/
private void init() {
options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.thumb)
// .showImageOnFail(R.drawable.ic_error)
.cacheInMemory(true)
.cacheOnDisk(true)
.considerExifParams(true)
.p_w_picpathScaleType(p_w_picpathScaleType != null ? p_w_picpathScaleType: ImageScaleType.EXACTLY)
.delayBeforeLoading(10)
.displayer(new SimpleBitmapDisplayer())// //正常顯示一張圖片
.build();
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return data != null ? data.size() : 0;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return data != null ? data.get(position) : null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = getViewHolder(position, convertView, parent);
TextView business_id_tv = holder.getView(R.id.business_id_tv);
TextView name_tv = holder.getView(R.id.name_tv);
TextView avg_price_tv = holder.getView(R.id.avg_price_tv);
TextView regions_tv = holder.getView(R.id.regions_tv);
TextView distance_tv = holder.getView(R.id.distance_tv);
String s_photo_url = data.get(position).getS_photo_url();
String rating_s_img_url = data.get(position).getRating_img_url();
int business_id = data.get(position).getBusiness_id();
String name = data.get(position).getName();
String branch_name = data.get(position).getBranch_name();
int avg_price = data.get(position).getAvg_price();
String regions = data.get(position).getRegions();
String categories = data.get(position).getCategories();
int distance = data.get(position).getDistance();
holder.displayImages(R.id.s_photo_url_iv, s_photo_url);
holder.displayImages(R.id.star_iv, rating_s_img_url);
business_id_tv.setText("" + business_id);
name = name.substring(0, name.indexOf("("));
if (!StringUtils.isEmpty(branch_name)) {
name = name + "(" +branch_name +")";
}
name_tv.setText(name);
if (avg_price > 0) {
avg_price_tv.setText("¥" + avg_price +"/人");
}
// 過濾下數據
regions = regions.replace("\"", "").replace("[", "").replace("]", "").split(",")[1];
categories = categories.replace("\"", "").replace("[", "").replace("]", "");
regions_tv.setText(regions + " " + categories);
if (distance > 0) {
float die = distance;
DecimalFormat decimalFormat=new DecimalFormat(".0");
if(die >= 1000) {
// 大于1000 米 轉化為千米展示數據
die /= 1000;
String temp_die = decimalFormat.format(die);
temp_die = temp_die.replace(".0", "");
distance_tv.setText(temp_die +"km" );
} else {
distance_tv.setText(distance +"m" );
}
}
setClick(data, holder.getConvertView(), position);
return holder.getConvertView();
}
private ViewHolder getViewHolder(int position, View convertView,
ViewGroup parent) {
return ViewHolder.get(context, convertView, parent, mItemLayoutId,
position, options);
}
private void setClick(final List<Businesses> datas, final View convertView,
final int position) {
convertView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
int business_ids = datas.get(position).getBusiness_id();
String keywords = "";
try {
keywords = URLEncoder.encode("" + business_ids, "UTF-8");
Uri url = Uri.parse("dianping://shopinfo?id=" + keywords);
Intent intent = new Intent(Intent.ACTION_VIEW, url);
context.startActivity(intent);
} catch (Exception e) {
// 沒有安裝應用,默認打開HTML5站
Intent intent = new Intent(Intent.ACTION_VIEW, Uri
.parse("http://m.dianping.com/search.aspx?skey="
+ keywords));
context.startActivity(intent);
}
}
});
}
源碼本人放在我的常用博客上了, 傳送門
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。