RecyclerVIew onItemClickListener Non funziona

Sto lavorando a RecyclerView e cerco di utilizzare su listener di clic per each elemento di recyclcerview utilizzando Interface

Ecco la mia class Attività:

  • AlertDialog con button positivo e validation EditText personalizzato
  • Android VpnService per catturare i pacchetti non cattura i pacchetti
  • cosa esegue metodo Activity.finish ()?
  • Cattura immagini con la camera in android e salva con un nome personalizzato
  • Quando posso prima misurare una vista?
  • Recupera il context da un frammento
  • 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); } } } 

    E qui è la mia class adattatore

     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); } }); } } } 

    E finalmente sto usando questa interface per ascoltatore:

     public interface LegacyVideoClickListener { public void onItemClickListener(int position); } 

    Questa procedura funziona bene tutta la mia altra porzione di app, ma non funziona in questo caso. Non so where sia il problema. Mi aiuti per favore .

    6 Solutions collect form web for “RecyclerVIew onItemClickListener Non funziona”

    Tu questo approccio:

     @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 } }); } 

    Pass onVideosRVItemClickListener nel tuo constructor di ItemViewHolder.

     @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, onVideosRVItemClickListener); } @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()); } public class ItemViewHolder extends RecyclerView.ViewHolder { private TextView categoryName; private ImageView categoryImage; private Button title_image_btn; public ItemViewHolder(View itemView, final LegacyVideoClickListener videosRVItemClickListener) { 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); itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { videosRVItemClickListener.onItemClickListener(getAdapterPosition()); } }); } } 

    Controllare questo campione

    OnCLick può essere ottenuto utilizzando l'interface personalizzata

     interface Listener { void performOperation(int Position); } 

    nel tuo adattatore

     private Listener mListener; 

    Nella tua scheda inizializza il tuo listener in constructor come questo

     mListener = (Listener) context; 

    Nel metodo bindViewHolder

     holder.yourView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mListener.performOperation(position); } }); 

    Ora nella tua attività implementa l'interface come questa

     public class LegacyHomeActivity extends ActivityBaseDrawer implements listner { @Override public void performOperation(int Position) { //Do whatever you want to do with this position } } 

    La class dell'adattatore di interface dovrebbe essere così

     public class LegacyModeHomeAdapter extends RecyclerView.Adapter<LegacyModeHomeAdapter.ItemViewHolder> { private List<Video> videoList; private Context mContext; public static 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 implements View.OnClickListener { 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); itemView.setOnClickListener(this); } @Override public void onClick(View v) { // TODO Auto-generated method stub onVideosRVItemClickListener.onItemClickListener(getPosition(), v); } } public void setOnItemClickListener(LegacyVideoClickListener clickListener) { LegacyModeHomeAdapter.onVideosRVItemClickListener = clickListener; } public interface LegacyVideoClickListener { public void onItemClickListener(int position, View v); } } 

    E la class di utilizzo dell'adattatore dovrebbe essere così

     adapter.setOnItemClickListener(new LegacyModeHomeAdapter.LegacyVideoClickListener() { @Override public void onItemClick(int position, View v) { //Your code } }); 

    Utilizzando questo modo di impostazioni di interface è ansible get la function di listener onclick esattamente. Fathemes sapere se hai problemi ancora.

      class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { public Button btMyButton; public ViewHolder(View itemView) { super(itemView); itemView.setOnClickListener(this); } @Override public void onClick(View v) { // your implementation. In order to get position you can use `getAdapterPosition()` method } } 
     @Override public void onBindViewHolder(RecyclerViewAdapter.ItemViewHolder holder, int position) { holder.name.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { } }); } 

    Provare così

    L'Android è un fan Android di Google, tutto su telefoni Android, Android Wear, Android Dev e applicazioni Android Games e così via.