1.简介:CardView是用于实现卡片布局效果的重要控件,由appcompat-v7库提供,实际上CardView也是一个Fram
eLayout,只是额外提供了圆角和阴影等效果,看上去由立体感。
*app:cardCornerRadius属性制定卡片圆角的弧度。
*app:elevation属性制定卡片的高度。即投影范围。
*AppBarLayout:实际上是一个垂直方向上的LinearLayout,在内部做了很多滚动事件的封装,通过它可以解决Recycl
e覆盖Toolbar的问题。
解决步骤:1.将Toolbar嵌套到AppBarLayout中
2.给RecyclerView制定一个布局行为。app:layout_behavior="@string/appbar_scrolling_view_behavior"
可以实现的效果:在嵌套的Toolbar中设置:app:layout_scrollFlags="scroll|enterAlways|snap";scroll表示RecycleView
向上滑动的时候,Toolbar会跟着一起向上滚动并实现隐藏,enterAlways表示RecycleView向下滚动的时候,Toolbar
会跟着一起向下滚动并重新显示。snap表示当Toolbar还没有完全隐藏或者显示的时候,会根据当前滚动的距离,自
动选择显示还是隐藏。
2.添加依赖
implementation 'com.android.support:cardview-v7:27.1.1'
3.fruit_item.xml布局
4.主布局
5.FruitAdapter和实体类
public class Fruit {
private String name;
private int imageId;
public Fruit(String name, int imageId) {
this.name = name;
this.imageId = imageId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getImageId() {
return imageId;
}
public void setImageId(int imageId) {
this.imageId = imageId;
}
}
public class FruitAdapter extends RecyclerView.Adapter{
private Context mContext;
private List mFruitList;
public FruitAdapter(List fruitList){
mFruitList=fruitList;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
if (mContext==null){
mContext=parent.getContext();
}
View view=LayoutInflater.from(mContext).inflate(R.layout.fruit_item,
parent,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
Fruit fruit=mFruitList.get(position);
holder.fruitName.setText(fruit.getName());
Glide.with(mContext).load(fruit.getImageId()).into(holder.fruitImage);
}
@Override
public int getItemCount() {
return mFruitList.size();
}
static class ViewHolder extends RecyclerView.ViewHolder{
CardView cardView;
ImageView fruitImage;
TextView fruitName;
public ViewHolder(View itemView) {
super(itemView);
cardView= (CardView) itemView;
fruitImage=itemView.findViewById(R.id.fruit_image);
fruitName=itemView.findViewById(R.id.fruit_name);
}
}
}
6.代码
public class CardViewActiivity extends AppCompatActivity {
private RecyclerView recyclerView;
private DrawerLayout mDrawerLayout;
private Fruit[] fruits={new Fruit("Apple",R.drawable.mm),
new Fruit("Banana",R.drawable.mm),new Fruit("Orange",R.drawable.mm),
new Fruit("Watermelon",R.drawable.mm),new Fruit("Pear",R.drawable.mm),
new Fruit("Grape",R.drawable.mm),new Fruit("Pineapple",R.drawable.mm),
new Fruit("Strawerry",R.drawable.mm),new Fruit("Cherry",R.drawable.mm),
new Fruit("Mango",R.drawable.mm)};
private List fruitList=new ArrayList<>();
private FruitAdapter adapter;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.navigationview_activity);
recyclerView=findViewById(R.id.recycle_view);
initFruit();
GridLayoutManager layoutManager=new GridLayoutManager(this,2);
recyclerView.setLayoutManager(layoutManager);
adapter=new FruitAdapter(fruitList);
recyclerView.setAdapter(adapter);
}
private void initFruit(){
fruitList.clear();
for (int i=0;i<50;i++){
Random random=new Random();
int index=random.nextInt(fruits.length);
fruitList.add(fruits[index]);
}
}
}