Kotlin 外卖 APP
首页布局代码
代码
class HomeFragment : Fragment(){
lateinit var homeRvAdapter: HomeAdapter
lateinit var rvHome :RecyclerView
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = View.inflate(activity as Context?, R.layout.fragment_home, null)
rvHome = view.find<RecyclerView>(R.id.rv_home) //从上到 // 下的列表视图
rvHome.layoutManager = LinearLayoutManager(activity)
homeRvAdapter = HomeAdapter(activity)
rvHome.adapter = homeRvAdapter
distance = 170.dp2px()
return view
}
fun Int.dp2px() :Int{
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,toFloat(),resources.displayMetrics).toInt()
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
initData()
}
val datas : ArrayList<String> = ArrayList()
var sum : Int = 0
var distance : Int = 0
var alpha = 55
private fun initData() {
datas.clear()
for (i in 0 until 20){
datas.add("我是商家:"+i)
}
homeRvAdapter.setData(datas)
//有数据可以滚动才可以监听滚动时间
rvHome.setOnScrollListener(object : RecyclerView.OnScrollListener(){
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
}
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
sum += dy
if (sum > distance){
alpha = 255
}else{
alpha = (sum / distance)
alpha += 55
}
ll_title_container.setBackgroundColor(Color.argb(alpha,0x31,0x90,0xe8))
}
})
}
}
布局
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rv_home">
</android.support.v7.widget.RecyclerView>
<LinearLayout
android:id="@+id/ll_title_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#553190E8"
android:orientation="horizontal"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/ll_title_search"
android:orientation="horizontal"
android:layout_marginTop="30dp">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:orientation="horizontal">
<TextView
android:id="@+id/home_tv_address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="5dip"
android:layout_weight="1"
android:singleLine="true"
android:text="昌平区北七家宏福创业园修正大厦"
android:textColor="#FFF"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginRight="5dip"
android:src="@drawable/home_address_drop_dowm"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:layout_marginBottom="12dp"
android:layout_marginRight="10dp"
android:layout_marginTop="12dp"
android:background="@drawable/home_search_text_background">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginRight="10dip"
android:layout_gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:layout_width="15sp"
android:layout_height="15sp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="4dp"
android:src="@drawable/home_search"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="输入商家、商品名称"
android:textColor="#FFF"
android:textSize="12sp"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
行布局
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:background="#fff">
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<com.makeramen.roundedimageview.RoundedImageView
app:riv_oval="true"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_margin="8dp"
android:src="@drawable/item_kfc"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/seller_logo"
android:layout_margin="8dp"
android:layout_gravity="right"
android:background="@drawable/circle_red"
android:gravity="center"
android:text="1"
android:textColor="#fff"
android:textSize="12sp"
android:visibility="gone"/>
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:background="@drawable/shape_background_solid"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:text="品牌"
android:textColor="@android:color/black"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:id="@+id/tv_title"
android:layout_weight="1"
android:singleLine="true"
android:text="肯德基宅急送(信息路店)"
android:textColor="#000"
android:textSize="22sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingLeft="2dp"
android:paddingRight="2dp"
android:text="票"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:gravity="center_vertical">
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/ratingBarStyleSmall"
android:id="@+id/ratingBar"
android:rating="2.8"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:text="2.8"
android:textColor="#FFDE3D"
android:textSize="12sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_home_sale"
android:text="月售243单"
android:textSize="12sp"/>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_home_send_price"
android:layout_alignParentLeft="true"
android:text="¥0起送/配送费 ¥9"
android:textSize="12sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_home_distance"
android:layout_alignParentRight="true"
android:text="556米/33分钟"
android:textSize="12sp"/>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_marginBottom="5dp"
android:layout_marginTop="10dp"
android:background="#FFFFFF"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:background="#FFFFFF">
<ImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_gravity="center_vertical"
android:background="@drawable/jian"
android:paddingLeft="2dp"
android:paddingRight="2dp"
android:text="减"
android:textColor="#fff"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="在线支付满49元减10元,满89元减16元"
android:textSize="12sp"
android:layout_marginLeft="5dp"
android:layout_gravity="center_vertical"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:gravity="center_vertical">
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center_vertical"
android:background="@drawable/zeng"
android:paddingLeft="5dp"
android:paddingRight="2dp"
android:text="赠"
android:textColor="#fff"/>
<TextView
android:layout_marginLeft="8dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="满45元赠九珍果汁饮料"
android:textSize="12sp"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
个人中心、登录界面
个人中心
class PeopleFragment : Fragment(){
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val peopleview = View.inflate(activity, R.layout.fragment_people, null)
val login = peopleview.find<ImageView>(R.id.image_login)
login.setOnClickListener{
val intent = Intent(activity, LoginActivity::class.java)
activity!!.startActivity(intent)
}
return peopleview
}
}
个人中心跳转
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#16A1E7">
<com.makeramen.roundedimageview.RoundedImageView
app:riv_oval="true"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_gravity="center_vertical"
android:layout_marginBottom="20dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="25dp"
android:src="@drawable/xxx"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:layout_weight="1.0"
android:layout_marginBottom="20dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="15dp"
android:layout_marginTop="20dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/image_login"
android:src="@drawable/login"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/ll_userinfo"
android:gravity="center"
android:visibility="invisible"
android:orientation="vertical"
android:layout_marginTop="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/username"
android:text="username"
android:textColor="#fff"
android:textSize="18sp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center_vertical">
</LinearLayout>
</LinearLayout>
</FrameLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
登录界面
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.activity.LoginActivity"
android:orientation="vertical"
android:background="#EDEDEB">
<RelativeLayout android:layout_width="match_parent" android:layout_height="0dp"
android:layout_weight="1.2"
android:orientation="horizontal"
android:background="#3090E6">
<ImageView android:layout_width="40dp" android:layout_height="40dp"
android:id="@+id/iv_goback" android:src="@drawable/goback"
android:layout_alignParentBottom="true" android:layout_marginLeft="20dp"/>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="登陆" android:layout_toRightOf="@id/iv_goback" android:id="@+id/iv_text_login"
android:textColor="#FFF" android:textSize="24sp" android:layout_alignParentBottom="true"
android:layout_marginBottom="5dp" android:layout_marginLeft="15dp"/>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="密码登陆" android:textSize="20sp" android:textColor="#FFF" android:layout_alignParentBottom="true"
android:layout_alignParentRight="true" android:layout_marginBottom="7dp"/>
</RelativeLayout>
<RelativeLayout android:layout_width="match_parent" android:layout_height="0dp"
android:layout_weight="8.5">
<EditText android:layout_width="280dp" android:layout_height="wrap_content"
android:id="@+id/iv_edit_phone" android:layout_marginTop="30dp"
android:layout_marginLeft="20dp" android:hint="请输入手机号码"/>
<Button android:layout_width="wrap_content" android:layout_height="45dp"
android:id="@+id/iv_btn_houqu" android:text="获取验证码" android:textColor="#FFF"
android:background="#3090E6" android:layout_toRightOf="@id/iv_edit_phone"
android:layout_marginTop="20dp" android:layout_marginLeft="10dp"
/>
<EditText android:layout_width="100dp" android:layout_height="wrap_content"
android:id="@+id/iv_edit_number" android:hint="验证码" android:layout_below="@id/iv_edit_phone"
android:layout_marginLeft="20dp" android:layout_marginTop="10dp"/>
<Button android:layout_width="match_parent" android:layout_height="wrap_content"
android:id="@+id/iv_btn_login" android:background="#33C937" android:text="登陆"
android:layout_below="@id/iv_edit_number" android:layout_marginTop="50dp"
android:textColor="#FFF" android:textSize="20sp"/>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:id="@+id/texeView" android:text="第三方登录"
android:layout_below="@id/iv_btn_login" android:layout_marginTop="100dp"
android:textSize="20sp" android:layout_centerHorizontal="true"/>
<LinearLayout android:layout_width="360dp" android:layout_height="wrap_content"
android:orientation="horizontal" android:layout_below="@id/texeView" android:id="@+id/iv_lin_image"
android:layout_marginTop="30dp" android:layout_centerHorizontal="true">
<ImageView android:layout_width="0dp" android:layout_weight="1" android:layout_height="50dp"
android:src="@mipmap/weixin" android:id="@+id/iv_people_weixin" />
<ImageView android:layout_width="0dp" android:layout_weight="1" android:layout_height="50dp"
android:src="@mipmap/qq" android:id="@+id/iv_people_qq"/>
<ImageView android:layout_width="0dp" android:layout_weight="1" android:layout_height="50dp"
android:src="@mipmap/weibo" android:id="@+id/iv_people_weibo"/>
<ImageView android:layout_width="0dp" android:layout_weight="1" android:layout_height="50dp"
android:src="@mipmap/taobao" android:id="@+id/iv_people_taobao"/>
</LinearLayout>
<LinearLayout android:layout_width="360dp" android:layout_height="wrap_content"
android:orientation="horizontal" android:layout_below="@id/iv_lin_image"
android:layout_marginTop="5dp" android:layout_centerHorizontal="true">
<TextView android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content"
android:text="微信" android:textSize="20sp" android:gravity="center"/>
<TextView android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content"
android:text="QQ" android:textSize="20sp" android:gravity="center"/>
<TextView android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content"
android:text="微博" android:textSize="20sp" android:gravity="center"/>
<TextView android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content"
android:text="淘宝" android:textSize="20sp" android:gravity="center"/>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
效果展示