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定位。
|