问题描述
我在lang(java)中的onBindViewHolder中写了一些代码行.我试图在Binding Adapter中用kotlin编写确切的代码,但我无法写出
I have some lines of code written in onBindViewHolder in lang (java) .. I'm trying to write the exact code in kotlin in Binding Adapter , but i'm not able to write that
EarthQuakeAdapter(JAVA) ..我尝试了一下,但是我无法编写相同的代码来从onBindViewHolder(JAVA lang)方法中将circleCircle,PrimaryLocation和LocationOffSet获取到我发布的kotlin中的Binding Adapter中.下方
EarthQuakeAdapter( JAVA) .. I tried but i was not able to write the same code to get magnitudeCircle , PrimaryLocation and LocationOffSet from onBindViewHolder(JAVA lang) method to Binding Adapter in kotlin which i posted below
public class EarthquakeAdapter extends RecyclerView.Adapter<EarthquakeAdapter.MyViewHolder> {
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int i) {
Properties properties = this.mData.get(i).getProperties();
double earthquakeMagnitude = properties.getMag();
DecimalFormat decimalFormat = new DecimalFormat("0.0");
String formattedMagnitude = decimalFormat.format(earthquakeMagnitude);
holder.tvMagnitude.setText(formattedMagnitude);
// Set the proper background color on the magnitude circle.
// Fetch the background from the TextView, which is a GradientDrawable.
holder.magnitudeCircle = (GradientDrawable) holder.tvMagnitude.getBackground();
//Get the appropriate background color based on the current earthquake magnitude
int magnitudecolor = getMagnitudeColor(properties.getMag());
//Set the color on the magnitude circle
holder.magnitudeCircle.setColor(magnitudecolor);
//Get the original location string from the earthquake object ,
// which can be in the format of "5km N of Cairo, Egypt" or "Pacific-Antartic Ridge' .
//If the original location String (i.e, 5km of Cairo , Egypt ) contains
//a primary location (Cario, Egypt) and a location offset (5km of that city )
//then store the primary location separately from the location offset in 2 strings,
//so they can be displayed in two TextViews.
String originalLocation = properties.getPlace();
//Check whether the originalLocation string contain the "of" text
if (originalLocation.contains("of")) {
//Split the string into different parts (as an array of strings)
//based on the "of" text. We expect an array of two strings, where
// the first string will , be " 5km N" and the second string will be 'Cairo Egypt"
String[] parts = originalLocation.split("of");
//location offset should be "5km N" + " of " ---> "5km N of'
//Primary location should be "Cairo Egypt"
holder.tvLocationOffSet.setText(parts[0] + "of");
holder.tvPrimaryLocation.setText(parts[1]);
} else {
//otherwise , there is no " of " text in the originalLocation string .
//Hence, the default location offset to say " Near the"
holder.tvLocationOffSet.setText("Near The");
holder.tvPrimaryLocation.setText(originalLocation);
}
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
TextView tvMagnitude, tvLocationOffSet, tvPrimaryLocation,
GradientDrawable magnitudeCircle;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
tvMagnitude = itemView.findViewById(R.id.magnitude);
tvLocationOffSet = itemView.findViewById(R.id.location_offset);
tvPrimaryLocation = itemView.findViewById(R.id.primary_location);
}
}
private int getMagnitudeColor(double magnitude) {
int magnitudeColorResourceId;
int magnitudeFloor = (int) Math.floor(magnitude);
switch (magnitudeFloor) {
case 0:
case 1:
magnitudeColorResourceId = R.color.magnitude1;
break;
case 2:
magnitudeColorResourceId = R.color.magnitude2;
break;
case 3:
magnitudeColorResourceId = R.color.magnitude3;
break;
default:
magnitudeColorResourceId = R.color.magnitude10plus;
break;
}
return ContextCompat.getColor(mContext, magnitudeColorResourceId);
}
}
BindingAdapter.kt(KOTLIN)
@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)
// if (titlePlace.contains("of")) {
// @BindingAdapter("placeone")
// fun bindPlaceOne(textView: TextView, titlePlace: String) {
// var parts = titlePlace.split("of").toString()
// textView.setText(parts[0] + "of")
//
// @BindingAdapter("placetwo")
// fun bindPlaceTwo(textView: TextView, titlePlace: String) {
// textView.setText(parts[1].toString())
// }
// }
//
// } else {
// @BindingAdapter("placeone")
// fun bindPlaceOne(textView: TextView, titlePlace: String) {
//
// textView.setText( "NEAR THE") }
//
// @BindingAdapter("placetwo")
// fun bindPlaceTwo(textView: TextView, titlePlace: String) {
// textView.setText(titlePlace)
// }
// }
}
@BindingAdapter("time")
fun bindTime(textView: TextView, titleTime :Long) {
// SimpleDateFormat timeFormat = new SimpleDateFormat("h:mm a");
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())
}
RecyclerviewAdapter.kt
class RecyclerviewAdapter() : ListAdapter<Feature, RecyclerviewAdapter.EarthquakePropertyViewHolder>(DiffCallback()) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): EarthquakePropertyViewHolder {
return EarthquakePropertyViewHolder.from(parent)
}
override fun onBindViewHolder(holder: EarthquakePropertyViewHolder, position: Int) {
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
}
}
Earthquake_raw.xml
<layout
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" >
<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" />
<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>
请帮助我,我还是Kotlin编程的新手.
Please help me, I'm still new to Kotlin programming.
推荐答案
基本上,规则是反转语法.如果您在kotlin中具有String originalLocation = properties.getPlace();
,则该名称将为var/val originalLocation: String = properties.getPlace()
. Var代表要更改的变量,而val代表不变的值.您可以绕过定义对象的类型,因为kotlin足够聪明,可以知道不同类型的初始化属性. void
是fun
,您无需指定它是否公开,因为默认情况下它是公开的,除非您另有说明. class
相同,并且函数中的变量被反向声明,因此(@NonNull MyViewHolder holder, int i)
将是(holder: MyViewHolder, i: Int)
.
Basically the rule is to reverse syntax. If you have String originalLocation = properties.getPlace();
in kotlin that would be var/val originalLocation: String = properties.getPlace()
. Var stands for variable that changes and val is for unchangeable values. You can bypass defining object's type because kotlin is smart enough to know the different types of initialized properties. void
is fun
and you don't need to specify if it's public because it is by default unless you say otherwise. class
is the same in both and variables in functions are declared in reverse so (@NonNull MyViewHolder holder, int i)
would be (holder: MyViewHolder, i: Int)
.
当然,Android Studio具有这个宏伟的选项,当您将Java代码复制到kotlin文件中时,它将询问您是否要将其更改为kotlin.
Of course Android Studio has this magnificent option that, when you copy java code into kotlin file, it will ask you if you want it changed to kotlin.
这篇关于如何在绑定适配器中将此Java代码写入kotlin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!