我尝试了许多选项和解决方案,但仍然无法在ViewHolder上执行Click事件。即viewHolder.mView.setOnClickListener
不能分段工作。我在活动中使用了相同的代码,但效果很好,但在Fragment中却没有。
等级依赖:
implementation 'com.android.support:recyclerview-v7:26.1.0' <br>
implementation 'com.firebaseui:firebase-ui-database:2.3.0'
代码:
public class OffersFragment extends Fragment {
private View mMainView;
private RecyclerView mBusinessOffersList;
private RecyclerView.LayoutManager mLayoutManager;
public OffersFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
mMainView = inflater.inflate(R.layout.fragment_offers, container, false);
//------Recyclerview to show Business Gallery ------------//
mBusinessOffersList = mMainView.findViewById(R.id.businessOffersList);
mBusinessOffersList.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(getActivity());
mBusinessOffersList.setLayoutManager(mLayoutManager);
.
.
.
if(currentUser!=null && mBusinessUserID != null){
mdatabaseOffers = FirebaseDatabase.getInstance().getReference().child("Offers")
.child(BusinessCity).child(mBusinessUserID);
}
return mMainView;
}
@Override
public void onStart() {
super.onStart();
FirebaseRecyclerAdapter <EshopOffers, BusinessOffersViewHolder> mfirebaseRecycleAdapter = new FirebaseRecyclerAdapter<EshopOffers, BusinessOffersViewHolder>
( EshopOffers.class,
R.layout.shop_offerslist_layout,
BusinessOffersViewHolder.class,
mdatabaseOffers) {
@Override
protected void populateViewHolder(final BusinessOffersViewHolder viewHolder, EshopOffers model, int position) {
//---tried--Method 1: Open new Activity on CLick----but no luck--------//
viewHolder.mView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getActivity(), "Item Clicked", Toast.LENGTH_LONG).show();
}
});
}
};mBusinessOffersList.setAdapter(mfirebaseRecycleAdapter);
}
public static class BusinessOffersViewHolder extends RecyclerView.ViewHolder {
View mView;
public LinearLayout offerLayout;
ImageView offerImage1;
LinearLayout offerLayout2;
// public RelativeLayout offerRLlayout;
public BusinessOffersViewHolder(View itemView) {
super(itemView);
mView = itemView;
//---tried--Method 2: Open new Activity on CLick----but no luck--------//
itemView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Log.i("View Clicked", "View Clicked");
}
});
}
}
}
我尝试了RecyclerView onClick和how to implement a SetOnItemClickListener FirebaseRecyclerViewAdapter的大多数解决方案,但是没有运气。 :(
我已经挠2了将近2天3个晚上,但仍然无处可去... :(所以现在需要您的帮助..!