代码之家  ›  专栏  ›  技术社区  ›  WISHY

安卓recylerview适配器中的数据绑定?

  •  0
  • WISHY  · 技术社区  · 6 年前

    我想将数据绑定到我的recylerview适配器。这是我当前遵循MVVM模式的代码

    碎片

    class NotificationFragment : Fragment() {
    var customeProgressDialog: CustomeProgressDialog? = null
    private val appPreferences: AppPreference by inject()
    private val notificationViewModel: NotificationViewModel by viewModel()
    private lateinit var binding: FragmentNotificationBinding
    
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = FragmentNotificationBinding.inflate(inflater, container, false)
        return binding.getRoot()
    }
    
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding.notification.layoutManager=LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false)
        customeProgressDialog = CustomeProgressDialog(activity)
        notificationViewModel.notifications(
            appPreferences.getUsername(),
            appPreferences.getPassword(),
            appPreferences.getUserId()
        )
        initObservables()
    }
    
    private fun initObservables() {
        notificationViewModel.progressDialog?.observe(this, Observer {
            if (it!!) customeProgressDialog?.show() else customeProgressDialog?.dismiss()
        })
        notificationViewModel.apiResponse?.observe(
            viewLifecycleOwner,
            androidx.lifecycle.Observer { response ->
                if (response.dataList != null) {
                    val notificationAdapter = NotificationAdapter(response.dataList as List<Data>)
                    notificationAdapter.notifyDataSetChanged()
                    binding.notification.adapter = notificationAdapter
                }
            })
    }
    }
    

    查看模型

    class NotificationViewModel(networkCall: NetworkCall) : ViewModel(),
    Callback<ApiResponse> {
    var progressDialog: SingleLiveEvent<Boolean>? = null
    var apiResponse: MutableLiveData<ApiResponse>? = null
    var networkCall: NetworkCall;
    
    init {
        progressDialog = SingleLiveEvent<Boolean>()
        apiResponse = MutableLiveData<ApiResponse>()
        this.networkCall = networkCall
    }
    
    fun notifications(username: String?, password: String?, userId: String?) {
        progressDialog?.value = true
        val apiPost = ApiPost()
        apiPost.userName = username
        apiPost.password = password
        apiPost.UserId = userId
        apiPost.FileType = NetworkConstant.FILE_TYPE_NOT
        networkCall.getPDF(apiPost).enqueue(this)
    }
    
    override fun onFailure(call: Call<ApiResponse>, t: Throwable) {
        progressDialog?.value = false
    }
    
    override fun onResponse(call: Call<ApiResponse>, response: Response<ApiResponse>) {
        progressDialog?.value = false
        apiResponse?.value = response.body()
    }
    }
    

    适配器

    class NotificationAdapter(private val list: List<Data>) :
    RecyclerView.Adapter<NotificationAdapter.ViewHolder>() {
    
    
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val inflater = LayoutInflater.from(parent.context)
        val binding = ElementListBinding.inflate(inflater)
    

    //val-view=inflater.inflate(R.layout.element_list,父级,false) return ViewHolder(绑定) }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val movie: Data = list[position]
        holder.bind(movie)
        holder.itemView.setOnClickListener {
    
            if (!TextUtils.isEmpty(movie.filePath)) {
                try {
                    val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse(movie.filePath))
                    holder.itemView.context.startActivity(browserIntent)
                } catch (e: Exception) {
                    e.printStackTrace()
                }
            }
    
        }
    }
    
    override fun getItemCount(): Int = list.size
    
    inner class ViewHolder(binding: ElementListBinding) : RecyclerView.ViewHolder(binding.root) {
        fun bind(movie: Data) {
            binding.item = movie
        }
    }
    }
    
    
    unable to find binding object
    

    recylerview元素列表xml

    <layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    
    <data>
        <variable
            name="data"
            type="com.mountmeru.model.Data" />
    </data>
    
    <androidx.cardview.widget.CardView
        android:id="@+id/main_cardview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp">
    
        <androidx.appcompat.widget.LinearLayoutCompat
            android:id="@+id/main_cardrl"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">
    
            <RelativeLayout
                android:id="@+id/rl_newsdate"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="0.3">
    
                <androidx.appcompat.widget.AppCompatTextView
                    android:id="@+id/tv_notifi"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:layout_marginTop="10dp"
                    android:layout_marginRight="10dp"
                    android:maxLines="2"
                    android:text="@{data.displayName}"
                    android:textColor="@android:color/black"
                    android:textSize="16sp" />
    
                <androidx.appcompat.widget.AppCompatTextView
                    android:id="@+id/tv_brief"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/tv_notifi"
                    android:layout_marginLeft="10dp"
                    android:layout_marginTop="5dp"
                    android:layout_marginRight="10dp"
                    android:textColor="@android:color/black"
                    android:textSize="16sp"
                    android:visibility="gone" />
    
                <androidx.appcompat.widget.AppCompatTextView
                    android:id="@+id/tv_date"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/tv_brief"
                    android:layout_marginLeft="10dp"
                    android:layout_marginTop="2dp"
                    android:layout_marginRight="10dp"
                    android:maxLines="1"
                    android:text="hey i am date"
                    android:textColor="@color/inactive_text"
                    android:textSize="14sp" />
            </RelativeLayout>
    
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_toRightOf="@+id/rl_newsdate"
                android:layout_weight="0.7"
                android:padding="5dp">
    
                <androidx.appcompat.widget.AppCompatImageView
                    android:id="@+id/iv_notifi"
                    android:layout_width="match_parent"
                    android:layout_height="75dp"
                    android:layout_centerHorizontal="true"
                    android:layout_centerVertical="true"
                    android:src="@drawable/mer" />
    
            </RelativeLayout>
    
        </androidx.appcompat.widget.LinearLayoutCompat>
    
    </androidx.cardview.widget.CardView>
    </layout>
    

    有人能确认我的MVVM实现是否正确,或者它需要一些重构吗?

    如何在recycleview列表元素xml中进行数据绑定?

    1 回复  |  直到 6 年前
        1
  •  4
  •   Alpha 1    4 年前

    您已经使用 <layout> 作为element_list.xml中的父标记。现在,您可以使用DataBinding在适配器类中展开它。请参阅下面的示例:

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding = ElementListBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return ViewHolder(Binding)
    }
    

    您必须修改ViewHolder类,如下所示:

    inner class ViewHolder(val binding: ElementListBinding) : RecyclerView.ViewHolder(binding.root) {
        fun bind(movie: Data) {
            with(itemView) {
                binding.tvNotifi.text = movie.displayName
                binding.tvDate.text = movie.UpdatedDate
                if (movie.description != null) {
                    binding.tvBrief.text = movie.description
                    binding.tvBrief.visibility = View.VISIBLE
                }
            }
        }
    }