为什么单击RecyclerView项会返回错误的项ID?

时间:2017-11-25 09:25:54

标签: android android-recyclerview kotlin

为什么点击RecyclerView项会返回错误的商品ID? (它根据滚动列表而改变)

我有Activity里面有一个按钮。当用户点击按钮时,我弹出一个对话框。在对话框中,我有一个RecyclerView。 我的RecyclerView适配器使用Web服务来获取列表:

setRecyclerViewAdapter(recycleView,1)
val dialogBuilder = AlertDialog.Builder(this)
val dialogView = this.layoutInflater.inflate(R.layout.dialog_menu, null)
dialogBuilder.setView(dialogView)
val b = dialogBuilder.create()
b.show()

下面的代码用于从webservice获取数据并设置RecyclerView适配器:

private fun setRecyclerViewAdapter(recycleList:RecyclerView,subjectId:Int){
     recycleList.layoutManager =LinearLayoutManager(this,LinearLayoutManager.VERTICAL, false)

        doAsync {
            categoryList= GetSubCategoriesWebService()
                    .SetRecyclerViewAdapter(subjectId)
            uiThread {
            }
            onComplete {
                val onClick= View.OnClickListener {
                    val returnIntent = Intent()
                    returnIntent.putExtra("result", result[0].toString())
                    setResult(Activity.RESULT_OK, returnIntent)
                    finish()
                }
                val adapter = CategoryList_Adapter(categoryList,onClick,result)
                recycleList.adapter = adapter
                adapter.notifyDataSetChanged()
            }
        }
    }

CategoryList_Adapter我使用onClick RecyclerView setOnClickListener函数...

请注意,Result是一个数组,用于存储用户点击RecyclerView项目并将ID返回给活动,并填入CategoryList_Adapter

这是CategoryList_Adapter代码:

class CategoryList_Adapter(private val dataList: ArrayList<CategoryListObject>?, 
                           private val listener: View.OnClickListener?, 
                           private val adSubjectId :IntArray?) 
    : RecyclerView.Adapter<CategoryList_Adapter.ItemRowHolder>() {
    override fun onCreateViewHolder(viewGroup: ViewGroup, i: Int): ItemRowHolder {
        val rowNumber = getItemViewType(i)
        val view = LayoutInflater.from(viewGroup.context)
                .inflate(R.layout.category_listview_row, viewGroup, false)
        return ItemRowHolder(view, rowNumber, dataList!![i].id)
    }

    override fun onBindViewHolder(itemRowHolder: ItemRowHolder, i: Int) {
        Log.d("position  ", i.toString())
        itemRowHolder.itemTitle.text = dataList!![i].title
    }
     inner class ItemRowHolder(view: View,
                              rowNumber: Int,
                              subjectId: Int)
        : RecyclerView.ViewHolder(view) {
        var itemTitle: TextView

        init {
            view.setOnClickListener(listener)
            if(adSubjectId!=null)
                adSubjectId[0]=subjectId
     }
}

为什么?

感谢您的耐心等待。

1 个答案:

答案 0 :(得分:1)

是的,我遇到了同样的问题,但我使用界面解决了它。 首先,您将在适配器类中的click类上创建一个接口。

例如:     公共类TestAdapter扩展了RecyclerView.Adapter {

Context context;
OnTapClickListner tapClick;
List<ModelClass> datas;

public TestAdapter(Context context,OnTapClickListner tapClick,List<ModelClass> datas) {
        super();
        this.context = context;
        this.mediaClick = mediaClick;
        this.datas = datas;
    }

@Override
public void onBindViewHolder(ViewHolder holder, int position) {

  ModelClass mainData = datas.get(position);

  holder.View.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               tapClick.onItemClick(mainData);
           }
    }
 }

@Override
public int getItemCount() {
       return datas.size();
  }

public interface OnTapClickListner {
        void onItemClick(ModelClass data);
    }
}

你在adpter类中设置了这样的界面。 那么你将在主类中使用适配器,并且onclick如下所示:

TestAdapter.OnTapClickListner itemClick = new TestAdapter.OnTapClickListner() {
        @Override
        public void onItemClick(ModelClass data) {

    }
}

在您的adpter中使用Itemclick,如下所示:

new TestAdapter(this,itemClick,datas);