如何仅为一个回收站视图适配器设置不同的OnClickListener

时间:2019-11-20 13:03:57

标签: android android-recyclerview recycler-adapter

我正在开发一个能够搜索用户以及餐厅的android应用。因此,我正在使用自定义适配器来处理两种类型的项目。我需要显示的对象是“用户”和“餐馆”。这些对象需要完全不同的布局才能显示它们。

这是我的SearchingFragment,其中包含适配器:

public class SearchingFragment extends Fragment {

private SearchView searchView;
private RecyclerView resultList;

private RecyclerView.Adapter<RecyclerView.ViewHolder> adapter;
final int VIEW_TYPE_USER = 0;
final int VIEW_TYPE_EATERIES = 1;

private ArrayList<User> userArrayList = new ArrayList<User>();
private ArrayList<Eateries2> eateriesArrayList = new ArrayList<Eateries2>();

FirebaseFirestore db = FirebaseFirestore.getInstance();
String TAG;

private OnItemClickListener mListener;

public interface OnItemClickListener {
    void OnItemClick(int position);

}

public void setOnItemClickListener(OnItemClickListener listener) {
    mListener = listener;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View root = inflater.inflate(R.layout.fragment_searching, container, false);
    //return inflater.inflate(R.layout.fragment_searching, container, false);

    searchView = root.findViewById(R.id.searchView);

    resultList = root.findViewById(R.id.resultList);
    resultList.setHasFixedSize(true);
    resultList.setLayoutManager(new LinearLayoutManager(getActivity()));

    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            if(newText.trim().isEmpty()){
                userArrayList = new ArrayList<>();
                eateriesArrayList = new ArrayList<>();
            }
            else {
                userArrayList = new ArrayList<>();
                eateriesArrayList = new ArrayList<>();
                FirestoreUserSearch(newText);
            }
            return false;
        }
    });



    return root;
}


private void FirestoreUserSearch(String searchText) {

    db.collection("users")
            .orderBy("username")
            .startAt(searchText)
            .endAt(searchText + "\uf8ff")
            .get()
            .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                @Override
                public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
                    for(DocumentChange document :queryDocumentSnapshots.getDocumentChanges()){
                        userArrayList.add(document.getDocument().toObject(User.class));
                        adapter.notifyDataSetChanged();
                    }
                }
            });


    db.collection("eateries")
            .orderBy("placeName")
            .startAt(searchText)
            .endAt(searchText + "\uf8ff")
            .get()
            .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                @Override
                public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
                    for(DocumentChange doc : queryDocumentSnapshots.getDocumentChanges()){
                        String placeId = doc.getDocument().getString("placeID");
                        String placeName = doc.getDocument().getString("placeName");
                        List<String> category = (List<String>)doc.getDocument().get("category");
                        String address = doc.getDocument().getString("address");
                        Double rating = doc.getDocument().getDouble("rating");
                        Double price_level = doc.getDocument().getDouble("price_level");

                        Eateries2 eateries = new Eateries2(address, category, placeId, placeName, rating, price_level);
                        eateriesArrayList.add(eateries);
                        adapter.notifyDataSetChanged();
                    }
                }
            });



    adapter = new RecyclerView.Adapter<RecyclerView.ViewHolder>() {

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view;

            if (viewType==VIEW_TYPE_USER){
                view = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.user_list_layout,parent,false);
                return  new SearchingFragment.UserViewHolder(view);
            }
            if(viewType==VIEW_TYPE_EATERIES){
                view = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.eateries_list_layout,parent,false);
                return new SearchingFragment.EateriesViewHolder(view,mListener);
            }
            return null;

        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
            if(holder instanceof SearchingFragment.UserViewHolder){
                ((SearchingFragment.UserViewHolder) holder).userName.setText(userArrayList.get(position).getUsername());
                ((SearchingFragment.UserViewHolder) holder).category.setText("User");
                ((SearchingFragment.UserViewHolder) holder).userImage.setImageURI(null);
                if(userArrayList.get(position).getPhoto()!=null){
                    Uri uri=Uri.parse(userArrayList.get(position).getPhoto());
                    Glide.with(getContext()).load(uri).into(((SearchingFragment.UserViewHolder) holder).userImage);
                }

            }

            if(holder instanceof SearchingFragment.EateriesViewHolder){
                ((SearchingFragment.EateriesViewHolder) holder).placeName.setText(eateriesArrayList.get(position-userArrayList.size()).getPlaceName());
                ((SearchingFragment.EateriesViewHolder) holder).category.setText("Eatery\n"+eateriesArrayList.get(position-userArrayList.size()).getCategory());
                ((SearchingFragment.EateriesViewHolder) holder).address.setText(Html.fromHtml(eateriesArrayList.get(position-userArrayList.size()).getAddress()));
                ((EateriesViewHolder) holder).ratingBar.setRating(Float.parseFloat(eateriesArrayList.get(position-userArrayList.size()).getRating().toString()));
                ((EateriesViewHolder) holder).ratingBar.setIsIndicator(true);
                ((EateriesViewHolder) holder).priceRateBar.setRating(Float.parseFloat(eateriesArrayList.get(position-userArrayList.size()).getPrice_level().toString()));

                List<Place.Field> fields = Arrays.asList(Place.Field.PHOTO_METADATAS);
                FetchPlaceRequest placeRequest = FetchPlaceRequest.newInstance(eateriesArrayList.get(position-userArrayList.size()).getPlaceID(), fields);
                String api = "AIzaSyDz5BGny6Lsp7gW-uJznoLVZS4riEdfnF0";
                Places.initialize(getContext(), api);
                PlacesClient placesClient = Places.createClient(getContext());

                placesClient.fetchPlace(placeRequest).addOnSuccessListener((response) -> {
                    Place place = response.getPlace();
                    PhotoMetadata photoMetadata = place.getPhotoMetadatas().get(0);
                    String attributions = photoMetadata.getAttributions();

                    FetchPhotoRequest photoRequest = FetchPhotoRequest.builder(photoMetadata)
                            .build();
                    placesClient.fetchPhoto(photoRequest).addOnSuccessListener((fetchPhotoResponse) -> {
                        Bitmap bitmap = fetchPhotoResponse.getBitmap();
                        ((SearchingFragment.EateriesViewHolder) holder).eateryImage.setImageBitmap(bitmap);
                        // imageView.setImageBitmap(bitmap);
                    }).addOnFailureListener((exception) -> {
                        if (exception instanceof ApiException) {
                            ApiException apiException = (ApiException) exception;
                            int statusCode = apiException.getStatusCode();
                            // Handle error with given status code.
                            Log.e(TAG, "Place not found: " + exception.getMessage());
                        }
                    });
                });

            }
        }

        @Override
        public int getItemCount() {
            return userArrayList.size()+eateriesArrayList.size();
        }

        @Override
        public int getItemViewType(int position){
            if(position<userArrayList.size()){
                return VIEW_TYPE_USER;

            }
            if(position-userArrayList.size()<eateriesArrayList.size()){
                return VIEW_TYPE_EATERIES;
                }
            return -1;
        }
    };
    resultList.setAdapter(adapter);

}

private class UserViewHolder extends RecyclerView.ViewHolder {
    TextView userName;
    ImageView userImage;
    TextView category;

    UserViewHolder(View itemView) {
        super(itemView);
        userName = itemView.findViewById(R.id.userName);
        userImage = itemView.findViewById(R.id.userImage);
        category = itemView.findViewById(R.id.catagory);

    }
}

private class EateriesViewHolder extends RecyclerView.ViewHolder{
    TextView placeName;
    TextView category;
    TextView address;
    ImageView eateryImage;
    RatingBar ratingBar,priceRateBar;

    EateriesViewHolder(View itemView,OnItemClickListener listener){
        super(itemView);
        placeName = itemView.findViewById(R.id.placeName);
        category = itemView.findViewById(R.id.catagory);
        address = itemView.findViewById(R.id.address);
        eateryImage = itemView.findViewById(R.id.eateryImage);
        ratingBar = itemView.findViewById(R.id.ratingBar);
        priceRateBar = itemView.findViewById(R.id.priceRateBar);

    }
}

那么,如何为适配器设置不同的OnClickListener?非常感谢。

3 个答案:

答案 0 :(得分:1)

您可以将界面用于不同的点击监听器

您可以创建接口类

public interface ICallback {

public void onItemClick(int pos);

}

然后在适配器上调用接口类,然后单击项目单击

 holder.textview.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            iCallback.onItemClick(position);

        }

  });

活动或片段中的最终调用接口类之后

iCallback = new ICallback() {
        @Override
        public void onItemClick(int pos) {

              Intent intent = new Intent(HomeActivity.this,SecondActivity.class);
            startActivity(intent);
        }
    };

答案 1 :(得分:0)

i:第一种方式

  itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if([this method is Override method]getItemViewType(position) == VIEW_TYPE_USER){
                   // here USER logic 
                }else{
                  // here EATERIES logic
                }

            }
        });

II)第二种方式

    @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
            if(holder instanceof SearchingFragment.UserViewHolder){
                ((SearchingFragment.UserViewHolder) holder).userName.setText(userArrayList.get(position).getUsername());
                ((SearchingFragment.UserViewHolder) holder).category.setText("User");
                ((SearchingFragment.UserViewHolder) holder).userImage.setImageURI(null);
                if(userArrayList.get(position).getPhoto()!=null){
                    Uri uri=Uri.parse(userArrayList.get(position).getPhoto());
                    Glide.with(getContext()).load(uri).into(((SearchingFragment.UserViewHolder) holder).userImage);
                }

((SearchingFragment.UserViewHolder) holder).itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {


                }
            });

            }

            if(holder instanceof SearchingFragment.EateriesViewHolder){
                ((SearchingFragment.EateriesViewHolder) holder).placeName.setText(eateriesArrayList.get(position-userArrayList.size()).getPlaceName());
                ((SearchingFragment.EateriesViewHolder) holder).category.setText("Eatery\n"+eateriesArrayList.get(position-userArrayList.size()).getCategory());
                ((SearchingFragment.EateriesViewHolder) holder).address.setText(Html.fromHtml(eateriesArrayList.get(position-userArrayList.size()).getAddress()));
                ((EateriesViewHolder) holder).ratingBar.setRating(Float.parseFloat(eateriesArrayList.get(position-userArrayList.size()).getRating().toString()));
                ((EateriesViewHolder) holder).ratingBar.setIsIndicator(true);
                ((EateriesViewHolder) holder).priceRateBar.setRating(Float.parseFloat(eateriesArrayList.get(position-userArrayList.size()).getPrice_level().toString()));

                List<Place.Field> fields = Arrays.asList(Place.Field.PHOTO_METADATAS);
                FetchPlaceRequest placeRequest = FetchPlaceRequest.newInstance(eateriesArrayList.get(position-userArrayList.size()).getPlaceID(), fields);
                String api = "AIzaSyDz5BGny6Lsp7gW-uJznoLVZS4riEdfnF0";
                Places.initialize(getContext(), api);
                PlacesClient placesClient = Places.createClient(getContext());

                placesClient.fetchPlace(placeRequest).addOnSuccessListener((response) -> {
                    Place place = response.getPlace();
                    PhotoMetadata photoMetadata = place.getPhotoMetadatas().get(0);
                    String attributions = photoMetadata.getAttributions();

                    FetchPhotoRequest photoRequest = FetchPhotoRequest.builder(photoMetadata)
                            .build();
                    placesClient.fetchPhoto(photoRequest).addOnSuccessListener((fetchPhotoResponse) -> {
                        Bitmap bitmap = fetchPhotoResponse.getBitmap();
                        ((SearchingFragment.EateriesViewHolder) holder).eateryImage.setImageBitmap(bitmap);
                        // imageView.setImageBitmap(bitmap);
                    }).addOnFailureListener((exception) -> {
                        if (exception instanceof ApiException) {
                            ApiException apiException = (ApiException) exception;
                            int statusCode = apiException.getStatusCode();
                            // Handle error with given status code.
                            Log.e(TAG, "Place not found: " + exception.getMessage());
                        }
                    });
                });
               ((SearchingFragment.EateriesViewHolder) holder).itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {


                }
            });

            }

            }
        }

答案 2 :(得分:0)

按如下所示更新您的ViewHolders

private class UserViewHolder extends RecyclerView.ViewHolder {
        TextView userName;
        ImageView userImage;
        TextView category;

        UserViewHolder(View itemView) {
            super(itemView);
            userName = itemView.findViewById(R.id.userName);
            userImage = itemView.findViewById(R.id.userImage);
            category = itemView.findViewById(R.id.catagory);


            // set Click Listener of whole item view or your desired view
            itemView.setOnClickListener(view -> {

                User user = userArrayList.get(getAdapterPosition());
                // do whatever you want to
            });
        }

    }

    private class EateriesViewHolder extends RecyclerView.ViewHolder {
        TextView placeName;
        TextView category;
        TextView address;
        ImageView eateryImage;
        RatingBar ratingBar, priceRateBar;

        EateriesViewHolder(View itemView) {
            super(itemView);
            placeName = itemView.findViewById(R.id.placeName);
            category = itemView.findViewById(R.id.catagory);
            address = itemView.findViewById(R.id.address);
            eateryImage = itemView.findViewById(R.id.eateryImage);
            ratingBar = itemView.findViewById(R.id.ratingBar);
            priceRateBar = itemView.findViewById(R.id.priceRateBar);

            // set Click Listener of whole item view or your desired view
            itemView.setOnClickListener(view -> {

                Eateries2 Eateries2 = eateriesArrayList.get(getAdapterPosition());
                // do whatever you want to
            });

        }

    }