如何在回收站视图中单击

时间:2018-11-02 03:27:45

标签: android android-recyclerview kotlin recycler-adapter

我有一个问题,可以单击一个回收站视图,但是我不知道它是如何工作的。但是也许必须对适配器进行更改才能使其实现,但我对此却有些困惑。

这是我的适配器

class MainAdapter(val teamFeed: LastActivity.TeamFeed) :
    RecyclerView.Adapter<ViewHolder>() {
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
    val team = teamFeed.events.get(position)
    holder.itemView.home_team.text = team.strHomeTeam
    holder.itemView.away_team.text = team.strAwayTeam
    holder.itemView.home_score.text = team.intHomeScore
    holder.itemView.away_score.text = team.intAwayScore
    holder.itemView.tanggal.text = team.dateEvent
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {

    val layoutInflater = LayoutInflater.from(parent?.context)
    val cellForRow = layoutInflater.inflate(R.layout.item_layout, parent, false)

    return ViewHolder(cellForRow)
}


    override fun getItemCount(): Int {
    return teamFeed.events.count()
}


override fun getItemViewType(position: Int): Int {
    return position
}
class ViewHolder(val view: View) : RecyclerView.ViewHolder(view)

}

这是我想通过点击来表达意图的活动

client.newCall(request).enqueue(object : Callback, okhttp3.Callback {
        override fun onResponse(call: okhttp3.Call?, response: okhttp3.Response?) {
            val body = response?.body()?.string()
            println(body)

            val gson = GsonBuilder().create()
            val teamFeed = gson.fromJson(body, TeamFeed::class.java)

            runOnUiThread {
                recyclerView_main.adapter = MainAdapter(teamFeed)

            }
        }

        override fun onFailure(call: okhttp3.Call?, e: IOException) {
            println("failed")
        }

    })

请帮助如何使其工作

2 个答案:

答案 0 :(得分:1)

原始帖子可以找到here

可以在here

中找到更多讨论

这是一种为OnClickListener实现RecyclerView的更好且紧密耦合的方法。

使用摘要:

RecyclerView recyclerView = findViewById(R.id.recycler);
recyclerView.addOnItemTouchListener(
    new RecyclerItemClickListener(context, recyclerView ,new RecyclerItemClickListener.OnItemClickListener() {
      @Override public void onItemClick(View view, int position) {
        // do whatever
      }

      @Override public void onLongItemClick(View view, int position) {
        // do whatever
      }
    })
);

RecyclerItemClickListener实现:

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;


public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {
  private OnItemClickListener mListener;

  public interface OnItemClickListener {
    public void onItemClick(View view, int position);

    public void onLongItemClick(View view, int position);
  }

  GestureDetector mGestureDetector;

  public RecyclerItemClickListener(Context context, final RecyclerView recyclerView, OnItemClickListener listener) {
    mListener = listener;
    mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            return true;
        }

        @Override
        public void onLongPress(MotionEvent e) {
            View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
            if (child != null && mListener != null) {
                mListener.onLongItemClick(child, recyclerView.getChildAdapterPosition(child));
            }
        }
    });
}

  @Override public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
    View childView = view.findChildViewUnder(e.getX(), e.getY());
    if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
      mListener.onItemClick(childView, view.getChildAdapterPosition(childView));
      return true;
    }
    return false;
  }

  @Override public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) { }

  @Override
  public void onRequestDisallowInterceptTouchEvent (boolean disallowIntercept){}
}

答案 1 :(得分:0)

希望对您有帮助

以这种方式,从您的Viewholder中实现OnClickListener事件

ClickListener.kt界面

interface ClickListener {
    fun onClick(view:View, index:Int)
}

OnClickMainAdapter中的实现

class MainAdapter(val teamFeed: LastActivity.TeamFeed, var listener: ClickListener) : RecyclerView.Adapter<MainAdapter.ViewHolder>() {
...
...
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
    val layoutInflater = LayoutInflater.from(parent?.context)
    val cellForRow = layoutInflater.inflate(R.layout.item_layout, parent, false)

    return ViewHolder(cellForRow, listener)
}
...
...
class ViewHolder(val view: View,  listener:ClickListener) : RecyclerView.ViewHolder(view), View.OnClickListener{
    var listener: ClickListener?=null
    init{
        this.listener=listener
        itemView.setOnClickListener(this)
    }
    override fun onClick(view: View?) {
        this.listener?.onClick(view!!, adapterPosition)
    }
}

使用摘要:

var recyclerview=findViewById(R.id.recyclerview)
var mainAdapter=MainAdapter(/*LastActivity.TeamFeed parameter*/, object : ClickListener{
                override fun onClick(view: View, index: Int) {
                    //do whatever
                }
            })
recyclerview.adapter=mainAdapter