如何使Recycler View的一部分变为粗体?

时间:2018-09-17 10:44:58

标签: android xml android-recyclerview kotlin

我有一个EditText和一个recyclerview。我在“ recyclerview”中添加了一个“列表”,现在我想使“ recycleview”的第一个字母变为粗体。但是我只想使在我的editText中写的字母为粗体,所以我不能仅在xml中定义它。我怎样才能使我的列表以粗体显示,例如

Bo ld1

Bo ld2

这是我的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Abfahrtsmonitor">

<EditText
    android:id="@+id/editText"
    android:layout_width="360dp"
    android:layout_height="47dp"
    android:ems="10"
    android:hint="Suche"
    android:inputType="textPersonName"
    tools:layout_editor_absoluteX="16dp"
    tools:layout_editor_absoluteY="7dp" />

<android.support.v7.widget.RecyclerView
    android:id="@+id/Recycleview"
    android:layout_width="370dp"
    android:layout_height="494dp"
    android:layout_marginEnd="4dp"
    android:layout_marginTop="8dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/editText" />
</android.support.constraint.ConstraintLayout>

这是我的kotlin代码的一部分:

class Abfahrtsmonitor : AppCompatActivity() {
 override fun onCreate(savedInstanceState: Bundle?) {
 super.onCreate(savedInstanceState)
    setContentView(R.layout.abfahrtsmonitor)
    Recycleview.layoutManager = LinearLayoutManager(this)
HaltestellenEingabe_dummy.add(helpname)
Recycleview.adapter = listadapter(HaltestellenEingabe_dummy, this)
  var textview = findViewById<TextView>(R.id.editText)
    textview.addTextChangedListener(object : TextWatcher {

        override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
        }
        override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
        }
        override fun afterTextChanged(s: Editable) {
            println("1")
            HaltestellenEingabe_dummy.clear()
            for(i in 0..Haltestellen.size-1){
                var inttext=0
                var inteingabe=0
                if(textview.text.toString()==Haltestellen[i].subSequence(0,textview.length()).toString()){     //If the same letters are used
                    HaltestellenEingabe_dummy.add(Haltestellen[i]) // Here I would like to define the layout of my Segments.
                }
            }
            Recycleview.adapter.notifyDataSetChanged()
        }
    })

这里是ListAdapter:

class listadapter(val items : ArrayList<String>, val context: Context) : RecyclerView.Adapter<ViewHolder>() {

// Gets the number of animals in the list
override fun getItemCount(): Int {
    return items.size
}

// Inflates the item views
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
    return     ViewHolder(LayoutInflater.from(context).inflate(R.layout.listviewfile, parent, false))
}

// Binds each animal in the ArrayList to a view
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    holder?.haltestellenliste?.text = items.get(position)
}
}
class ViewHolder (view: View) : RecyclerView.ViewHolder(view) {
// Holds the TextView that will add each animal to
val haltestellenliste = view.aufzaehlunghaltestellen
}

1 个答案:

答案 0 :(得分:1)

onBindViewHolder中设置文本的位置,可以使用SpannableStringBuilder来加粗第一个字母:

holder?.haltestellenliste?.text = SpannableStringBuilder(items[position]).apply{
    setSpan(StyleSpan(Typeface.BOLD), 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
}