第一天-Kotlin 外卖APP

第一天项目进度

部分代码展示

MainActivity代码

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
//        判断设备是否有虚拟按键,如果有增加paddingottom = 50dp
        if (checkDeviceHasNavigationBar(this)){
            ll_main_activity.setPadding(0,0,0,50.dp2px())
        }

        initBottomBar()
        changIndex(0)
    }

    /**
     * 把转换功能添加到int类中作为扩展函数
     */
    fun Int.dp2px() :Int{
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,toFloat(),resources.displayMetrics).toInt()
    }

    //    获取是否存在NavigoationBar
    fun checkDeviceHasNavigationBar(context : Context) :Boolean{
    var  hasNavigationBar = false
    val rs = context.getResources()
    val id = rs.getIdentifier("config_showNavigationBar","bool","android")
    if (id > 0){
        hasNavigationBar = rs.getBoolean(id)
    }
    try {
        val systemPropertiesClass = Class.forName("android.os.SystemProperties")
        val m = systemPropertiesClass.getMethod("get",String::class.java)
        val navBarOverride = m.invoke(systemPropertiesClass,"qemu.hw.mainkeys") as String
        if ("1" == navBarOverride){
            hasNavigationBar = false
        }else if ("0" == navBarOverride){
            hasNavigationBar = true
        }
    }catch (e:Exception){

    }
    return hasNavigationBar
}

//    val  fragments : ArrayList<Fragment> = ArrayList()
    val  fragments : List<Fragment> = listOf<Fragment>(HomeFragment(),ShopFragment(),PeopleFragment(),MoreFragment())

    private fun initBottomBar() {
        for (i in 0 until main_bottombar.childCount){
           main_bottombar.getChildAt(i).setOnClickListener {
               view -> changIndex(i)
           }
        }
    }

    private fun changIndex(index: Int) {
        for (i in 0 until main_bottombar.childCount){
            val  child = main_bottombar.getChildAt(i)
            if (i == index){
                //选中,禁用效果
//                child.isEnabled = false
                setEnable(child,false)
            }else{
                //没选中  enable = true
//                child.isEnabled = true
                setEnable(child,true)
            }
        }
        supportFragmentManager.beginTransaction().replace(R.id.main_count,fragments.get(index)).commit()
    }

    private fun setEnable(child: View, isEnable: Boolean) {
        child.isEnabled = isEnable
        if (child is ViewGroup){
            for (i in 0 until child.childCount){
                child.getChildAt(i).isEnabled = isEnable
            }
        }
    }
}

HomeFragment首页

class HomeFragment : Fragment(){
    lateinit var homeRvAdapter: HomeAdapter
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val view = View.inflate(activity, R.layout.fragment_home, null)
        val rvHome = view.find<RecyclerView>(R.id.rv_home) //从上到 // 下的列表视图
        rvHome.layoutManager = LinearLayoutManager(activity)
        homeRvAdapter = HomeAdapter(activity)
        rvHome.adapter = homeRvAdapter
        return view
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        initData()
    }

    val datas : ArrayList<String> = ArrayList()
    private fun initData() {
        for (i in 0 until 100){
            datas.add("我是商家:"+i)
        }
        homeRvAdapter.setData(datas)
    }
}

首页Fragment适配器

class HomeAdapter(val context: FragmentActivity?) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    var mDatas : ArrayList<String> = ArrayList()

    fun setData(data:ArrayList<String>){
        this.mDatas = data
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(p0: ViewGroup, p1: Int): RecyclerView.ViewHolder {
        return HomeItemHolder(View.inflate(context, R.layout.item_home_common,null))
    }

    override fun getItemCount(): Int {
        return mDatas.size
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, postion: Int) {
        (holder as HomeItemHolder).bindData(mDatas[postion])
    }

    inner class HomeItemHolder(item : View) : RecyclerView.ViewHolder(item){
        var textView : TextView
        init {
            textView = item.findViewById(R.id.tv)
        }

        fun bindData(data :String){
            textView.text = data
        }
    }
}

布局文件

MainActivity布局文件

<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.MainActivity"
        android:orientation="vertical"
android:id="@+id/ll_main_activity">

    <FrameLayout android:layout_width="match_parent" android:layout_height="0dp"
    android:layout_weight="1"
    android:id="@+id/main_count">
    </FrameLayout>

    <LinearLayout android:layout_width="match_parent" android:layout_height="60dp"
    android:layout_weight="0"
    android:id="@+id/main_bottombar"
    android:orientation="horizontal">
    <FrameLayout android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent">
        <ImageView android:layout_width="match_parent"
                   android:layout_height="40dp"
                   android:src="@drawable/main_fangzi"/>
        <TextView android:layout_width="match_parent"
                  android:layout_height="25dp"
                  android:text="首页"
                  android:layout_gravity="bottom"
                  android:gravity="center"
                  android:textSize="18sp"
                  android:textColor="@color/main_textcolor"/>
    </FrameLayout>
    <FrameLayout android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent">
        <ImageView android:layout_width="match_parent"
                   android:layout_height="40dp"
                   android:src="@drawable/main_shop"/>
        <TextView android:layout_width="match_parent"
                  android:layout_height="25dp"
                  android:text="订单"
                  android:layout_gravity="bottom"
                  android:gravity="center"
                  android:textSize="18sp"
                  android:textColor="@color/main_textcolor"/>
    </FrameLayout>
    <FrameLayout android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent">
        <ImageView android:layout_width="match_parent"
                   android:layout_height="30dp"
                   android:src="@drawable/main_people"/>
        <TextView android:layout_width="match_parent"
                  android:layout_height="25dp"
                  android:text="我的"
                  android:layout_gravity="bottom"
                  android:gravity="center"
                  android:textSize="18sp"
                  android:textColor="@color/main_textcolor"/>
    </FrameLayout>
    <FrameLayout android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent">
        <ImageView android:layout_width="match_parent"
                   android:layout_height="30dp"
                   android:src="@drawable/main_gengduo"/>
        <TextView android:layout_width="match_parent"
                  android:layout_height="25dp"
                  android:text="更多"
                  android:layout_gravity="bottom"
                  android:gravity="center"
                  android:textSize="18sp"
                  android:textColor="@color/main_textcolor"/>
    </FrameLayout>
    </LinearLayout>

</LinearLayout>

首页Fragment布局文件

<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <LinearLayout
            android:id="@+id/top"
         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>

    <android.support.v7.widget.RecyclerView
            android:layout_below="@id/top"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/rv_home">
    </android.support.v7.widget.RecyclerView>

</RelativeLayout>

效果演示

在这里插入图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值