记得加网络权限
java包:
//
compile 'jp.wasabeef:glide-transformations:3.0.1' implementation 'com.squareup.retrofit2:converter-gson:2.3.0' implementation 'com.squareup.okhttp3:okhttp:3.9.0' implementation 'com.mcxiaoke.next:recycler:1.5.0' implementation 'com.github.bumptech.glide:glide:3.7.0'
Main:
main:
public class MainActivity extends AppCompatActivity { private RecyclerView mRecycle_View;
private Adapter_OkHttp mAdapter_okHttp; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
Retrofit builder = new Retrofit.Builder()
.baseUrl("http://www.qubaobei.com/")
.addConverterFactory(GsonConverterFactory.create())
.build();
Call<Bean> okhttp = builder.create(OkHttp_zhoukao.class).getOkhttp();
okhttp.enqueue(new Callback<Bean>() {
@Override
public void onResponse(Call<Bean> call, Response<Bean> response) {
final ArrayList<Bean.DataBean> data = (ArrayList<Bean.DataBean>) response.body().getData(); mAdapter_okHttp = new Adapter_OkHttp(data,MainActivity.this); StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(4,StaggeredGridLayoutManager.HORIZONTAL); mRecycle_View.setAdapter(mAdapter_okHttp); mRecycle_View.setLayoutManager(layoutManager); mAdapter_okHttp.setOnItemListner(new Adapter_OkHttp.OnItemListner() {
@Override
public void onClick(int position) {
Intent intent = new Intent(MainActivity.this,Main2Activity.class);
intent.putExtra("Pic",data.get(position).getPic());
intent.putExtra("Title",data.get(position).getTitle());
intent.putExtra("Collect_num",data.get(position).getCollect_num());
intent.putExtra("Food_str",data.get(position).getFood_str());
startActivity(intent); }
}); } @Override
public void onFailure(Call<Bean> call, Throwable t) {
Toast.makeText(MainActivity.this, "请求失败", Toast.LENGTH_SHORT).show();
}
});
} private void initView() {
mRecycle_View = (RecyclerView) findViewById(R.id.mRecycle_View);
}
}
Main2: public class Main2Activity extends AppCompatActivity { private ImageView img;
private TextView mTitle;
private TextView mCollect_num;
private TextView mFood_str; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
initView();
//接收的值
String Pic = getIntent().getStringExtra("Pic");
String Title = getIntent().getStringExtra("Title");
String Collect_num = getIntent().getStringExtra("Collect_num");
String Food_str = getIntent().getStringExtra("Food_str"); Glide.with(this).load(Pic)
.apply(RequestOptions.bitmapTransform(new CropCircleTransformation()))
.into(img);
mTitle.setText(Title);
mCollect_num.setText(Collect_num);
mFood_str.setText(Food_str);
Log.e("Pic",mTitle.toString());
Log.e("Pic",mCollect_num.toString());
Log.e("Pic",mFood_str.toString());
} private void initView() {
img = (ImageView) findViewById(R.id.img);
mTitle = (TextView) findViewById(R.id.Title);
mCollect_num = (TextView) findViewById(R.id.Collect_num);
mFood_str = (TextView) findViewById(R.id.Food_str);
}
}
Adapter:
public class Adapter_OkHttp extends RecyclerView.Adapter<Adapter_OkHttp.ViewHodle> {
private ArrayList<Bean.DataBean>mList;
private Context mContext; public Adapter_OkHttp(ArrayList<Bean.DataBean> list, Context context) {
mList = list;
mContext = context;
} @Override
public ViewHodle onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.item,null);
ViewHodle viewHodle = new ViewHodle(view);
return viewHodle;
} @Override
public void onBindViewHolder(ViewHodle holder, int position) {
holder.mTextView1.setText(mList.get(position).getTitle());
holder.mTextView2.setText(mList.get(position).getCollect_num());
holder.mTextView3.setText(mList.get(position).getCollect_num());
Glide.with(mContext).load(mList.get(position).getPic())
.apply(RequestOptions.bitmapTransform(new CropCircleTransformation()))
.into(holder.mImageView); } @Override
public int getItemCount() {
return mList == null ? 0 :mList.size();
} public class ViewHodle extends RecyclerView.ViewHolder {
private ImageView mImageView;
private TextView mTextView1,mTextView2,mTextView3; public ViewHodle(View itemView) {
super(itemView);
mImageView = itemView.findViewById(R.id.mImage);
mTextView1 =itemView.findViewById(R.id.title);
mTextView2 = itemView.findViewById(R.id.title2);
mTextView3 = itemView.findViewById(R.id.title3);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mOnItemListner !=null){
mOnItemListner.onClick(getPosition()); }
}
}); }
}
public interface OnItemListner{
void onClick(int position); }
public OnItemListner mOnItemListner;
public void setOnItemListner(OnItemListner mOnItemListner){ this.mOnItemListner = mOnItemListner;
} }
OkHttp:
public interface OkHttp_zhoukao {
@GET("ios/cf/dish_list.php?stage_id=1&limit=30&page=1")
Call<Bean> getOkhttp(); }
记得加