package com.z.zmusicplayer.fragment.mainfragment; import android.support.v4.widget.SwipeRefreshLayout; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.widget.Toast; import com.z.zmusicplayer.R; import com.z.zmusicplayer.bean.VideoBean; import com.z.zmusicplayer.fragment.BaseFragment; import com.z.zmusicplayer.http.HttpManager; import com.z.zmusicplayer.http.MyHttpCallback; import com.z.zmusicplayer.utils.URLProviderUtil; import java.io.IOException; import java.util.ArrayList; import java.util.List; import okhttp3.Call; import okhttp3.Request; import okhttp3.Response; import static com.z.zmusicplayer.fragment.mainfragment.MainfragmentContract.*; /** * Created by z on 2017/6/25. */ public class MainFragment extends BaseFragment implements MainfragmentContract.View<MainfragmentContract.Presenter> { MainfragmentContract.Presenter presenter; RecyclerView recyclerView; SwipeRefreshLayout refresh; private MainFragmentAdapter adapter; private List<VideoBean> videoBeans = new ArrayList<>(); private boolean refreshing; private int lastItemIndex; private boolean hasMoreData; int startIndex; @Override protected int getLayoutId() { return R.layout.mainfragment; } @Override protected void init() { refresh= (SwipeRefreshLayout) rootView.findViewById(R.id.refresh); recyclerView= (RecyclerView) rootView.findViewById(R.id.recyclerview); // getPicSize(540,640); getPicSize(540,300); adapter= new MainFragmentAdapter(videoBeans,getActivity(),mWidth,mHeight); recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); recyclerView.setAdapter(adapter); refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { refreshing=true; presenter.getData(0,size); } }); recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrollStateChanged(RecyclerView recyclerView, int newState) { super.onScrollStateChanged(recyclerView, newState); if(lastItemIndex == videoBeans.size()-1&& hasMoreData){ presenter.getData(startIndex,size); } } @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); lastItemIndex = ((LinearLayoutManager) recyclerView.getLayoutManager()).findLastVisibleItemPosition(); } }); new MainFragmentPresenter(this); presenter.getData(startIndex,size); } @Override public void showData(List<VideoBean> videoBeanList) { if(refreshing){ //成员变量 videoBeans 集合 ; videoBeans.clear(); refreshing = false ; } if(videoBeanList.size()>0){ hasMoreData = true; }else { hasMoreData = false ; } startIndex = startIndex +videoBeanList.size(); videoBeans.addAll(videoBeanList); adapter.notifyDataSetChanged(); refresh.setRefreshing(false); } @Override public void showError(String errorMsg) { } @Override public void setPresenter(Presenter presenter) { this.presenter = presenter; } }
package com.z.zmusicplayer.fragment.mainfragment; import android.app.Activity; import android.support.v7.widget.RecyclerView; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.RelativeLayout; import android.widget.TextView; import com.bumptech.glide.Glide; import com.z.zmusicplayer.R; import com.z.zmusicplayer.bean.VideoBean; import java.util.List; /** * Created by z on 2017/7/2. */ public class MainFragmentAdapter extends RecyclerView.Adapter<MainFragmentAdapter.MyViewHolder> { private List<VideoBean> videoBeanList = null; private Activity activity; private LayoutInflater inflater; private int mWidth, mHeight; public MainFragmentAdapter(List<VideoBean> videoBeanList, Activity activity, int mWidth, int mHeight) { this.videoBeanList = videoBeanList; this.mWidth = mWidth; this.mHeight = mHeight; this.activity =activity; inflater = LayoutInflater.from(activity); } @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = inflater.inflate(R.layout.homepage_item, parent, false); return new MyViewHolder(view); } @Override public void onBindViewHolder(MyViewHolder holder, int position) { VideoBean videoBean = videoBeanList.get(position); ViewGroup.LayoutParams layoutParams = holder.ivContentimg.getLayoutParams(); layoutParams.width = mWidth; layoutParams.height = mHeight; holder.ivContentimg.setLayoutParams(layoutParams); holder.viewbg.setLayoutParams(layoutParams); holder.tvTitle.setText(videoBean.getTitle()); holder.tvDescription.setText(videoBean.getDescription()); String posterPic = videoBean.getPosterPic(); ImageView iv = holder.ivContentimg; if(activity !=null ){ Log.d("activity",activity.toString()); }else { Log.d("activity","不存在"); } Glide.with(activity).load(posterPic).into(iv); // holder.ivContentimg.setImageResource(R.mipmap.home_page_project); final int tag; String type = videoBean.getType(); if (type.equals("ACTIVITY")) { tag = 0; holder.ivType.setImageResource(R.mipmap.home_page_activity); } else if (type.equalsIgnoreCase("VIDEO")) { tag = 1; holder.ivType.setImageResource(R.mipmap.home_page_video); } else if (type.equalsIgnoreCase("WEEK_MAIN_STAR")) { tag = 2; holder.ivType.setImageResource(R.mipmap.home_page_star); } else if (type.equalsIgnoreCase("PLAYLIST")) { tag = 3; holder.ivType.setImageResource(R.mipmap.home_page_playlist); } else if (type.equalsIgnoreCase("AD")) { tag = 4; holder.ivType.setImageResource(R.mipmap.home_page_ad); } else if (type.equalsIgnoreCase("PROGRAM")) { tag = 5; holder.ivType.setImageResource(R.mipmap.home_page_program); } else if (type.equalsIgnoreCase("bulletin")) { tag = 6; holder.ivType.setImageResource(R.mipmap.home_page_bulletin); } else if (type.equalsIgnoreCase("fanart")) { tag = 7; holder.ivType.setImageResource(R.mipmap.home_page_fanart); } else if (type.equalsIgnoreCase("live")) { tag = 8; holder.ivType.setImageResource(R.mipmap.home_page_live); } else if (type.equalsIgnoreCase("LIVENEW") || type.equalsIgnoreCase("livenewlist")) { tag = 9; holder.ivType.setImageResource(R.mipmap.home_page_live_new); } else if (type.equalsIgnoreCase("LIVENTORY") || type.equalsIgnoreCase(videoBean.getType())) { tag = 10; holder.ivType.setImageResource(R.mipmap.home_page_project); } else { tag = -100; holder.ivType.setImageResource(0); } } @Override public int getItemCount() { return videoBeanList== null ?0:videoBeanList.size(); } public class MyViewHolder extends RecyclerView.ViewHolder { private ImageView ivitemLogo; private ImageView ivContentimg; private final View viewbg; private final TextView tvTitle; private final TextView tvDescription; private final RelativeLayout rlItemRootVIew; private final ImageView ivType; public MyViewHolder(View itemView) { super(itemView); ivitemLogo = (ImageView) itemView.findViewById(R.id.iv_item_logo); ivContentimg = (ImageView) itemView.findViewById(R.id.iv_contentimg); viewbg = itemView.findViewById(R.id.viewbg); tvDescription = (TextView) itemView.findViewById(R.id.tv_description); tvTitle = (TextView) itemView.findViewById(R.id.tv_title); rlItemRootVIew = (RelativeLayout) itemView.findViewById(R.id.rl_item_rootView); ivType = (ImageView) itemView.findViewById(R.id.iv_type); } } }
原文:http://www.cnblogs.com/Oldz/p/7108441.html