Lisview inisde recyclerview仅显示第一个元素

时间:2019-04-16 11:50:20

标签: android listview kotlin

对不起,当我尝试将数据加载到Recyclerview内的Listview中时,我遇到了一些问题。

我获得了数据,并且已经将它们传递到Listview中,但是它只是显示了结果数据的第一个元素。

这是我的recyclerview的CostumViewholder

@Suppress("PLUGIN_WARNING")
class PengaduanViewHolder(val view: View, var aduan: Pengaduan? = null) : 
RecyclerView.ViewHolder(view), KomentarView {

private lateinit var presenter: KomentarPresenter
private var komentarList: MutableList<KomentarModel> = mutableListOf()
private lateinit var adapter: MyListAdapter

@RequiresApi(Build.VERSION_CODES.O)
@SuppressLint("SetTextI18n")
fun bindItem(aduan: Pengaduan) {
    val generator = ColorGenerator.MATERIAL
    Glide.with(view.context.applicationContext)
        .load(BuildConfig.BASE_URL + "assets/lapor/image/${aduan.lapor_image_1}")
        .into(view.iv_aduan_gambar)
    val jam = aduan.lapor_jam
    val tanggal = aduan.lapor_tanggal?.toDate()?.formatTo("EEEE, dd MMMM yyyy")
    view.tv_aduan_waktu.text = "$tanggal - $jam"
    view.tv_aduan_status.text = aduan.lapor_proses
    view.tv_aduan_lokasi.text = aduan.lapor_lokasi
    view.tv_aduan_lokasi.setOnClickListener {
        val browserIntent = Intent(Intent.ACTION_VIEW)
        browserIntent.data = Uri.parse("https://maps.google.com/?q=${aduan.lapor_lat},${aduan.lapor_long}")
        view.context.startActivity(browserIntent)
    }
    view.tv_aduan_deskripsi.text = aduan.lapor_deskripsi

    aduan.warga_namadepan?.let {
        it == "Unknown"
    }
    val letter = aduan.warga_namadepan?.substring(0, 1)
    Log.d("NAMA", letter)

    val color = generator.getColor(aduan.warga_id)
    var drawable: TextDrawable? = null
    val laporAnonim: String? = aduan.lapor_anonim
    when (laporAnonim) {
        null -> {
            drawable = TextDrawable.builder().buildRound("A", R.color.divider)
            view.tv_aduan_warga_nama.text = aduan.warga_namadepan
        }
        "Tidak" -> {
            drawable = TextDrawable.builder().buildRound(letter, color)
            view.tv_aduan_warga_nama.text = "${aduan.warga_namadepan} ${aduan.warga_namabelakang}"
        }
        "Ya" -> {
            drawable = TextDrawable.builder().buildRound("A", R.color.divider)
            view.tv_aduan_warga_nama.text = "Anonim"
        }
    }
    view.aduan_profil_image.setImageDrawable(drawable)
}

companion object {
    const val EXTRA_ADUAN_DETAIL = "EXTRA_ADUAN_DETAIL"
}

init {
    view.view_click.setOnClickListener {
        val intent = Intent(view.context, AduanDetailActivity::class.java)
        intent.putExtra(EXTRA_ADUAN_DETAIL, aduan)
        view.context.startActivity(intent)
    }

    view.ic_expand_less.setOnClickListener {

        view.progress_horizontal.visibility = View.VISIBLE
        view.ic_expand_less.visibility = View.GONE
        view.ic_expand_more.visibility = View.VISIBLE
        view.showComent.visibility = View.GONE
        view.hideComent.visibility = View.VISIBLE
        view.commentList.visibility = View.VISIBLE

        presenter = KomentarPresenter(this)
        val token = SharedPreferenceHelper(view.context).deviceToken

        Log.d("PENGADUAN_ADAPTER", token)
        adapter = MyListAdapter(view.context, komentarList)
        view.commentList.adapter = adapter
        adapter.notifyDataSetChanged()

        if (komentarList.size >= 1) {
            view.progress_horizontal.visibility = View.GONE
        } else {
            presenter.getKomentar(token, aduan?.lapor_id!!)
        }


    }
    view.ic_expand_more.setOnClickListener {
        view.progress_horizontal.visibility = View.GONE
        view.ic_expand_less.visibility = View.VISIBLE
        view.ic_expand_more.visibility = View.GONE
        view.showComent.visibility = View.VISIBLE
        view.hideComent.visibility = View.GONE
        view.commentList.visibility = View.GONE


    }
}


override fun showData(response: KomentarResponse) {
    super.showData(response)
    if (response.success!!) {
        view.progress_horizontal.visibility = View.GONE
        komentarList.clear()
        komentarList.addAll(response.data!!)
        adapter.notifyDataSetChanged()
    }
}
}

这是我的ListAdapter外观

class MyListAdapter(private var context: Context?, private var item: List<KomentarModel>) : BaseAdapter() {


@SuppressLint("InflateParams")
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
    var crView = convertView
    val holder: ComentViewHolder
    if (crView == null) {
        holder = ComentViewHolder()
        val inflater = context?.getSystemService((Context.LAYOUT_INFLATER_SERVICE)) as LayoutInflater
        crView = inflater.inflate(R.layout.row_comment,null)
        crView.setPadding(0,10,0,10)
        holder.nama = crView?.findViewById(R.id.tv_nama_komentar) as TextView
        holder.isi = crView.findViewById(R.id.tv_isi_komentar) as TextView
        holder.tgl = crView.findViewById(R.id.tv_tgl_komentar) as TextView
        crView.tag = holder
    } else {
        holder = convertView?.tag as ComentViewHolder
    }
    val commnt = item[position]
    holder.nama?.text = commnt.nama
    holder.isi?.text = commnt.komentar
    holder.tgl?.text = commnt.komentar_waktu
    return crView

}

override fun getItem(position: Int): Any {
    return item[position]
}

override fun getItemId(position: Int): Long {
    return position.toLong()
}

override fun getCount(): Int {
    return item.size
}


private inner class ComentViewHolder {
    var nama: TextView? = null
    var isi: TextView? = null
    var tgl: TextView? = null


}


}

recyclerview_lis.xml

<?xml version="1.0" encoding="utf-8"?>

<androidx.cardview.widget.CardView
        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:id="@+id/view_click"
        android:layout_margin="4dp"
        app:cardElevation="6dp"
        android:clickable="true"
        android:focusable="true"
        android:foreground="?android:attr/selectableItemBackground"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        <LinearLayout
                android:gravity="center"
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:baselineAligned="false"
                tools:ignore="UnusedAttribute">
            <LinearLayout
                    android:padding="8dp"
                    android:orientation="vertical"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent">
                <ImageView
                        android:id="@+id/aduan_profil_image"
                        android:layout_width="50dp"
                        android:layout_height="50dp"
                        android:contentDescription="@string/profil_picture"/>
            </LinearLayout>

            <LinearLayout
                    android:gravity="center"
                    android:orientation="vertical"
                    android:layout_weight="1"
                    android:layout_width="0dp"
                    android:layout_height="match_parent">
                <TextView
                        android:id="@+id/tv_aduan_warga_nama"
                        android:layout_gravity="center"
                        android:textSize="18sp"
                        android:textStyle="bold"
                        tools:text="Lukman"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"/>
                <TextView
                        android:id="@+id/tv_aduan_waktu"
                        android:layout_gravity="center"
                        tools:text="2 days ago"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"/>
            </LinearLayout>


        </LinearLayout>
        <LinearLayout
                android:layout_marginBottom="8dp"
                android:layout_marginStart="10dp"
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginLeft="10dp">
            <TextView
                    android:textStyle="bold"
                    android:text="@string/lokasi_2"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"/>
            <TextView
                    android:textStyle="italic"
                    android:textColor="@color/colorPrimary"
                    android:id="@+id/tv_aduan_lokasi"
                    android:layout_marginStart="8dp"
                    tools:text="@string/lokasi"
                    android:maxLines="1"
                    android:ellipsize="end"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="8dp"
                    android:layout_marginRight="8dp"/>

        </LinearLayout>
        <com.borjabravo.readmoretextview.ReadMoreTextView
                app:colorClickableText="@color/primaryText"
                android:id="@+id/tv_aduan_deskripsi"
                android:layout_marginEnd="14dp"
                android:layout_marginLeft="12dp"
                tools:text="Judul ini merupakan judul yang akan ada pada berita nantinya dan akan diberitakan selama"
                app:trimExpandedText="Read more"
                app:trimCollapsedText="Read less"
                app:showTrimExpandedText="true"
                app:trimMode="trimModeLine"
                app:trimLines="3"
                android:layout_marginStart="12dp"
                android:layout_marginRight="14dp"
                android:textColor="@color/secondaryText"
                android:layout_width="match_parent"
                android:paddingBottom="8dp"
                android:layout_height="wrap_content"/>

        <FrameLayout
                android:layout_marginBottom="12dp"
                android:layout_marginTop="2dp"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
            <ImageView
                    android:id="@+id/iv_aduan_gambar"
                    android:scaleType="fitXY"
                    tools:src="@drawable/baunyaleee"
                    android:layout_width="match_parent"
                    android:layout_height="200dp"
                    tools:targetApi="m"
                    tools:ignore="ContentDescription,UnusedAttribute"/>
            <LinearLayout
                    android:elevation="4dp"
                    tools:targetApi="lollipop"
                    android:paddingRight="8dp"
                    android:paddingLeft="8dp"
                    android:layout_marginLeft="12dp"
                    android:background="@drawable/success_label"
                    android:layout_marginBottom="12dp"
                    android:layout_gravity="bottom"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="12dp">
                <TextView
                        android:textStyle="bold"
                        android:id="@+id/tv_aduan_status"
                        android:padding="6dp"
                        android:textColor="@color/text"
                        tools:text="Completed"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                />
            </LinearLayout>


        </FrameLayout>
        <LinearLayout
                android:id="@+id/komentarShow"
                android:layout_marginRight="4dp"
                android:layout_marginBottom="4dp"
                android:gravity="end"
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginEnd="4dp">
            <TextView
                    android:id="@+id/hideComent"
                    android:visibility="gone"
                    android:gravity="end"
                    android:textColor="@color/primaryText"
                    android:text="Hide Comment"
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="wrap_content"/>
            <TextView
                    android:id="@+id/showComent"
                    android:gravity="end"
                    android:textColor="@color/primaryText"
                    android:text="View 10 comment"
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="wrap_content"/>
            <ImageView
                    android:id="@+id/ic_expand_less"
                    android:src="@drawable/ic_expand_less"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
            <ImageView
                    android:id="@+id/ic_expand_more"
                    android:visibility="gone"
                    android:src="@drawable/ic_expand_more"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>

        </LinearLayout>
        <ProgressBar
                android:visibility="gone"
                android:id="@+id/progress_horizontal"
                android:layout_marginBottom="8dp"
                android:layout_gravity="center"
                android:layout_width="30dp"
                android:layout_height="30dp"/>
        <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_behavior="@string/appbar_scrolling_view_behavior"
                android:fillViewport="true">
            <ListView
                    android:id="@+id/commentList"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"/>
        </LinearLayout>
    </LinearLayout>
</androidx.cardview.widget.CardView>

recyclerview_list.xml在我的设计编辑器中是这样的

recyclerview_list.xml image

当我尝试运行我的应用时, 输出,就像这张图片:

Output

任何人都可以帮助我,对不起我的英语不好。

1 个答案:

答案 0 :(得分:0)

您不必这样做。只需创建一个回收器视图,然后在其中创建一个方向为“垂直”而不是listView的LinearLayout。现在,在recyclerView的OnBindViewHolder中,只需运行一个循环,直到list.size()并使用addView()不断扩大linearLayout中的视图。在for循环之前,只需removeAllView()。这将起作用,并且不会使滚动体验变差。