无法更新RecyclerView

时间:2018-07-02 13:04:59

标签: android android-recyclerview kotlin

我无法更新我的RecyclerView。在stackoverflow上尝试了所有解决方案,但没有帮助。我试图在代码中添加layoutmanager,增加项目的硬编码数量,制作新文件,检查是否有重复的文件等。除构造函数外,不调用适配器

适配器:

    class BaseAdapter(context: Context, val items: List<Item>) : 
    RecyclerView.Adapter<LineBoxAdapter.BaseAdapter>() {

    override fun getItemCount(): Int = items.size

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.textView.text = items[position].text
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): 
    ViewHolder {
        return ViewHolder(
            LayoutInflater.from(parent.context).inflate(
                R.layout.simple_item,
                parent,
                false
            )
        )
    }

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val textView = itemView.findViewById<TextView>(R.id.title)
    }
}

用于更新recyclerView的功能:

val listView =
  LayoutInflater.from(context).inflate(R.layout.list, viewGroup, 
  false)
  val list = listView.findViewById<RecyclerView>(R.id.
  LinearLayoutManager(context)
  val adapter = BaseAdapter(context, items)
  list.adapter = adapter
  adapter.notifyDataSetChanged()

RECYCLERVIEW:

<android.support.v7.widget.RecyclerView 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/items_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:overScrollMode="never"
android:requiresFadingEdge="horizontal"
app:layoutManager="android.support.v7.widget.LinearLayoutManager" />


SIMPLE_ITEM:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="false"
android:orientation="horizontal">

<LinearLayout xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    >

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:text="25" />
</LinearLayout>

1 个答案:

答案 0 :(得分:0)

您应该避免使用API​​中现有的类名。

我将发布我的工作代码。

simple_item:

plt3 = plt.subplot(gs[3, 0:3])
plt.title('Wave Graph')
plt.ylabel('wavelength [m]')
plt.xlabel('Altitude [km]')

WT = plt.contourf(Alt, Wavlgth, wave, 100, cmap=plt.cm.seismic, extend = 'both')
plt.xlim(xlim[:])
plt.ylim([np.min(period), np.max(period)])
ax = plt.gca().yaxis
plt.plot(time, coi, 'w')
plt3.fill_between(Alt, coi, np.max(coi), facecolor = 'white')

position2=fig.add_axes([.58, 0.03, 0.01, 0.2]) #Pos and size of colorbar
plt.colorbar(WT, cax=position2, orientation='vertical')

RecyclerAdapter:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/text_simple_item"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

我的活动:

class RecyclerAdapter(context: Context, val items: List<Item>) :
        RecyclerView.Adapter<RecyclerAdapter.MyViewHolder>() {

    override fun getItemCount(): Int = items.size

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        holder.textView.text = items[position].itemName
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int):
            MyViewHolder {
        return MyViewHolder(
                LayoutInflater.from(parent.context).inflate(
                        R.layout.simple_item,
                        parent,
                        false
                )
        )
    }

    class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val textView = itemView.findViewById<TextView>(R.id.text_simple_item)
    }
}

我的物品:

class RecyclerActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_recycler)

        val recyler = findViewById<RecyclerView>(R.id.rv_activity_recycler)
        val list = mutableListOf(Item("Me"), Item("You"), Item("We"))
        val adapter = RecyclerAdapter(this, list)
        recyler.layoutManager = LinearLayoutManager(this)
        recyler.adapter = adapter

    }
}