当前位置 博文首页 > 李猫er:【Android移动开发】使用Fragment碎片搭建框架实现不同
实现效果:
本篇只讲使用Fragment碎片搭建框架实现不同页面切换,至于页面内容暂不讲,因为这涉及的知识点较多还有很多第三方库.
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical"
tools:context="com.lyx.mycommunity.app.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_user"/>
<FrameLayout
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<RadioGroup
android:id="@+id/rg_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@drawable/home_bottom_parent_bg"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rb_home"
style="@style/MainButtonStyle"
android:drawableTop="@drawable/home_button_selector"
android:text=" 首页" />
<RadioButton
android:id="@+id/rb_category"
style="@style/MainButtonStyle"
android:drawableTop="@drawable/category_button_selector"
android:text=" 服务" />
<RadioButton
android:id="@+id/rb_community"
style="@style/MainButtonStyle"
android:drawableTop="@drawable/community_button_selector"
android:paddingTop="10dp"
android:text=" 新闻" />
<RadioButton
android:id="@+id/rb_search"
style="@style/MainButtonStyle"
android:drawableTop="@drawable/cart_button_selector"
android:text=" 搜索" />
<RadioButton
android:id="@+id/rb_user"
style="@style/MainButtonStyle"
android:drawableTop="@drawable/user_button_selector"
android:text=" 个人中心" />
</RadioGroup>
</LinearLayout>
onCreateView()
和onActivityCreated()
方法 在onCreateView()方法中调用:inflater.inflate()方法加载Fragment的布局文件,并返回加载的view对象package com.lyx.mycommunity.base;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public abstract class BaseFragment extends Fragment {
public Context myContext;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myContext = getActivity();
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return initView();
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
initData();
}
/**
*由子类实现,实现特有效果
* @return
*/
public abstract View initView();
/**
* 初始化数据
*/
public void initData() {
}
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
initData();
}
fragments = new ArrayList<>();
fragments.add(new HomeFragment());
fragments.add(new ServiceFragment());
fragments.add(new NewsFragment());
fragments.add(new SearchFragment());
fragments.add(new UserFragment());
主Activity页面代码:
package com.lyx.mycommunity.app;
import android.content.Intent;
import android.os.Bundle;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.RecyclerView;
import com.lyx.mycommunity.adapter.HomeRecycleAdapter;
import com.lyx.mycommunity.base.BaseFragment;
import com.lyx.mycommunity.fragment.ServiceFragment;
import com.lyx.mycommunity.fragment.NewsFragment;
import com.lyx.mycommunity.fragment.HomeFragment;
import com.lyx.mycommunity.fragment.UserFragment;
import com.lyx.mycommunity.utils.BatteryChangeReceiver;
import com.lyx.shoppingcity.R;
import com.lyx.mycommunity.fragment.SearchFragment;
import java.util.ArrayList;
import java.util.List;
import butterknife.Bind;
import butterknife.ButterKnife;
public class MainActivity extends FragmentActivity {
@Bind(R.id.frameLayout)
FrameLayout frameLayout;
@Bind(R.id.rb_home)
RadioButton rbHome;
@Bind(R.id.rb_category)
RadioButton rbType;
@Bind(R.id.rb_community)
RadioButton rbCommunity;
@Bind(R.id.rb_search)
RadioButton rbSearch;
@Bind(