android作业,整了半天才整出来,还得继续练啊。里面运用了LinearLayout RelativeLayout,RadioButton的样式设置,获取check
请看效果图:
代码如下:
//自定义商品类
public class Product {
private String name;
private int photoId;
private String title;
private String price;
public Product(String name, int photoId, String title, String price) {
this.name = name;
this.photoId = photoId;
this.title = title;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPhotoId() {
return photoId;
}
public void setPhotoId(int photoId) {
this.photoId = photoId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
}
//主页面布局
```go
<?xml version="1.0" encoding="utf-8"?>
<ListView android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/iv_products"
xmlns:android="http://schemas.android.com/apk/res/android" />
//自定义item布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/btu1"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:buttonTint="#BF3E30"/>
<ImageView
android:id="@+id/photo1"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_toRightOf="@+id/btu1"
android:layout_centerVertical="true"
android:src="@drawable/house" />
<TextView
android:id="@+id/iv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/photo1"
android:layout_centerVertical="true"
android:textSize="20sp"
android:layout_toEndOf="@+id/photo1" />
<ImageView
android:id="@+id/more"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/iv_name"
android:layout_centerVertical="true"
android:src="@drawable/more" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="领券"
android:textSize="15sp" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp">
<RadioButton
android:layout_gravity="center_vertical"
android:id="@+id/btu2"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="20dp"
android:checked="false"
android:textColor="#EF2929"
android:buttonTint="#BF3E30"/>
<ImageView
android:id="@+id/iv_photo"
android:layout_width="100dp"
android:layout_height="100dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/iv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="13sp"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/yuan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:src="@drawable/yuan" />
<TextView
android:id="@+id/iv_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/yuan"
android:layout_alignParentBottom="true"
android:textColor="#EF2929" />
<TextView
android:id="@+id/iv_reduce"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_toLeftOf="@+id/iv_num"
android:background="@drawable/border"
android:text="-"
android:textAlignment="center"
android:textStyle="bold"
tools:ignore="RtlCompat"
android:layout_alignParentBottom="true"/>
<TextView
android:id="@+id/iv_num"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="bottom"
android:layout_toLeftOf="@+id/iv_increase"
android:background="@drawable/border"
android:text="1"
android:textAlignment="center"
android:textStyle="bold"
tools:ignore="RtlCompat"
android:layout_alignParentBottom="true"/>
<TextView
android:id="@+id/iv_increase"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="bottom"
android:background="@drawable/border"
android:text="+"
android:textAlignment="center"
android:textStyle="bold"
tools:ignore="RtlCompat"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="15dp"/>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
//MainActivity类
```java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
List Products = new ArrayList();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//准备数据
initDate();
//定义item布局文件(自定义)
//创建Adapter,绑定Adapter
CustomAdapter customAdapter = new CustomAdapter(this, Products, R.layout.product_list_item);
ListView proListView = findViewById(R.id.iv_products);
proListView.setAdapter(customAdapter);
}
public void initDate() {
Product p1 = new Product("华为旗舰店", R.drawable.phone, "正品HuaWei/华为Mate9全网通4G双卡双待指纹卡双摄像智能4G手机", "2000.0");
Product p2 = new Product("创意良品", R.drawable.charger, "iWALK三合一充电线移动电源一拖二可爱迷你充电宝苹果安卓充电线", "88.0");
Products.add(p1);
Products.add(p2);
}
}
//自定义Adapter
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class CustomAdapter extends BaseAdapter {
private Context mContext;
private List products = new ArrayList<>();
private int itemLayoutRes;
public CustomAdapter(Context mContext, List products, int itemLayoutRes) {
this.mContext = mContext;
this.products = products;
this.itemLayoutRes = itemLayoutRes;
}
public Context getmContext() {
return mContext;
}
public void setmContext(Context mContext) {
this.mContext = mContext;
}
public List getProducts() {
return products;
}
public void setProducts(List products) {
this.products = products;
}
public int getItemLayoutRes() {
return itemLayoutRes;
}
public void setItemLayoutRes(int itemLayoutRes) {
this.itemLayoutRes = itemLayoutRes;
}
@Override
public int getCount() {
if (null != products) {
return products.size();
}
return 0;
}
@Override
public Object getItem(int position) {
if (null != products) {
return products.get(position);
}
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(itemLayoutRes, null);
//获取item控件的引用
TextView ivName = convertView.findViewById(R.id.iv_name);
ImageView ivPhoto = convertView.findViewById(R.id.iv_photo);
TextView ivTitle = convertView.findViewById(R.id.iv_title);
TextView ivPrice = convertView.findViewById(R.id.iv_price);
TextView ivIncrease = convertView.findViewById(R.id.iv_increase);
final TextView ivSum = convertView.findViewById(R.id.iv_num);
TextView ivReduce = convertView.findViewById(R.id.iv_reduce);
final RadioButton btu1 = convertView.findViewById(R.id.btu1);
final RadioButton btu2 = convertView.findViewById(R.id.btu2);
//设置控件内容
ivPhoto.setImageResource(products.get(position).getPhotoId());
ivName.setText(products.get(position).getName());
ivTitle.setText(products.get(position).getTitle());
ivPrice.setText(products.get(position).getPrice());
btu1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (btu2.isChecked()) {
btu1.setChecked(false);
btu2.setChecked(false);
} else {
btu1.setChecked(true);
btu2.setChecked(true);
}
}
});
btu2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (btu1.isChecked()) {
btu1.setChecked(false);
btu2.setChecked(false);
} else {
btu1.setChecked(true);
btu2.setChecked(true);
}
}
});
ivIncrease.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int i = Integer.valueOf(ivSum.getText().toString());
String j = i + 1 + "";
ivSum.setText(j);
}
});
ivReduce.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int i = Integer.valueOf(ivSum.getText().toString());
if (i == 1) {
ivSum.setText("1");
} else if (i > 1) {
String j = i - 1 + "";
ivSum.setText(j);
}
}
});
return convertView;
}
}