赞
踩
目录
3.3 Fragment的动态使用(CardView与RecycleView的结合使用)
- <?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>
- <?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>
- <?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>
- 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);
- }
- }
- <?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>
- <?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>
- <?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>
- 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>
- 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;
- }
- });
-
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。