Issue
I have an Recycler.Adapter and my onBindViewHolder
is like this:
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
final String url = urls.get(position);
final width_height wh = whs.get(position);
holder.imageView.setClickable(true);
Picasso.with(context)
.load(url).resize(wh.width, wh.height).centerCrop()
.into(holder.imageView);
holder.imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialogFragment.show(fragmentManager, "");
dialogFragment.setImage(url);
dialogFragment.setRating(0);
}
});
holder.textView.setText(position + "");
holder.textView.setClickable(true);
holder.textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialogFragment.show(fragmentManager, "");
dialogFragment.setImage(url);
dialogFragment.setRating(0);
}
});
}
The dialogFragment is already instantiated in the contructor of Adapter. However, its onCreateView has not been called yet when the holder is clicked. And the setImage
and setRating
need to modify its imageView and RatingBar, which are null unless the onCreateView of the fragment is called. Is there a way to resolve this?
Solution
That's because DialogFragment.show
commits asynchronously. In your case you'd want it to use DialogFragment.showNow
instead so it commits synchronously.
@Override
public void onClick(View v) {
dialogFragment.showNow(fragmentManager, "");
dialogFragment.setImage(url);
dialogFragment.setRating(0);
}
Otherwise I'd suggest to create new DialogFragment
every time the button is clicked, and pass image url and rating as arguments before it is shown.
Answered By - Aaron
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.