底部导航中的片段内的Kotlin Android Recycler视图不起作用

时间:2018-09-10 12:21:16

标签: android android-fragments android-recyclerview kotlin

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

有人可以帮我吗?

enter image description here

我的适配器类:

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>

2 个答案:

答案 0 :(得分:0)

  

您的问题可能出在这种方法中,

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)

我希望它能解决问题。

答案 1 :(得分:0)

这可能有助于某些人寻求清除相同的异常: 我所做的是,上面的 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">

我不知道它是如何工作的,但是它确实起作用了!!!所以任何知道那里发生的事情的人都可以解释!