RecyclerView中的自定义列表项侦听器接口未响应单击

时间:2017-04-10 10:56:05

标签: android android-recyclerview

适配器

public class RVAdapter_CategoryDetails extends
    RecyclerView.Adapter<RVAdapter_CategoryDetails.ViewHolder> {

final private RVAdapter_CategoryDetails.ListItemClickListener mListItemClickListener;

//Interface for handling Item Clicks
public interface ListItemClickListener {
    void onListItemClick(int clickedItemIndex, String place_id);
}

private List<Example> exampleList;
private Context context;
private List<Result> resultList;

// Constructor
RVAdapter_CategoryDetails(ListItemClickListener listener,List<Result> exampleList){
    this.resultList = exampleList;
    mListItemClickListener = listener;
}


@Override
public RVAdapter_CategoryDetails.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(
            R.layout.category_cards_layout, parent, false);
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(RVAdapter_CategoryDetails.ViewHolder holder, int position) {
    holder.tv_place_name.setText(resultList.get(position).getName());
    holder.tv_place_address.setText(resultList.get(position).getVicinity());    }

@Override
public int getItemCount() {
    return resultList.size();
}

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

    ImageView iv_place_photo;
    TextView tv_place_name, tv_place_address;
    RatingBar rb_place_rating;

    public ViewHolder(View itemView) {
        super(itemView);
        iv_place_photo = (ImageView) itemView.findViewById(R.id.place_photo);
        tv_place_name = (TextView) itemView.findViewById(R.id.place_name);
        tv_place_address = (TextView) itemView.findViewById(R.id.place_address);
        rb_place_rating = (RatingBar) itemView.findViewById(R.id.place_rating);
    }

    @Override
    public void onClick(View v) {
        int clickedPosition = getAdapterPosition();
        String place_id = resultList.get(clickedPosition).getPlaceId();
        mListItemClickListener.onListItemClick(clickedPosition, place_id);
    }
}
}

活动

public class Category extends AppCompatActivity implements
    RVAdapter_CategoryDetails.ListItemClickListener{

String type;
int itemIndex = 0, radius = 15000;
Double latitude, longitude;
String category;
List<Result> places;
RecyclerView recyclerView;
RVAdapter_CategoryDetails adapter;
Toast mToast;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.category_layout);

    ActionBar actionBar = getSupportActionBar();
    // TODO (COMPLETED) : Fix the ActionBar disappearing
    if (actionBar != null) {
        actionBar.setDisplayHomeAsUpEnabled(true);
    }

    Intent intent = getIntent();
    if (intent != null) {
        Bundle extras = intent.getExtras();
        itemIndex = extras.getInt("clickIndex", 0);
        latitude = extras.getDouble("latitude");
        longitude = extras.getDouble("longitude");

        Log.i(String.valueOf(this), "Lat: " + latitude + "\nLong: " + longitude);
    }

    // TODO (1) : find a better solution to this
    switch (itemIndex) {
        case 0:
            setTitle("ATMs");
            category = "atm";
            break;
        case 1:
            setTitle("Banks");
            category = "bank";
            break;
        case 2:
            setTitle("Bars");
            category = "bars";
            break;
        case 3:
            setTitle("Hospital");
            category = "hospital";
            break;
        case 4:
            setTitle("Parks");
            category = "park";
            break;
        case 5:
            setTitle("Police");
            category = "police";
            break;
        case 6:
            setTitle("Shopping");
            category = "shopping";
            break;
        case 7:
            setTitle("Stores");
            category = "store";
            break;
        case 8:
            setTitle("Train Stations");
            category = "train_station";
            break;
        case 9:
            setTitle("Taxi Stations");
            category = "travel_agency";
            break;
        case 10:
            setTitle("Movie Theaters");
            category = "movie_theater";
            break;
        default:
            setTitle("Respected Category Here");
    }

    //initializing the views for layout
    initViews();
}

private void initViews() {
    recyclerView = (RecyclerView) findViewById(R.id.rv_cat_details);
    recyclerView.setHasFixedSize(true);
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(
            getApplicationContext());
    recyclerView.setLayoutManager(layoutManager);
    loadJson();
}

private void loadJson() {
    Retrofit retrofit = ApiClient.getClient();
    ApiInterface apiInterface = retrofit.create(ApiInterface.class);

    Call<Example> exampleCall = apiInterface.getNearbyPlaces
            (latitude + "," + longitude, radius, category);

    exampleCall.enqueue(new Callback<Example>() {
        @Override
        public void onResponse(Call<Example> call, Response<Example> response) {
            places = response.body().getResults();
            Log.d(String.valueOf(this), "Number of places received: " + places.size());
            setAdapter();
        }

        @Override
        public void onFailure(Call<Example> call, Throwable t) {
            Log.d(String.valueOf(this), t.toString());
        }
    });
}

private void setAdapter(){
    adapter = new RVAdapter_CategoryDetails(this, places);
    recyclerView.setAdapter(adapter);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    if (item.getItemId() == R.id.home) {
        Intent actMain = new Intent(Category.this, MainActivity.class);
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void onListItemClick(int clickedItemIndex, String place_id) {
    Log.i(String.valueOf(this),  "Item #" +clickedItemIndex + "\nPlace ID:" +place_id);

    if(mToast!=null){ mToast.cancel();}
    mToast = Toast.makeText(this, "Item #" +clickedItemIndex + "\nPlace ID:" +place_id, Toast.LENGTH_LONG);
    mToast.show();
}
}

我已经实现了自定义界面,它可以在正确的位置处理列表项上的onClick事件。但是,当我点击任何项时,onListItemClick()方法不会被调用。

2 个答案:

答案 0 :(得分:3)

您应该在View.OnclickListener课程中实施ViewHolder,将课程更改为:

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

        ImageView iv_place_photo;
        TextView tv_place_name, tv_place_address;
        RatingBar rb_place_rating;

        public ViewHolder(View itemView) {
            super(itemView);
            iv_place_photo = (ImageView) itemView.findViewById(R.id.place_photo);
            tv_place_name = (TextView) itemView.findViewById(R.id.place_name);
            tv_place_address = (TextView) itemView.findViewById(R.id.place_address);
            rb_place_rating = (RatingBar) itemView.findViewById(R.id.place_rating);

//It`s missing this line
tv_place_name.setOnClickListener(this);

        }

        @Override
        public void onClick(View v) {
            int clickedPosition = getAdapterPosition();
            String place_id = resultList.get(clickedPosition).getPlaceId();
            mListItemClickListener.onListItemClick(clickedPosition, place_id);
        }
    }

答案 1 :(得分:0)

在ViewHolder中您需要添加一行

itemView.setOnClickListener(this);