这是我在
中出错的代码Picasso.with(ThreeFragement.this).load(model.getMoviePoster()).into(viewHolder.ivMoviePoster);
即使我将ThreeFragment
设置为getContext,它也无效。
整个代码写在下面我要在RecyclerView
中实现Picasso到Firebase Fragment
并在其上检索图像。
这只是Activity
上实施的一个教程,我尝试将其放入Fragment
。
package com.tabs.ayushmaniac.letsmanageit;
import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import com.tabs.ayushmaniac.letsmanageit.fragment.AddMovieFragment;
import com.tabs.ayushmaniac.letsmanageit.models.Movie;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.squareup.picasso.Picasso;
public class ThreeFragment extends Fragment {
private FloatingActionButton fab;
ScaleAnimation shrinkAnim;
private RecyclerView mRecyclerView;
private StaggeredGridLayoutManager mLayoutManager;
private TextView tvNoMovies;
//Getting reference to Firebase Database
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference mDatabaseReference = database.getReference();
private static final String USER_ID = "53";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_three, container, false);
return rootView;
//Initializing our Recyclerview
mRecyclerView = (RecyclerView)getView().findViewById(R.id.my_recycler_view);
tvNoMovies = (TextView)getView().findViewById(R.id.tv_no_movies);
//scale animation to shrink floating actionbar
shrinkAnim = new ScaleAnimation(1.15f, 0f, 1.15f, 0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
if (mRecyclerView != null) {
//to enable optimization of recyclerview
mRecyclerView.setHasFixedSize(true);
}
//using staggered grid pattern in recyclerview
mLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(mLayoutManager);
//Say Hello to our new Firebase UI Element, i.e., FirebaseRecyclerAdapter
final FirebaseRecyclerAdapter<Movie,MovieViewHolder> adapter = new FirebaseRecyclerAdapter<Movie, MovieViewHolder>(
Movie.class,
R.layout.movie_board_item,
MovieViewHolder.class,
//referencing the node where we want the database to store the data from our Object
mDatabaseReference.child("users").child(USER_ID).child("movies").getRef()
) {
@Override
protected void populateViewHolder(MovieViewHolder viewHolder, Movie model, int position) {
if(tvNoMovies.getVisibility()== View.VISIBLE){
tvNoMovies.setVisibility(View.GONE);
}
viewHolder.tvMovieName.setText(model.getMovieName());
viewHolder.ratingBar.setRating(model.getMovieRating());
Picasso.with(this).load(model.getMoviePoster()).into(viewHolder.ivMoviePoster);
}
};
mRecyclerView.setAdapter(adapter);
fab = (FloatingActionButton)getView().findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
getFragmentManager().beginTransaction()
.replace(R.id.frame_container, new AddMovieFragment())
.addToBackStack(null)
.commit();
//animation being used to make floating actionbar disappear
shrinkAnim.setDuration(400);
fab.setAnimation(shrinkAnim);
shrinkAnim.start();
shrinkAnim.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
//changing floating actionbar visibility to gone on animation end
fab.setVisibility(View.GONE);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
}
});
}
public static class MovieViewHolder extends RecyclerView.ViewHolder{
TextView tvMovieName;
RatingBar ratingBar;
ImageView ivMoviePoster;
public MovieViewHolder(View v) {
super(v);
tvMovieName = (TextView) v.findViewById(R.id.tv_name);
ratingBar = (RatingBar) v.findViewById(R.id.rating_bar);
ivMoviePoster = (ImageView) v.findViewById(R.id.iv_movie_poster);
}
}
}
答案 0 :(得分:1)
使用getActivity()
代替ThreeFragement.this
尝试以下代码;
Picasso.with(getActivity()).load(model.getMoviePoster()).into(viewHolder.ivMoviePoster);
答案 1 :(得分:1)
试试这个
Picasso.with(context).load(model.getMoviePoster()).into(viewHolder.ivMoviePoster);
而不是
Picasso.with(ThreeFragement.this).load(model.getMoviePoster()).into(viewHolder.ivMoviePoster);
答案 2 :(得分:0)
您可以使用getActivity()
,它会返回与片段相关联的activity
。
所以替换代码
Picasso.with(ThreeFragement.this).load(model.getMoviePoster()).into(viewHolder.ivMoviePoster);
<强>与强>
Picasso.with(getActivity()).load(model.getMoviePoster()).into(viewHolder.ivMoviePoster);