引入包
implementation 'com.squareup.okhttp3:okhttp:3.12.3'
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation 'com.github.bumptech.glide:glide:3.7.0'
权限
<uses-permission android:name="android.permission.INTERNET" />
MainActivity.java
public class MainActivity extends AppCompatActivity {
private List<Item> items=new ArrayList<>();
private ItemAdapter adapter;
private View view;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView rv=findViewById(R.id.rv);
LinearLayoutManager manager=new LinearLayoutManager(this);
// GridLayoutManager manager1=new GridLayoutManager(this,3);表格布局
manager.setOrientation(LinearLayoutManager.VERTICAL);
rv.setLayoutManager(manager);
adapter=new ItemAdapter(items,this);
rv.setAdapter(adapter);
OkHttpClient client=new OkHttpClient();
Request request=new Request.Builder()
.url("https://api.myjson.com/bins/apm1f")
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call,IOException e) {
Log.d("错误","onFailure:"+e.toString());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
try {
String data=response.body().string();
JSONArray jsonArray=new JSONArray(data);
for (int i=0;i<jsonArray.length();i++){
JSONObject jso=jsonArray.getJSONObject(i);
Item item=new Item();
item.setName(jso.getString("name"));
item.setImg(jso.getString("img"));
items.add(item);
}
runOnUiThread(new Runnable() {
@Override
public void run() {
adapter.notifyDataSetChanged();
}
});
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
public void bottom(View view){
this.view = view;
Toast.makeText(MainActivity.this,"查看详情",Toast.LENGTH_SHORT).show();
Intent intent=new Intent(MainActivity.this,Main2Activity.class);
startActivity(intent);}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<android.support.v7.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
item_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/imgTv"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="120dp" />
<TextView
android:textSize="24dp"
android:id="@+id/nameTv"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="50dp" />
<Button
android:onClick="bottom"
android:layout_weight="1"
android:text="查看详情"
android:layout_width="0dp"
android:layout_height="50dp" />
</LinearLayout>
Item.java
public class Item {
private String name;
private String img;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getImg() {
return img;
}
public void setImg(String img) {
this.img = img;
}
}
ItemHoder.java
public class ItemHoder extends RecyclerView.ViewHolder {
TextView nameTv;
ImageView imgTv;
public ItemHoder( View itemView) {
super(itemView);
nameTv=itemView.findViewById(R.id.nameTv);
imgTv=itemView.findViewById(R.id.imgTv);
}
}
ItemAdapter.java
public class ItemAdapter extends RecyclerView.Adapter<ItemHoder> {
private List<Item> items;
private Context context;
public ItemAdapter(List<Item> items,Context context){
this.items=items;
this.context=context;
}
@Override
public ItemHoder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view= LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_layout,viewGroup,false);
return new ItemHoder(view);
}
@Override
public void onBindViewHolder(ItemHoder itemHoder, int position) {
itemHoder.nameTv.setText(items.get(position).getName());
Glide.with(context).load(items.get(position).getImg()).into(itemHoder.imgTv);
}
@Override
public int getItemCount() {
return items.size();
}
}
Main2Activity.java
public class Main2Activity extends AppCompatActivity {
private TextView textView;
private Button bu1;
private Button bu2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
textView=findViewById(text);bu2=findViewById(R.id.bu2);
bu1=findViewById(R.id.bu1);
bu2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(Main2Activity.this,MainActivity.class);
startActivity(intent);
}
});
}
public void but1(View view) {
Toast.makeText(Main2Activity.this,"购买",Toast.LENGTH_SHORT).show();
Intent intent=new Intent(Main2Activity.this,Main3Activity.class);
startActivity(intent);
}
}
activity_main2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Main2Activity" android:orientation="vertical">
<TextView
android:textSize="30dp"
android:text="商品详情"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:src="@drawable/one"/>
<TextView
android:id="@+id/text"
android:text="促销价:¥129.90"
android:textSize="25dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:textSize="20dp"
android:text="商品名称:零食大礼包 商品编号:6401663
商品产地:中国大陆 类别:其它国产/进口国产"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:onClick="but1"
android:id="@+id/bu1"
android:layout_marginTop="20dp"
android:layout_gravity="center"
android:text="购买"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/bu2"
android:layout_gravity="center"
android:text="返回"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Main3Activity.java
public class Main3Activity extends AppCompatActivity {
private Button b3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
b3=findViewById(R.id.b3);
b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(Main3Activity.this,Main2Activity.class);
startActivity(intent);
}
});
}
}
activity_main3.xml
<TextView
android:textSize="35dp"
android:text="请用微信扫码支付"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:layout_marginLeft="50dp"
android:layout_marginTop="20dp"
android:src="@drawable/tt"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/b3"
android:text="返回"
android:layout_marginRight="30dp"
android:layout_gravity="right"
android:layout_marginTop="50dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />