当前位置: 首页 > 知识库问答 >
问题:

如何在回收器视图中单击

茹正祥
2023-03-14
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")
        }

    })

请帮助如何使其工作

共有1个答案

松旻
2023-03-14

原帖可以在这里找到

进一步的讨论可以在这里找到

下面是实现RecycerViewOnClickListener的一种更好且耦合较少的方法

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
      }
    })
);
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){}
}
 类似资料:
  • 单击时的卡片视图正在工作。但回收器视图的区域不可单击。如何使其可点击,以便捕获卡片视图的事件。

  • 我已经在我的回收器视图中实现了Admob原生快递广告。它在列表中保留空白,直到广告被加载,如果没有互联网可用。我怎样才能隐藏空白处直到广告加载?谢谢

  • 然后,在每个@test方法中,我调用activitytestrule.launchactivity(Intent.action_main)),然后进行单击。我发现,只有第一次点击不起作用,但第二次和第三次点击起作用。那么,这种行为的原因又能是什么呢?

  • 我刚开始在firebase工作。我设法上传了文本和图像,但是,我无法检索要显示在回收器视图中的图像,只能检索文本。我用的是毕加索依赖。我已经包括了我的主要活动。java类,该类负责显示从问题“我的适配器”中的firebase检索的回收器视图项。java类和模型类。我相信,在我将图像URI上载到firebase存储时,我可能犯了没有存储图像URI的错误,因此适配器无法检索图像位置。我想这可能是因为我

  • 如果用户点击一个,我希望它做一些事情,也为两个和三个。我如何实现这一点?

  • 这是我的适配器 我想每次滚动一个项目。比如你在Instagram上看到多张照片。我怎么能那样做? 谢谢 和我的XML;