大佬教程收集整理的这篇文章主要介绍了如何在设置片段中添加共享首选项并使用导航组件在 android kotlin 中的片段之间移动,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我不知道如何在设置片段中添加共享首选项,也不知道如何使用 androID 导航组件在片段之间移动,用户可以在其中更改 API 键的“值”。这是我使用的 https://earthquake.usgs.gov/fdsnws/event/1/query?format=geoJson&orderby=time&minmag=5&limit=10
API。从 API 中,我想更改 @H_760_3@minmag 值,用户可以在设置片段中放置 @H_760_3@minmag 的任何值,结果应保存在共享首选项中,并且结果应显示在主活动中{ {3}}
我正在使用 kotlin,改造并遵循 androID 架构组件
EarthquakeAPIservice.kt
private const val BASE_URL = "https://earthquake.usgs.gov/fdsnws/" // https://earthquake.usgs.gov/fdsnws/event/1/query?format=geoJson&orderby=time&minmag=5&limit=10 private val moshi = moshi.builder() .add(KotlinjsonAdapterFactory()) .build() private val retrofit = Retrofit.builder() .addConverterFactory(moshiConverterFactory.create(moshi)) .baseUrl(BASE_URL) .build() interface EarthquakeAPIservice { @GET("event/1/query?") suspend fun getJson (@query("minmag") minMagnitude : String,@query("format") formatted : String,@query("limit") limited: String,@query("orderby") orderBy : String) : EarthquakeResponse } object BookAPI { val retrofitservice : EarthquakeAPIservice by lazy { retrofit.create(EarthquakeAPIservice::class.java) } }
@H_403_19@地震响应.kt
data class EarthquakeResponse( val features : Array<Feature> ?) data class Feature( val propertIEs : PropertIEs) data class PropertIEs(val mag : Double,val place : String,val time : Long)
@H_403_19@概览片段
class OvervIEwFragment : Fragment() { private val viewmodel: OvervIEwviewmodel by lazy { viewmodelProvIDer(this).get(OvervIEwviewmodel::class.java) } overrIDe fun onCreateVIEw( inflater: LayoutInflater,container: VIEwGroup?,savedInstanceState: Bundle?): VIEw? { val binding: FragmentOvervIEwBinding = DataBindingUtil.inflate( inflater,R.layout.fragment_overvIEw,container,falsE) binding.viewmodel = viewmodel binding.recycleList.adapter = RecyclervIEwAdapter() binding.lifecycleowner = this setHasOptionsMenu(true) return binding.root } overrIDe fun onCreateOptionsMenu(menu: Menu,inflater: MenuInflater) { super.onCreateOptionsMenu(menu,inflater) inflater?.inflate(R.menu.overflow_menu,menu) } overrIDe fun onoptionsItemSELEcted(item: MenuItem): Boolean { return NavigationUI.onNavDesTinationSELEcted(item,requireVIEw().findNavController()) || super.onoptionsItemSELEcted(item) } }
@H_403_19@概览视图模型
class OvervIEwviewmodel : viewmodel() { lateinit var minMagnitude : String lateinit var orderBy: String // Internally,we use a mutablelivedata,because we will be updating the List of MarsProperty // with new values private val _propertIEs = mutablelivedata<EarthquakeResponse> () // The external liveData interface to the property is immutable,so only this class can modify val propertIEs: liveData<EarthquakeResponse> get() = _propertIEs init { getBookPropertIEs() } private fun getBookPropertIEs() { viewmodelScope.launch { try { _propertIEs.value = BookAPI.retrofitservice.getJson("2","geoJson","10","time") } catch (e : Exception) { } } } }
@H_403_19@RecyclervIEwAdapter.kt
class RecyclervIEwAdapter() : listadapter<Feature,RecyclervIEwAdapter.EarthquakePropertyVIEwHolder>(DiffCallBACk()) { overrIDe fun onCreateVIEwHolder(parent: VIEwGroup,vIEwType: @R_489_10185@: EarthquakePropertyVIEwHolder { return EarthquakePropertyVIEwHolder.from(parent) } overrIDe fun onBindVIEwHolder(holder: EarthquakePropertyVIEwHolder,position: @R_489_10185@ { val item = getItem(position) holder.bind(item) } class EarthquakePropertyVIEwHolder private constructor(val binding: EarthquakeRawBinding) : RecyclerVIEw.VIEwHolder(binding.root) { fun bind(books: FeaturE) { binding.property = books binding.executePendingBindings() } companion object { fun from(parent: VIEwGroup): EarthquakePropertyVIEwHolder { val layoutInflater = LayoutInflater.from(parent.context) val vIEw = EarthquakeRawBinding.inflate(layoutInflater,parent,falsE) return EarthquakePropertyVIEwHolder(vIEw) } } } } class DiffCallBACk : DiffUtil.ItemCallBACk<Feature>() { overrIDe fun areItemsTheSame(oldItem: Feature,newItem: FeaturE): Boolean { return oldItem === newItem } overrIDe fun areContentsTheSame(oldItem: Feature,newItem: FeaturE): Boolean { return oldItem == newItem } }
@H_403_19@BindingAdapter.kt
@BindingAdapter("ListData") fun bindRecyclerVIEw(recyclerVIEw: RecyclerVIEw,data: EarthquakeResponse?) { val adapter = recyclerVIEw.adapter as RecyclervIEwAdapter if (data != null) { adapter.submitList(data.features?.toList()) } } @BindingAdapter("magnitude") fun bindAuthor(textVIEw: TextVIEw,magname : DoublE) { val decimalFormat = decimalFormat("0.0") val formattedMagnitude = decimalFormat.format(magName) textVIEw.setText(formattedMagnitude.toString()) } @BindingAdapter("place") fun bindtitle(textVIEw: TextVIEw,titlePlace : String) { textVIEw.setText(titlePlacE) } @BindingAdapter("time") fun bindTime(textVIEw: TextVIEw,titleTime :Long) { val timeFormat = SimpleDateFormat("h:mm a") val formattedTime = timeFormat.format(titleTimE) textVIEw.setText(formattedTime.toString()) } @BindingAdapter("date") fun bindDate(textVIEw: TextVIEw,titleTime :Long) { val timeFormat = SimpleDateFormat("LLL dd,yyyy") val formattedDate = timeFormat.format(titleTimE) textVIEw.setText(formattedDate.toString()) }
@H_403_19@主活动
class MainActivity : AppCompatActivity() { private lateinit var navController: NavController overrIDe fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceStatE) setContentVIEw(R.layout.activity_main) } }
@H_403_19@activity_main.xml
<relativeLayout androID:layout_wIDth="match_parent" androID:layout_height="match_parent" tools:context="com.example.kotlinearthquake.MainActivity" xmlns:tools="http://scheR_676_11845@as.androID.com/tools" xmlns:androID="http://scheR_676_11845@as.androID.com/apk/res/androID"> <fragment xmlns:app="http://scheR_676_11845@as.androID.com/apk/res-auto" androID:ID="@+ID/myNavHostFragment" androID:name="androIDx.navigation.fragment.NavHostFragment" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" app:defaultNavHost="true" app:navGraph="@navigation/nav_graph" tools:ignore="MissingClass" /> </relativeLayout>
@H_403_19@earthquake_raw.xml
<layout xmlns:androID="http://scheR_676_11845@as.androID.com/apk/res/androID" xmlns:tools="http://scheR_676_11845@as.androID.com/tools" xmlns:app="http://scheR_676_11845@as.androID.com/apk/res-auto" > <data> <variable name="property" type="com.example.kotlinearthquake.network.Feature" /> </data> <linearLayout androID:ID="@+ID/container" androID:layout_wIDth="match_parent" androID:layout_height="150dp" androID:orIEntation="horizontal" androID:paddingStart="16dp" androID:paddingleft="16dp" androID:paddingend="16dp" androID:paddingRight="16dp"> <TextVIEw androID:ID="@+ID/magnitude" androID:layout_wIDth="36dp" androID:layout_height="36dp" androID:layout_gravity="center_vertical" androID:BACkground="@drawable/magnitude_circle" androID:FontFamily="sans-serif-medium" androID:gravity="center" androID:textcolor="@androID:color/black" androID:textSize="16sp" app:magnitude="@{property.propertIEs.mag}" tools:text="8.9" /> <linearLayout androID:layout_wIDth="0dp" androID:layout_height="wrap_content" androID:layout_gravity="center_vertical" androID:layout_marginStart="16dp" androID:layout_marginleft="16dp" androID:layout_weight="1" androID:orIEntation="vertical"> <TextVIEw androID:ID="@+ID/LOCATIOn_offset" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:ellipsize="end" androID:FontFamily="sans-serif-medium" androID:maxlines="1" androID:textAllCaps="true" androID:textcolor="@androID:color/black" androID:textSize="12sp" tools:text="30km S of\n" /> <TextVIEw androID:ID="@+ID/priMary_LOCATIOn" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:ellipsize="end" androID:maxlines="2" androID:textcolor="@androID:color/black" androID:textSize="12sp" app:place="@{property.propertIEs.placE}" tools:text="Long placeholder that should wrap to more than 2 line of text" /> </linearLayout> <linearLayout androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_gravity="center_vertical" androID:layout_marginStart="16dp" androID:layout_marginleft="16dp" androID:orIEntation="vertical"> <TextVIEw androID:ID="@+ID/date" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_gravity="end" androID:textcolor="@color/textcolorEarthquakeDetails" androID:textSize="12sp" app:date="@{property.propertIEs.timE}" tools:text="Mar 6,2010" /> <TextVIEw androID:ID="@+ID/time" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_gravity="end" androID:textcolor="@color/textcolorEarthquakeDetails" androID:textSize="12sp" app:time="@{property.propertIEs.timE}" tools:text="3:00 PM" /> </linearLayout> </linearLayout> </layout>
@H_403_19@fragment_overvIEw.xml
<layout xmlns:androID="http://scheR_676_11845@as.androID.com/apk/res/androID" xmlns:tools="http://scheR_676_11845@as.androID.com/tools" xmlns:app="http://scheR_676_11845@as.androID.com/apk/res-auto"> <data> <variable name="viewmodel" type="com.example.kotlinearthquake.overvIEw.overvIEwviewmodel" /> </data> <relativeLayout androID:layout_wIDth="match_parent" androID:layout_height="match_parent" tools:context = "com.example.kotlinearthquake.MainActivity" > <androIDx.recyclervIEw.Widget.RecyclerVIEw androID:ID="@+ID/recycle_List" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:orIEntation="vertical" app:ListData="@{viewmodel.propertIEs}" tools:Listitem="@layout/earthquake_raw" app:layoutManager="androIDx.recyclervIEw.Widget.linearlayoutmanager"/> <TextVIEw androID:ID="@+ID/empty_vIEw" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_centerInParent="true" androID:textAppearance="?androID:textAppearanceMedium"/> <Progressbar androID:ID="@+ID/loading_inDicator" style="@style/Widget.AppCompat.Progressbar" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_centerInParent="true"/> </relativeLayout> </layout>
@H_403_19@我尝试过,但没有找到解决我的问题的任何好的答案..我从过去 1 个月开始就陷入困境,请帮助我,我还是 androID 和 Kotlin 编程的新手。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)
以上是大佬教程为你收集整理的如何在设置片段中添加共享首选项并使用导航组件在 android kotlin 中的片段之间移动全部内容,希望文章能够帮你解决如何在设置片段中添加共享首选项并使用导航组件在 android kotlin 中的片段之间移动所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。