package com.example.liyan.recyclerview2; / import android.os.Bundle; import android.os.Handler; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import com.example.liyan.bean.Bean; import com.example.liyan.recyclerview2.view.PullBaseView; import com.example.liyan.recyclerview2.view.PullRecyclerView; import com.example.liyan.url.OkHttp; import com.google.gson.Gson; import com.nostra13.universalimageloader.core.ImageLoader; import com.nostra13.universalimageloader.core.ImageLoaderConfiguration; import java.io.IOException; import java.util.List; import okhttp3.Request; public class MainActivity extends AppCompatActivity implements PullRecyclerView.OnHeaderRefreshListener,PullRecyclerView.OnFooterRefreshListener{ PullRecyclerView mRecyclerView; HomeAdapter mAdapter; String path = "http://m.yunifang.com/yunifang/mobile/goods/getall?random=39986&encode=2092d7eb33e8ea0a7a2113f2d9886c90&category_id=17"; private List<Bean.DataBean> data; ImageLoader loader; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); loader=ImageLoader.getInstance(); loader.init(ImageLoaderConfiguration.createDefault(MainActivity.this)); mRecyclerView = (PullRecyclerView) findViewById(R.id.id_recyclerview); mRecyclerView.setOnHeaderRefreshListener(this);//设置下拉监听 mRecyclerView.setOnFooterRefreshListener(this);//设置上拉监听 mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); in(); } //网络请求 private void in() { OkHttp.getAsync(path, new OkHttp.DataCallBack() { @Override public void requestFailure(Request request, IOException e) { } @Override public void requestSuccess(String result) throws Exception { Gson mGson = new Gson(); Bean bean = mGson.fromJson(result, Bean.class); data = bean.getData(); mRecyclerView.setAdapter(mAdapter = new HomeAdapter()); } }); }
//上拉加载 @Override public void onFooterRefresh(PullBaseView view) { new Handler().postDelayed(new Runnable() { @Override public void run() { // mDatas.add("TEXT更多"); mAdapter.notifyDataSetChanged(); mRecyclerView.onFooterRefreshComplete(); } }, 2000); } //下拉刷新 @Override public void onHeaderRefresh(PullBaseView view) { new Handler().postDelayed(new Runnable() { @Override public void run() { // mDatas.add(0, "TEXT新增"); mAdapter.notifyDataSetChanged(); mRecyclerView.onHeaderRefreshComplete(); } }, 3000); }
//适配器 class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.MyViewHolder> { @Override public HomeAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { MyViewHolder holder = new MyViewHolder(LayoutInflater.from( MainActivity.this).inflate(R.layout.item, parent, false)); return holder; } @Override public void onBindViewHolder(HomeAdapter.MyViewHolder holder, int position) { holder.te.setText(data.get(position).getGoods_name()); loader.displayImage(data.get(position).getGoods_img(),holder.im); } @Override public int getItemCount() { return data.size(); } public class MyViewHolder extends RecyclerView.ViewHolder { TextView te; ImageView im; public MyViewHolder(View itemView) { super(itemView); te = (TextView) itemView.findViewById(R.id.textView); im = (ImageView) itemView.findViewById(R.id.im); } } } }
RecyclerView的使用
最新推荐文章于 2022-09-27 23:00:00 发布