Android项目学习笔记-商城APP

项目学习视频

项目结构

建立以下包:

  • Activity
  • Adapter
  • Domain
  • Helper

xml常用属性

android:layout_

  • width
  • height
  • marginStart
  • marginEnd

android:text

  • text
  • textColor
  • textSize
  • textAlignment=”center”
  • textStyle=”bold”

layout

  • gravity=”center”

界面相关

按钮边框

新建drawable类型的xml。

1
2
3
4
5
6
<item>
<shape android:shape="rectangle">
<stroke android:color="@color/red" android:with="1dp" />
<corners android:radius="100dp"/>
</shape>
</item>

相关知识

shape图形的三个节点:

  • corners 圆角
  • solid 填充
  • stroke 描边

layout

当在垂直布局使用layout_weight属性时,应设置 layout_height="0dp"

viewBinding

配置

在bulid.gradle中配置。

创建

1
2
3
4
xxxBinding binding;

binding = xxxBing.inflate(getLayoutInflater());
setContentView(binding.getRoot());

其中,xxx由根据xml确定,如 IntroActivity对应xml文件为activity_intro,则xxx为ActivityIntor。(及对应Activity命名反过来)。

常用操作

改变状态栏颜色

1
getWindow().setStatusBarColor(Color.parseColor("#FFE4B5"));

BaseActivity

自定义的基础活动,可由其他界面继承。

消息提示

  • Log.i(TAG, “..”);
  • Toast.makeText(xxxActivity.this, “text”, Toast.LENGTH_SHORT).show();

初始化技巧

创建 setVariable()函数。在其中进行初始化。

按钮点击事件

1
2
3
4
5
private void setVariable() {
binding.loginBtn.setOnClickListener(v->{

})
}

活动间传递消息

发送消息:

1
2
3
4
5
holder.itemView.setOnClickListener(v->{
Intent intent = new Intent(context, ListFoodsActivity.class);
intent.putExtra("CategoryId", items.get(position).getId());
context.startActivity(intent);
})

接受信息:

1
2
3
private void getIntentExtra() {
categoryId = getIntent().getIntExtra("CategoryId");
}

Adapter

RecyclerView.Adapter

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
public class BestFoodsAdapter extends RecyclerView.Adapter<BestFoodsAdapter.viewholder> {
ArrayLiset<Foods> items;
Context context;

public BestFoodsAdapter(ArrayList<Foods> items) {
this.items = items;
}

@NonNull
@Override
public BestFoodsAdapter.viewholder onCeateViewHolder(@NonNull ViewGroup parent, int viewType) {
context = parent.getContext();
View inflate = LayoutInflater.from(context).inflate(R.layout.viewholder_best_deal,parent,false);
return new viewholder(inflate);
}

@Override
public void onBindViewHolder(@NonNull BestFoodsAdapter.viewholder holder, int position) {
holder.titleTxt.setText(items.get(position).getTitle());
...

Glide.with(context)
.load(items.get(position).getImagePath())
.transform(new CenterCrop(), new RoundedCorners(30))
.into(holder.pic);
}

public int getItemCount() {
return items.size();
}

public class viewholder extends RecyclerView.ViewHolder {
TextView titleTxt,...;

public viewholder(@NonNull View itemView) {
super(itemView);
titleTxt = itemView.findViewById(R.id.titleTxt);
...
}
}
}
1
2
3
RecyclerView.Adapter adapter = new BestFoodsAdapter(list);

binding.bestfoodView.setAdapter(adapter);

ArrayAdapter

1
2
3
ArrayAdapter<Location> adapter = new ArrayAdapter<>(MainActivity.this, R.layout.sp_item, list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dorpdown_item);
binding.locationSp.setAdapter(adapter);

界面跳转

1
startActivity(new Intent(CurentActivity.this, DestinationActivity.class));

gson

build.gradle(app)中的dependencies中导入。