我正在 RecyclerView上* 工作,并尝试使用 Interface来 对recyclcerview的每个项目使用点击监听器 *
这是我的活动课:
public class LegacyHomeActivity extends ActivityBaseDrawer { private List<LegacyVideo> legacyVideoList = null; private List<Video> videoList = new ArrayList<>(); private RecyclerView mRecyclerView; private LegacyModeHomeAdapter adapter; @Override public void onNetworkStateChanged(boolean connected) { } @Override protected void onCreate(Bundle savedInstanceState) { isLegacyMode = true; super.onCreate(savedInstanceState); setContentView(R.layout.activity_legacy_home); if (getSupportActionBar() != null) { setTitle(getString(R.string.footy_legacy_home)); } ImageView legacy_live_score_imageView = (ImageView) findViewById(R.id.legacy_live_score_imageView); legacy_live_score_imageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { startActivity(new Intent(LegacyHomeActivity.this, LegacyLiveScoreActivity.class)); } }); ImageView legacy_highlight_imageView = (ImageView) findViewById(R.id.legacy_highlight_imageView); legacy_highlight_imageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { startActivity(new Intent(LegacyHomeActivity.this, LegacyHighlightsActivity.class)); } }); ImageView legacy_news_imageView = (ImageView) findViewById(R.id.legacy_news_imageView); legacy_news_imageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { startActivity(new Intent(LegacyHomeActivity.this, LegacyNewsActivity.class)); } }); mRecyclerView = (RecyclerView) findViewById(R.id.legacy_mood_recyclerview); final LinearLayoutManager layoutManager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false); mRecyclerView.setLayoutManager(layoutManager); adapter = new LegacyModeHomeAdapter(getApplicationContext(), itemClickListener); mRecyclerView.setAdapter(adapter); mRecyclerView.setHasFixedSize(true); swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout); swipeRefreshLayout.setColorSchemeResources(R.color.ThemeColor, R.color.ThemeColor, R.color.ThemeColor); swipeRefreshLayout .setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { getLegacyVideoList(); } }); circularProgressView = (CircularProgressView) findViewById(R.id.circularProgressView); showLoadingProgress(); getLegacyVideoList(); } LegacyVideoClickListener itemClickListener = new LegacyVideoClickListener() { @Override public void onItemClickListener(int position) { Video video = videoList.get(position); VideoPlayerUtil.initVideoPLayer(video, LegacyHomeActivity.this); } }; private void getLegacyVideoList() { FootyLightAPI api = new FootyLightAPI(getApplicationContext(), mResponseListener); api.getLegacyHomeVideoList(); } private final FootyLightAPI.ResponseListener mResponseListener = new FootyLightAPI.ResponseListener() { @Override public void getResponse(int apiId, final String response) { switch (apiId) { case R.integer.ApiLegacyVideoList: Log.e("Legacy ............", "Response:................" + response); try { final ObjectMapper mapper = new ObjectMapper(); legacyVideoList = mapper.readValue(response, new TypeReference<List<LegacyVideo>>() { }); if (legacyVideoList != null && legacyVideoList.size() > 0) { videoList.clear(); for (LegacyVideo video : legacyVideoList) { Log.e("Legacy video", "" + video.getTitle()); Video videoItem = new Video(); if (video.getItemType().equalsIgnoreCase("video")) { videoItem.setTitle(video.getTitle()); videoItem.setLink(video.getLink()); videoItem.setThumb(video.getThumb()); videoItem.setContentType(video.getContentType()); videoItem.setContentUrl(video.getContentUrl()); videoItem.setDmcaLabel(video.getDmcaLabel()); videoItem.setDmcaContent(video.getDmcaContent()); videoItem.setDmcaDisclaimer(video.getDmcaDisclaimer()); videoItem.setDisplayType(video.getDisplayType()); videoList.add(videoItem); } } } Log.e("VideoList", "........." + videoList.size()); runOnUiThread(new Runnable() { @Override public void run() { showVideoAdapter(); hideLoadingProgress(); } }); } catch (Exception e) { } break; default: break; } } @Override public void getError(int apiId, int errorCode) { } @Override public void getResponse(String trackingStr, int apiId, String response) { } @Override public void getError(String trackingStr, int apiId, int errorCode) { } }; public void showVideoAdapter() { if (adapter != null) { adapter.setVideos(videoList); } } }
这是我的适配器类
public class LegacyModeHomeAdapter extends RecyclerView.Adapter<LegacyModeHomeAdapter.ItemViewHolder> { private List<Video> videoList; private Context mContext; private LegacyVideoClickListener onVideosRVItemClickListener; private LayoutInflater getLayoutInflater() { return LayoutInflater.from(mContext); } public LegacyModeHomeAdapter(Context mContext, LegacyVideoClickListener onVideosRVItemClickListener) { this.mContext = mContext; this.onVideosRVItemClickListener = onVideosRVItemClickListener; } public void setVideos(List<Video> videosList) { this.videoList = videosList; notifyDataSetChanged(); } @Override public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = getLayoutInflater().inflate(R.layout.layout_legacy_mode_home_video_item, parent, false); return new ItemViewHolder(view); } @Override public void onBindViewHolder(ItemViewHolder holder, final int position) { Video video = videoList.get(position); Picasso.with(mContext).load(video.getThumb()).into(holder.categoryImage); holder.categoryName.setText(video.getTitle()); holder.title_image_btn.setText(video.getDisplayType()); holder.VideosItemClickListener(position, onVideosRVItemClickListener); } @Override public int getItemCount() { if (videoList != null) { return videoList.size(); } return 0; } public class ItemViewHolder extends RecyclerView.ViewHolder { private TextView categoryName; private ImageView categoryImage; private Button title_image_btn; public ItemViewHolder(View itemView) { super(itemView); categoryName = (TextView) itemView.findViewById(R.id.categoryName); categoryImage = (ImageView) itemView.findViewById(R.id.categoryImage); title_image_btn = (Button) itemView.findViewById(R.id.title_image_btn); } public void VideosItemClickListener(final int position, final LegacyVideoClickListener videosRVItemClickListener) { itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { videosRVItemClickListener.onItemClickListener(position); } }); } } }
最后我将这个接口用于侦听器:
public interface LegacyVideoClickListener { public void onItemClickListener(int position); }
此过程可以正常运行我的应用程序的所有其他部分,但不适用于这种情况。我不知道问题出在哪里。请帮我 。
您这种方法:
@Override public void onBindViewHolder(final ViewHolder holder, int position) { final String element = mDataset[position]; holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //your code } }); }