跑跑app模仿美团实例
登录首页:
创建首页布局视图代码展示:
<LinearLayout
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#E22319"
android:orientation="horizontal">
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:padding="10dp"
android:src="@mipmap/user" />
<EditText
android:layout_width="220dp"
android:layout_height="60dp"
android:padding="10dp"
android:text="请输入你需要搜索的商品" />
<TextView
android:layout_width="60dp"
android:layout_height="60dp"
android:gravity="center"
android:text="搜索"
android:textColor="#ffffff"
android:textSize="20sp" />
<ImageView
android:layout_width="50dp"
android:layout_height="60dp"
android:gravity="top"
android:padding="10dp"
android:src="@mipmap/goodsdetail_customer_service_nomal" />
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="280dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@mipmap/cai" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="horizontal"
android:padding="5dp">
<ImageView
android:id="@+id/tu1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="@mipmap/articles" />
<ImageView
android:id="@+id/tu2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="@mipmap/food" />
<ImageView
android:id="@+id/tu3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="@mipmap/agency" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal"
android:gravity="center">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<ImageView
android:id="@+id/zhuye_tu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@mipmap/main_check" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="主页"
android:textColor="#E22319" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<ImageView
android:id="@+id/weiwancheng_tu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="@mipmap/unfinished" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="未完成" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<ImageView
android:id="@+id/yiwancheng_tu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="@mipmap/completed" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="已完成" />
</LinearLayout>
</LinearLayout>
mainactivity步骤:
- 绑定ID,设置按钮监听
设置跳转传值至第二页面
代码展示:
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private ImageView richangbtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bindID();
}
private void bindID() {
richangbtn=findViewById(R.id.tu1);
richangbtn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.tu1:
Intent intent=new Intent(MainActivity.this,DailyActivity.class);
startActivity(intent);
break;
}
}
}
第二页面日常用品
布局视图代码activity_daily展示:
<ListView
android:id="@+id/daily_listview"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
布局视图代码daily_item展示:
<TextView
android:id="@+id/daily_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:gravity="center_vertical"/>
dailyactivity步骤:
- 绑定ID,设置按钮监听,创建列表,设置API
- 设置intent传值,页面跳转,intent传值跳转,拿到点击行的classifyId,使用putExtra传值(跳转至第三页面操作)
创建内部类DailyTask继承AsyncTask,在doInbackground进行http请求,创建URL,HttpURLConnection,InputStream,BufferedReader,创建temp,判断temp是否为空,不为空时,读取他的值temp
while ((temp=bufferedReader.readLine())!=null){ stringBuffer.append(temp); }
关闭bufferedReader,reader,inputStream
- 返回stringBuffer.toString()
- 在onPostExecute方法中,解析JSON,设置for循环输出多个数据
- 创建daily对象,导入列表
利用dailyadapter更新列表
dailyactivity代码展示:
public class DailyActivity extends AppCompatActivity { private ListView listView; private List<Daily> dailyList=new ArrayList<>(); private String api = "http://103.244.59.105:8014/paopaoserver/articles?params={\"page\":1,\"page_count\":10}"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_daily); listView = findViewById(R.id.daily_listview); new DailyTask().execute(); //设置列表按钮的点击事件 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Intent intent=new Intent(DailyActivity.this,TiaoliaoActivity.class); int classifyId=dailyList.get(position).getClassifyId();//拿到点击行的classifyId intent.putExtra("id",classifyId);//传值 startActivity(intent); } }); } class DailyTask extends AsyncTask<String, String, String> { StringBuffer stringBuffer=new StringBuffer(); @Override protected String doInBackground(String... strings) { try { URL url = new URL(api); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); InputStream inputStream = null; if (connection.getResponseCode() == 200) { inputStream = connection.getInputStream(); } else { return "network_failed";//网络连接失败 } InputStreamReader reader=new InputStreamReader(inputStream); BufferedReader bufferedReader=new BufferedReader(reader); String temp=""; while ((temp=bufferedReader.readLine())!=null){ stringBuffer.append(temp); } bufferedReader.close(); reader.close(); inputStream.close(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return stringBuffer.toString(); } @Override protected void onPostExecute(String s) { super.onPostExecute(s); try { JSONObject object=new JSONObject(s); JSONArray array=object.getJSONArray("datas"); for(int i=0;i<array.length();i++){ JSONObject obj=array.getJSONObject(i); //创建Daily对象 Daily daily=new Daily(obj.getInt("category_id"),obj.getInt("classify_id"),obj.getString("classify_name")); dailyList.add(daily);//导入列表 } //更新列表 DailyAdapter adapter=new DailyAdapter(DailyActivity.this,dailyList); listView.setAdapter(adapter); } catch (JSONException e) { e.printStackTrace(); } } } }
daily代码展示:
public class Daily {
private int catageoryId;
private int classifyId;
private String classifyName;
public Daily(int catageoryId, int classifyId, String classifyName) {
this.catageoryId = catageoryId;
this.classifyId = classifyId;
this.classifyName = classifyName;
}
public int getCatageoryId() {
return catageoryId;
}
public void setCatageoryId(int catageoryId) {
this.catageoryId = catageoryId;
}
public int getClassifyId() {
return classifyId;
}
public void setClassifyId(int classifyId) {
this.classifyId = classifyId;
}
public String getClassifyName() {
return classifyName;
}
public void setClassifyName(String classifyName) {
this.classifyName = classifyName;
}
}
dailyAdapter代码展示:
public class DailyAdapter extends BaseAdapter{
private Context context;
private List<Daily> dailyList;
public DailyAdapter(Context context, List<Daily> dailyList) {
this.context = context;
this.dailyList = dailyList;
}
@Override
public int getCount() {
return dailyList.size();
}
@Override
public Object getItem(int position) {
return dailyList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v=(LayoutInflater.from(context).inflate(R.layout.daily_item,null));
TextView tv=v.findViewById(R.id.daily_item);
tv.setText(dailyList.get(position).getClassifyName());
return v;
}
}
第三页面商品调料用品
视图页面tiaoliao_item代码展示:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="100dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/goods_item_iv"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_margin="10dp"
android:src="@mipmap/articles"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="80dp"
android:minHeight="100dp"
android:layout_gravity="center_vertical"
android:orientation="vertical"
android:layout_weight="4">
<TextView
android:id="@+id/goods_item_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="福临门食用调和油"/>
<TextView
android:id="@+id/goods_item_type"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="日常用品 调料干货"/>
<TextView
android:id="@+id/goods_item_price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="¥39.8"
android:layout_gravity="bottom"
android:textColor="@color/colorAccent"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2">
<Button
android:id="@+id/goods_item_buy_btn"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="购买"
android:textColor="#fff"
android:background="@color/colorAccent"
android:layout_marginRight="10dp"
android:layout_gravity="bottom"/>
</LinearLayout>
</LinearLayout>
视图页面activity_tiaoliao代码展示:
<ListView
android:id="@+id/tiaoliao_lv"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
tiaoactivity步骤:
- 绑定ID,创建列表,创建适配器,构造对应类目的API,用getExtea取第二页面传过来的值
- 创建内部模拟器MyTask继承AsyncTask
在doInbackground进行http请求,创建URL,HttpURLConnection,判断网页返回码是否为200正确,当不为200时,返回“network_failed”,正确时继续创建InputStream,BufferedReader,创建temp,判断temp是否为空,不为空时,读取他的值temp
while ((temp=bufferedReader.readLine())!=null){ stringBuffer.append(temp); }
关闭bufferedReader,reader,inputStream
- 返回stringBuffer.toString()
- 在onPostExecute方法中,判断在http请求中操作,当为返回“network_failed”时,告诉用户网络异常,否则继续解析JSON,设置for循环输出多个数据
- 创建tiaoliao对象,导入填充List列表
- 利用tiaoliaoadapter更新列表
tiaoliaoAdapter代码展示
public class tiaoliaoAdapter extends BaseAdapter{
private Context context;
private List<Tiaoliao> tiaoliaoList;
public tiaoliaoAdapter(Context context, List<Tiaoliao> tiaoliaoList) {
this.context = context;
this.tiaoliaoList = tiaoliaoList;
}
@Override
public int getCount() {
return tiaoliaoList.size();
}
@Override
public Object getItem(int position) {
return tiaoliaoList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view= LayoutInflater.from(context).inflate(R.layout.tiaoliao_item,null);
ImageView imageView=view.findViewById(R.id.goods_item_iv);
TextView titleTv=view.findViewById(R.id.goods_item_title);
TextView typtTv=view.findViewById(R.id.goods_item_type);
TextView priceTv=view.findViewById(R.id.goods_item_price);
Button buyBtn=view.findViewById(R.id.goods_item_buy_btn);
Tiaoliao tiaoliao=tiaoliaoList.get(position);
titleTv.setText(tiaoliao.getProductName());
typtTv.setText(tiaoliao.getCategoryName()+"-"+tiaoliao.getClassifyName());
priceTv.setText(tiaoliao.getNowPrice()+"");
String img_url="http://103.244.59.105:8014/paopaoserver"+tiaoliao.getSmallPic();
new ImgTask(imageView).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,img_url);
return view;
}
class ImgTask extends AsyncTask<String,String,String>{
private Bitmap bmp;
private ImageView imageView;
public ImgTask(ImageView imageView){
this.imageView=imageView;
}
@Override
protected String doInBackground(String... strings) {
try {
URL url=new URL(strings[0]);
HttpURLConnection connection= (HttpURLConnection) url.openConnection();
InputStream inputStream=null;
if((connection.getResponseCode()!=200)){
return "network_failed";
}
inputStream=connection.getInputStream();
bmp= BitmapFactory.decodeStream(inputStream);
} catch (MalformedURLException e) {
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
imageView.setImageBitmap(bmp);
}
}
}
tiaoliao代码展示
public class Tiaoliao {
private String categoryName;
private int cityId;
private int classifyId;
private String classifyName;
private int countiesId;
private double nowPrice;
private double oldPrice;
private int productId;
private String productName;
private String smallPic;
public Tiaoliao(String categoryName, int cityId, int classifyId, String classifyName, int countiesId, double nowPrice, double oldPrice, int productId, String productName, String smallPic) {
this.categoryName = categoryName;
this.cityId = cityId;
this.classifyId = classifyId;
this.classifyName = classifyName;
this.countiesId = countiesId;
this.nowPrice = nowPrice;
this.oldPrice = oldPrice;
this.productId = productId;
this.productName = productName;
this.smallPic = smallPic;
}
public String getCategoryName() {
return categoryName;
}
public void setCategoryName(String categoryName) {
this.categoryName = categoryName;
}
public int getCityId() {
return cityId;
}
public void setCityId(int cityId) {
this.cityId = cityId;
}
public int getClassifyId() {
return classifyId;
}
public void setClassifyId(int classifyId) {
this.classifyId = classifyId;
}
public String getClassifyName() {
return classifyName;
}
public void setClassifyName(String classifyName) {
this.classifyName = classifyName;
}
public int getCountiesId() {
return countiesId;
}
public void setCountiesId(int countiesId) {
this.countiesId = countiesId;
}
public double getNowPrice() {
return nowPrice;
}
public void setNowPrice(double nowPrice) {
this.nowPrice = nowPrice;
}
public double getOldPrice() {
return oldPrice;
}
public void setOldPrice(double oldPrice) {
this.oldPrice = oldPrice;
}
public int getProductId() {
return productId;
}
public void setProductId(int productId) {
this.productId = productId;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public String getSmallPic() {
return smallPic;
}
public void setSmallPic(String smallPic) {
this.smallPic = smallPic;
}
实现效果展示: