将RecycleView加载到Fragment Android中

时间:2016-11-17 00:39:43

标签: android android-recyclerview fragment

我试图将循环视图加载到片段中,问题是循环视图不会出现,除非我按下主页按钮并返回应用程序。

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_profile_, container, false);

    username = (TextView) view.findViewById(R.id.username);
    profile_picture =(CircleImageView)view.findViewById(R.id.profile_picture);
  gridview = (RecyclerView) view.findViewById(R.id.grid_videos);

    return view;
}


private void setupRecyclerView(RecyclerView recyclerView) {
    recyclerView.setLayoutManager(new LinearLayoutManager(recyclerView.getContext()));
    recyclerView.setAdapter(new RecycleView_videos(getActivity(),R.layout.grid_item,v));

}

@Override
public void onStart() {
    super.onStart();
    setupRecyclerView(gridview);


}

public class RecycleView_videos extends RecyclerView.Adapter<RecycleView_videos.RecycleView_Holder> {

    Context context;
    int ressource;
    ArrayList<Videos> videoList ;


    public RecycleView_videos(Context context, int ressource, ArrayList<Videos> videoList) {
        this.context = context;
        this.ressource = ressource;
        this.videoList = videoList;
    }

    @Override
    public RecycleView_Holder onCreateViewHolder(ViewGroup parent, int viewType) {

        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.grid_item, parent, false);
        // set the view's size, margins, paddings and layout parameters

        RecycleView_Holder vh = new RecycleView_Holder(v);
        return vh;


    }

    @Override
    public void onBindViewHolder(RecycleView_Holder holder, int position) {
        final Videos video = videoList.get(position);
        holder.video_player.setVideoURI(Uri.parse(video.getURL()));
        holder.video_player.requestFocus();
        holder.video_player.start();

    }

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

    class RecycleView_Holder extends RecyclerView.ViewHolder {
        TextView tv_nom;
        VideoView video_player ;

        public RecycleView_Holder(View itemView) {
            super(itemView);

            video_player = (VideoView) itemView.findViewById(R.id.video_player);

        }
    }

}

1 个答案:

答案 0 :(得分:1)

调用setupRecyclerView(gridview);在函数onActivityCreated()