近段时间的学习碎片整理(9)

1、viewpager适配器的写法

list可以是要传入fragment的参数

class ViewPagerAdapter(fm: FragmentManager,private val list: Array<String>):
    FragmentPagerAdapter(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT){
    override fun getCount(): Int = 4

    override fun getItem(position: Int) :Fragment{
        return PracticePageFragment.newInstance(list[position])
    }

    override fun getPageTitle(position: Int): CharSequence? {
        return list[position]
    }

}

在fragment里面的companion object

companion object {
        const val FRAGMENT_PRACTICE_TYPE = "FRAGMENT_PRACTICE_TYPE"
        fun newInstance(type:String): PracticePageFragment{
            val bundle = Bundle()
            bundle.putString(FRAGMENT_PRACTICE_TYPE, type)
            val practicePageFragment = PracticePageFragment()
            practicePageFragment.arguments = bundle
            return  practicePageFragment
        }
    }

2、犯了一个很笨的错误,在使用androidpicker时候,不断的初始化这个控件,事实上只需要初始化一次就行

        

private var pvTime: TimePickerView? = null
 private fun initTimePicker() {
            pvTime = TimePickerBuilder(mContext) {
                date, _ ->
            currentDate = date.time
            val dateToString = Utils.dateToString(date)
            binding.tvCalendar.text = dateToString
            getNewData(dateToString)
        }.build()
    }

可以通过setdate来设置打开时候的默认选项

      val stringToDate = Utils.stringToDate(formatLongMonthDayString2)
            val cal = Calendar.getInstance()
            cal.time = stringToDate
            pvTime?.setDate(cal)

3、如何获取手机网络在公网上的ip(参考Android 获取外网IP,实测有效 - 飞剑 - 博客园

        

 /**
     * 获取外网的IP(要访问Url,要放到后台线程里处理)
     *
     * @param @return
     * @return String
     * @throws
     * @Title: GetNetIp
     * @Description:
     */
    fun getNetIp(): String? {
        var infoUrl: URL? = null
        var inStream: InputStream? = null
        var ipLine = ""
        var httpConnection: HttpURLConnection? = null
        try {
//            infoUrl = new URL("http://ip168.com/");
            infoUrl = URL("http://pv.sohu.com/cityjson?ie=utf-8")
            val connection: URLConnection = infoUrl.openConnection()
            httpConnection = connection as HttpURLConnection
            val responseCode: Int = httpConnection.getResponseCode()
            if (responseCode == HttpURLConnection.HTTP_OK) {
                inStream = httpConnection.getInputStream()
                val reader = BufferedReader(
                    InputStreamReader(inStream, "utf-8")
                )
                val strber = StringBuilder()
                var line: String? = null
                while (reader.readLine().also { line = it } != null) {
                    strber.append(
                        """
                        $line
                        
                        """.trimIndent()
                    )
                }
                val pattern: Pattern = Pattern
                    .compile("((?:(?:25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d)))\\.){3}(?:25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d))))")
                val matcher: Matcher = pattern.matcher(strber.toString())
                if (matcher.find()) {
                    ipLine = matcher.group()
                }
            }
        } catch (e: MalformedURLException) {
            e.printStackTrace()
        } catch (e: IOException) {
            e.printStackTrace()
        } finally {
            try {
                inStream?.close()
                httpConnection?.disconnect()
            } catch (e: IOException) {
                e.printStackTrace()
            } catch (ex: Exception) {
                ex.printStackTrace()
            }
        }
        Timber.d("getNetIp = $ipLine")
        return ipLine
    }

4、踩坑:高德ip定位现在只在WiFi状态下才能获取到定位信息,在流量状态下是获取不到的,所以需要使用高德ip定位2.0,先获取网络在公网上的ip然后再去进行ip定位。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值