【问题标题】:Kotlin Android Recycler view inside Fragment with Bottom Navigation not working带有底部导航的片段内的 Kotlin Android Recycler 视图不起作用
【发布时间】:2018-09-10 12:21:15
【问题描述】:

我正在尝试开发使用 Android Jetpack 库的社交网络应用程序,但在使用导航组件使用底部导航来导航活动内的片段时,此适配器在 LayoutInflator() 处引发错误导致应用崩溃

谁能帮我解决这个问题:

我的适配器类:

class FeedAdapter : PagedListAdapter<feed,FeedAdapter.ViewHolder>(FeedDiffCallBack()){
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val userPost = LayoutInflater.from(parent.context)
                    .inflate(R.layout.feedrow,parent,false)
        return ViewHolder(userPost)
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    val feedItem = getItem(position)
    if(feedItem != null){
        holder.bind(feedItem)
    }
}

class ViewHolder(itemView:View):RecyclerView.ViewHolder(itemView) {
    //Retrieve data
    private val username:TextView = itemView.post_name

    private val userPic:ImageView = itemView.feedImage1
    private val location:TextView = itemView.postLocation
    private val time:TextView = itemView.postTime
    private val post:ImageView = itemView.postImage

    fun bind(feed: feed) = with(itemView){
        //TODO:Bind Data with View
            showFeedData(feed)

    }

    private fun showFeedData(feed: feed) {
        username.text = feed.username
        userPic.setImageURI(null)
        userPic.visibility = View.GONE
        location.text = feed.location
        time.text = feed.timeStamp.toString()
        post.setImageURI(Uri.parse(feed.mUrl))

    }

}

 }

class FeedDiffCallBack : DiffUtil.ItemCallback<feed>() {
override fun areItemsTheSame(oldItem:feed, newItem: feed): Boolean {
    return oldItem?.id == newItem?.id
}

override fun areContentsTheSame(oldItem: feed, newItem: feed): Boolean {
    return oldItem == newItem
}

}

片段类:

class FeedFragment : Fragment() {

companion object {
    fun newInstance() = FeedFragment()
}

private lateinit var viewModel: FeedViewModel
override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
): View? {
    val view =  inflater.inflate(R.layout.feed_fragment, container, false)
    val context = getContext() ?: return view

    val factory = InjectorUtils.provideViewModelFactory(context)
    viewModel = 
 ViewModelProviders.of(this,factory).get(FeedViewModel::class.java)
    val adapter = FeedAdapter()
    view.findViewById<RecyclerView>(R.id.feedView).adapter = adapter
    view.findViewById<RecyclerView>(R.id.feedView).layoutManager = 
LinearLayoutManager(MyApplication.getContext())
    subscribeUI(adapter)
    return view
}

private fun subscribeUI(adapter: FeedAdapter) {
    viewModel.showFeed().observe(this, object:Observer<PagedList<feed>>{
        override fun onChanged(t: PagedList<feed>?) {
            adapter.submitList(t)
            adapter.notifyDataSetChanged()
        }

    })
}


override fun onActivityCreated(savedInstanceState: Bundle?) {
    super.onActivityCreated(savedInstanceState)
}

}

feed_row.xml 用于回收站视图的单个项目 -->

<RelativeLayout
    android:id="@+id/postContainer"
    android:layout_margin="10dp"
    android:elevation="2dp"
    android:background="@drawable/bg_parent_rounded_corner"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <!--TODO:Change to Circle Image View-->
    <ImageView
        android:id="@+id/profileImage"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginTop="5dp"
        android:layout_marginLeft="5dp"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter"

        />
    <LinearLayout
        android:id="@+id/postDetail_1"
        android:orientation="vertical"
        android:layout_marginTop="5dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="20dp"
        android:layout_alignParentRight="true">
        <TextView
            android:id="@+id/post_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@style/LabelStyle"
            android:textSize="15sp"
            android:fontFamily="@font/sf_pro_display_semibold" />
        <LinearLayout
            android:id="@+id/postDetail_2"
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <TextView
                android:id="@+id/postLocation"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <TextView
                android:id="@+id/postTime"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="120dp" />
        </LinearLayout>

    </LinearLayout>

    <ImageView
        android:id="@+id/postImage"
        android:layout_height="200dp"
        android:layout_width="match_parent"
        android:layout_below="@+id/postDetail_1"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter"
        android:layout_marginTop="6dp"
       />



</RelativeLayout>

【问题讨论】:

  • 如何将视图对象绑定到 XML 文件?看起来您可能正在使用 kotlin android 扩展。如果是这种情况,我发现您的 XML 文件中不存在诸如 feedImage1 之类的某些字段
  • 我清除了!!!但是仍然出现同样的错误,当我尝试使用断点调试适配器时,LayoutInflator.from(),inflate() 函数会导致这个问题!!! @NicolásCarrasco

标签: android android-fragments android-recyclerview kotlin


【解决方案1】:

你的问题可能出在这个方法上,

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
   val feedItem = getItem(position)
   if(feedItem != null){
      holder.bind(feedItem)
   }
}

您的 ViewHolder 正在使用可能不一致的方法参数中的 poisition

See from here,

所以,你应该改成这样:

val feedItem = getItem(holder.adapterPosition)

而不是

val feedItem = getItem(position)

希望能解决问题。

【讨论】:

  • 不!这不解决问题!!!!同样的,二进制 XML 文件行:#3 Error inflating class layout 并显示在 Adapter @Jeel Vankhede 中的 onCreateView() 内的 LayoutInflator 方法发生错误
  • 您可以将您的imports 显示为kotlin extensions 吗? @Dilip
  • 它现在正在工作,我不知道如何,但我只是在布局文件中添加了 xmlns:appxmlns:tools 行并开始了工作
【解决方案2】:

这可能会帮助某些人寻找要清除的相同异常: 我所做的是我上面的 feed_row.xml 包含在 标记中,我以这种方式对其进行了更改,因此清除了异常:

异常之前:

<layout xmlns:android="http://schemas.android.com/apk/res/android">

改成此异常后清除:

 <layout 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">

我不知道它是如何工作的,但它确实有效!!!所以任何知道那里发生了什么的人都可以解释一下!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-13
    • 1970-01-01
    • 2017-03-20
    相关资源
    最近更新 更多