Android【CardView,DrawerLayout 滑动菜单,Fragment】
CardView的基本使用CardView是用于实现卡片式布局效果的重要控件,实际上也是一个frameLayout,只是额外提供了圆角和阴影,看上去有立体效果。
·
目录
3.3 Fragment的动态使用(CardView与RecycleView的结合使用)
1 CardView
1.1 CardView的基本使用
CardView 是用于实现卡片式布局效果的重要控件,实际上也是一个 frameLayout, 只是额外提供了圆角和阴影,看上去有立体效果。
1.2基本使用方法:
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal"
android:padding="5dp">
<ImageView
android:id="@+id/iv_img"
android:layout_height="50dp"
android:layout_width="50dp"
android:src="@mipmap/laoyou"
android:scaleType="fitXY"/>
<TextView
android:id="@+id/tv_title"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="旅游"
android:layout_marginTop="3dp"/>
</LinearLayout>
2 DrawerLayout 滑动菜单
DrawerLayout 包含两个界面 , 一个主界面和一个隐藏界面。隐藏界面可以通过点击按钮或者滑动屏幕边缘显示出来,一般隐藏界面用来做菜单使用。
代码:
<?xml version="1.0" encoding="utf-8"?> <androidx.drawerlayout.widget.DrawerLayout 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=".MainActivity3">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="主界面" />
<TextView
android:id="@+id/textView2"
android:layout_gravity="start"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="滑出页面"
android:background="#fff"/>
</androidx.drawerlayout.widget.DrawerLayout>
3 Fragment
3.1 Fragment的概念
1. Fragment是依赖于 Activity 的,不能独立存在。
2. 一个 Activity 里可以有多个 Fragment 。
3. 一个 Fragment 可以被多个 Activity 重用。
4. Fragment有自己的生命周期,并能接收输入事件。
5. 可以在 Activity 运行时动态地添加或删除 Fragment 。
3.1.1 Fragment生命周期
Activity 加载 Fragment 的时候,依次调用:
onAttach() -> onCreate() -> onCreateView() ->onActivityCreated() -> onStart() ->onResume()
3.2 Fragment的静态加载
1. 定义 Fragment 的布局,就是 fragment 显示内容
fragment_footer.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragment.FooterFragment">
<TextView
android:id="@+id/tv_footer"
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="#83A8C5"
android:gravity="center"
android:textSize="24dp"
android:text="页尾" />
</FrameLayout>
2. 自定义一个 Fragment 类,需要继承 Fragment 或者它的子类,重写 onCreateView() 方法,在该方法
中调用 inflater.inflate() 方法加载 Fragment 的布局文件,接着返回加载的 view 对象。
FooterFragment.java
public class FooterFragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1; private String mParam2;
public FooterFragment() {
// Required empty public constructor
}
/*** Use this factory method to create a new instance of
* this fragment using the provided parameters.
** @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment FooterFragment.
*/
// TODO: Rename and change types and number of parameters
public static FooterFragment newInstance(String param1, String param2) {
FooterFragment fragment = new FooterFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
publicViewonCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState{
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_footer, container, false);
}
}
3. 在需要加载 Fragment 的 Activity 对应的布局文件中添加 fragment 的标签,注意 name 属性是全限定
类名,就是要包含 Fragment 的包名。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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=".MainActivity4">
<fragment
android:id="@+id/headerFragment"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_alignParentTop="true"
android:name="com.hopu.cardviewdemo.fragment.HeaderFragment"/>
<fragment
android:id="@+id/footerFragment"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_alignParentBottom="true"
android:name="com.hopu.cardviewdemo.fragment.FooterFragment"/>
</RelativeLayout>
4. Activity 在 onCreate( ) 方法中调用 setContentView() 加载布局文件。
3.3 Fragment的动态使用(CardView与RecycleView的结合使用)
1. activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<FrameLayout
android:id="@+id/fl"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:layout_alignParentBottom="true"
app:menu="@menu/bottom_navigation_menu" />
</RelativeLayout>
2. 子布局CardView文件
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal"
android:padding="5dp">
<ImageView
android:id="@+id/iv_img"
android:layout_height="50dp"
android:layout_width="50dp"
android:src="@mipmap/laoyou"
android:scaleType="fitXY"/>
<TextView
android:id="@+id/tv_title"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="旅游"
android:layout_marginTop="3dp"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
3. 创建 Fragment
package com.wpw.socialapp.fragment;
import android.graphics.Color;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.widget.ViewPager2;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.wpw.socialapp.R;
import com.wpw.socialapp.adapter.AdsAdpter;
import com.wpw.socialapp.adapter.NaviqationAdapter1;
import com.wpw.socialapp.adapter.NaviqationAdapter2;
import com.wpw.socialapp.adapter.NaviqationAdapter3;
import com.wpw.socialapp.adapter.NaviqationAdapter4;
import com.wpw.socialapp.model.Ads;
import com.wpw.socialapp.model.Naviqation1;
import com.wpw.socialapp.model.Naviqation2;
import com.wpw.socialapp.model.Naviqation3;
import com.wpw.socialapp.model.Naviqation4;
import java.util.ArrayList;
import java.util.List;
public class ShouyeFragment extends Fragment {
private int[] imgs={R.mipmap.g1,R.mipmap.g2};
private List<Ads> list=new ArrayList<>();
private ViewPager2 vp_ads;
private int[] imgNavi1={R.mipmap.laoyou,R.mipmap.fuyou,R.mipmap.shangcan,R.mipmap.ziyuan};
private String[] titles1={"老弱服务","伤残服务","妇幼服务","自愿服务"};
private List<Naviqation1> listnavi1=new ArrayList<>();
private RecyclerView rv_navigation1;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_shouye, container, false);
vp_ads=view.findViewById(R.id.vp_ads);
list.clear();
listnavi1.clear();
info();
AdsAdpter adsAdpter=new AdsAdpter(list);
vp_ads.setAdapter(adsAdpter);
rv_navigation1=view.findViewById(R.id.rv_navigation1);
NaviqationAdapter1 naviqationAdapter1=new NaviqationAdapter1(listnavi1);
GridLayoutManager layoutManager1=new GridLayoutManager(getActivity(),4);
rv_navigation1.setLayoutManager(layoutManager1);
rv_navigation1.setAdapter(naviqationAdapter1);
return view;
}
private void info() {
for (int i = 0; i < imgs.length; i++) {
Ads ads=new Ads(imgs[i]);
list.add(ads);
}
for (int i = 0; i < imgNavi1.length; i++) {
Naviqation1 naviqation1=new Naviqation1(imgNavi1[i],titles1[i]);
listnavi1.add(naviqation1);
}
}
}
<?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"
android:orientation="vertical"
android:background="#F1F0F0"
tools:context=".fragment.ShouyeFragment">
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/vp_ads"
android:layout_width="match_parent"
android:layout_height="170dp"/>
<LinearLayout
android:layout_below="@+id/vp_ads"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/ll"
android:background="@color/white">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_navigation1"
android:layout_width="match_parent"
android:layout_height="80dp">
</androidx.recyclerview.widget.RecyclerView>
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_navigation2"
android:layout_width="match_parent"
android:background="@color/white"
android:layout_height="30dp"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_navigation3"
android:layout_marginTop="10dp"
android:background="@color/white"
android:layout_width="match_parent"
android:layout_height="120dp"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:scaleType="fitXY"
android:src="@mipmap/tuijian"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_navigation4"
android:background="@color/white"
android:layout_width="match_parent"
android:layout_height="200dp"/>
</LinearLayout>
4. 创建 Fragment2
5. 在活动中,点击按钮后切换 Fragment
package com.wpw.socialapp;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationBarView;
import com.wpw.socialapp.fragment.ShouyeFragment;
import com.wpw.socialapp.fragment.WodeFragment;
import com.wpw.socialapp.fragment.XiaoxiFragment;
import com.wpw.socialapp.fragment.ZixunFragment;
public class MainActivity extends AppCompatActivity {
private BottomNavigationView bottom_navigation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bottom_navigation=findViewById(R.id.bottom_navigation);
ShouyeFragment shouyeFragment=new ShouyeFragment();
ZixunFragment zixunFragment=new ZixunFragment();
getSupportFragmentManager().beginTransaction().add(R.id.fl,shouyeFragment).commit();
bottom_navigation.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
if (item.getItemId()==R.id.page_1) {
getSupportFragmentManager().beginTransaction().replace(R.id.fl,shouyeFragment).commit();
}else if (item.getItemId()==R.id.page_2) {
getSupportFragmentManager().beginTransaction().replace(R.id.fl,zixunFragment).commit();
}
return true;
}
});
}
}
更多推荐

所有评论(0)