本文介绍了RecylerView上的OnclickListener并获取单击项的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在RecyclerView上设置onClickListener,以获取被单击项的Json ID,然后重定向到片段,该片段根据该Json ID显示产品的详细信息.如何在适配器中实现呢?我被困在这里.我可以设置onClickListener,但无法获取Json ID.
I need to set onClickListener on RecyclerView which gets the Json Id of an Item Clicked and redirects to the fragment which displays the details of the product based on that Json ID. How Can I implement this in adapter? I am stuck here. I was able to set onClickListener but I couldn't get the Json ID.
这是我的适配器.
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import com.example.dell.retroexample.MainActivity;
import com.example.dell.retroexample.R;
import com.example.dell.retroexample.apiHandlers.model.Ad;
import java.util.List;
/**
* Created by dell on 7/3/2017.
*/
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder>{
private List<Ad> ads;
private Context context;
public RecyclerAdapter(List<Ad> ads, Context context){
this.ads=ads;
this.context=context;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item_ads, parent,false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.name.setText(ads.get(position).getTitle());
holder.details.setText("Rs. " +ads.get(position).getPrice().toString()+ "/-");
// Glide.with(context).load(ads.get(position).getPhoto1()).into(holder.thumbnail);
}
@Override
public int getItemCount() {
return ads.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
ImageView thumbnail;
TextView name, details;
public MyViewHolder(View itemView) {
super(itemView);
// thumbnail=(ImageView)itemView.findViewById(R.id.thumbnail);
name=(TextView)itemView.findViewById(R.id.title);
details=(TextView)itemView.findViewById(R.id.price);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
Toast.makeText(view.getContext(), "Clicked on Recycler View" , Toast.LENGTH_SHORT).show();
}
}
}
这是我的MainActivity
This is my MainActivity
package com.example.dell.retroexample;
import android.content.res.Resources;
import android.graphics.Rect;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import com.example.dell.retroexample.apiHandlers.adapter.RecyclerAdapter;
import com.example.dell.retroexample.apiHandlers.model.Ad;
import com.example.dell.retroexample.apiHandlers.rest.ApiInterface;
import com.example.dell.retroexample.apiHandlers.rest.ApiClient;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
private RecyclerAdapter adapter=null;
private List<Ad> ads;
private ApiInterface apiInterface;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView=(RecyclerView)findViewById(R.id.ads_recycler_view);
// calculating the number of columns
int columns=2;
DisplayMetrics lDisplayMetrics = getResources().getDisplayMetrics();
double widthPixels = lDisplayMetrics.widthPixels;
Log.e("Width", String.valueOf(widthPixels));
columns=(int)widthPixels/dpToPx(170);
RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(this, columns);
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
apiInterface=ApiClient.getClient().create(ApiInterface.class);
Call <List<Ad>> call=apiInterface.getads();
call.enqueue(new Callback<List<Ad>>() {
@Override
public void onResponse(Call<List<Ad>> call, Response<List<Ad>> response) {
ads=response.body();
adapter=new RecyclerAdapter(ads, MainActivity.this);
recyclerView.setAdapter(adapter);
}
@Override
public void onFailure(Call<List<Ad>> call, Throwable t) {
Log.e(TAG, t.toString());
}
});
}
private int dpToPx(int dp) {
Resources r = getResources();
return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics()));
}
}
推荐答案
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.name.setText(ads.get(position).getTitle());
holder.details.setText("Rs. " +ads.get(position).getPrice().toString()+ "/-"); Glide.with(context).load(ads.get(position).getPhoto1()).into(holder.thumbnail);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String id=ads.get(position).getId();
//You can call detail fragment here
}
});
}
这篇关于RecylerView上的OnclickListener并获取单击项的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!