public class MainActivity extends AppCompatActivity { private PullLoadMoreRecyclerView recyclerView; private List<Pic.美女Bean> list = new ArrayList<>(); private MyAdapter adapter; private String path = "http://c.3g.163.com/recommend/getChanListNews?channel=T1456112189138&size=20&passport=&devId=1uuFYbybIU2oqSRGyFrjCw%3D%3D&lat=%2F%2FOm%2B%2F8ScD%2B9fX1D8bxYWg%3D%3D&lon=LY2l8sFCNzaGzqWEPPgmUw%3D%3D&version=9.0&net=wifi&ts=1464769308&sign=bOVsnQQ6gJamli6%2BfINh6fC%2Fi9ydsM5XXPKOGRto5G948ErR02zJ6%2FKXOnxX046I&encryption=1&canal=meizu_store2014_news&mac=sSduRYcChdp%2BBL1a9Xa%2F9TC0ruPUyXM4Jwce4E9oM30%3D"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); startTask(); recyclerView.setOnPullLoadMoreListener(new PullLoadMoreRecyclerView.PullLoadMoreListener() { @Override public void onRefresh() { new Handler().postDelayed(new Runnable() { @Override public void run() { startTask(); recyclerView.setPullLoadMoreCompleted(); } }, 1500); } @Override public void onLoadMore() { new Handler().postDelayed(new Runnable() { @Override public void run() { startTask(); adapter.notifyItemInserted(list.size() - 1); adapter.notifyDataSetChanged(); recyclerView.setIsLoadMore(false); recyclerView.setPullLoadMoreCompleted(); } }, 1000); } }); } private void initView() { recyclerView = (PullLoadMoreRecyclerView) findViewById(R.id.Rec); recyclerView.setGridLayout(3); adapter = new MyAdapter(list, MainActivity.this); recyclerView.setAdapter(adapter); } private void startTask() { OkhttpUtils.getInstance().getBeanOfOk(this, path, Pic.class, new OkhttpUtils.CallBack<Pic>() { @Override public void getData(Pic pic) { if (pic != null) { list.addAll(0, pic.get美女()); initView(); } } }); } }public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> { private List<Pic.美女Bean> list; private Context mcontext; private MyOnItemClickListener itemClickListener; private MyOnItemLongClickListener itemLongClickListener; public interface MyOnItemClickListener { void OnItemClickListener(View view, int position); } public interface MyOnItemLongClickListener { void OnItemLongClickListener(View view, int position); } public MyAdapter(MainActivity mcontext, List<Pic.美女Bean> list) { this.list = list; this.mcontext = mcontext; } @Override public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(mcontext).inflate(R.layout.item, null); ViewHolder holder = new ViewHolder(view); return holder; } @Override public void onBindViewHolder(final MyAdapter.ViewHolder holder, int position) { final Pic.美女Bean bean = list.get(position); /// Glide.with(mcontext).load(bean.getImgsrc()).into(holder.picImage); Picasso.with(mcontext).load(bean.getImgsrc()).placeholder(R.mipmap.ic_launcher).error(R.mipmap.ic_launcher).into(holder.picImage); if (itemClickListener != null) { holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { itemClickListener.OnItemClickListener(holder.itemView, holder.getLayoutPosition()); } }); if (itemLongClickListener != null) { holder.itemView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { itemLongClickListener.OnItemLongClickListener(holder.itemView, holder.getLayoutPosition()); return true; } }); } } holder.picImage.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(mcontext, bean.getTitle(), Toast.LENGTH_SHORT).show(); PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("alpha", 1f, 0f, 1f); PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("scaleX", 1f, 0, 1f); PropertyValuesHolder pvhZ = PropertyValuesHolder.ofFloat("scaleY", 1f, 0, 1f); ObjectAnimator.ofPropertyValuesHolder(v, pvhX, pvhY, pvhZ).setDuration(2000).start(); } }); } public void setOnItemClickListener(MyOnItemClickListener itemClickListener) { this.itemClickListener = itemClickListener; } public void setOnItemLongClickListener(MyOnItemLongClickListener itemLongClickListener) { this.itemLongClickListener = itemLongClickListener; } @Override public int getItemCount() { return list == null ? 0 : list.size(); } public static class ViewHolder extends RecyclerView.ViewHolder { private ImageView picImage; public ViewHolder(View itemView) { super(itemView); picImage = (ImageView) itemView.findViewById(R.id.pic_image); } } }
RecyclerView第三方开源
最新推荐文章于 2024-06-14 09:41:21 发布