当前位置: 首页 > news >正文

海淀网站建设公司排名西安百度公司

海淀网站建设公司排名,西安百度公司,wordpress怎么关注别人,河南郑州特产文章目录 主界面布局资源两个工具Fragment主程序 主界面布局资源 在activity_main.xml中&#xff0c;声明两个按钮备用&#xff0c;再加入一个帧布局&#xff0c;待会儿用来展示Fragment。 <?xml version"1.0" encoding"utf-8"?> <LinearLayo…

文章目录

  • 主界面布局资源
  • 两个工具Fragment
  • 主程序

主界面布局资源

activity_main.xml中,声明两个按钮备用,再加入一个帧布局,待会儿用来展示Fragment。

<?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"tools:context=".MainActivity"><Buttonandroid:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/button1"android:text="@string/push"/><Buttonandroid:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/button2"android:text="@string/replace"/><FrameLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:id="@+id/framelayout"android:background="@color/purple_200"/></LinearLayout>

两个工具Fragment

用来展示的Fragment,随便找两个AS预设的即可,这里使用的是一个BlankFragment和一个ItemFragment。
BlankFragment:

<?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=".BlankFragment1"><!-- TODO: Update blank fragment layout --><TextViewandroid:layout_width="match_parent"android:layout_height="match_parent"android:text="@string/hello_blank_fragment" /></FrameLayout>
package com.example.dynamicfragment;import android.os.Bundle;import androidx.fragment.app.Fragment;import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;/*** A simple {@link Fragment} subclass.* Use the {@link BlankFragment1#newInstance} factory method to* create an instance of this fragment.*/
public class BlankFragment1 extends Fragment {// TODO: Rename parameter arguments, choose names that match// the fragment initialization parameters, e.g. ARG_ITEM_NUMBERprivate static final String ARG_PARAM1 = "param1";private static final String ARG_PARAM2 = "param2";// TODO: Rename and change types of parametersprivate String mParam1;private String mParam2;public BlankFragment1() {// 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 BlankFragment1.*/// TODO: Rename and change types and number of parameterspublic static BlankFragment1 newInstance(String param1, String param2) {BlankFragment1 fragment = new BlankFragment1();Bundle args = new Bundle();args.putString(ARG_PARAM1, param1);args.putString(ARG_PARAM2, param2);fragment.setArguments(args);return fragment;}@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);if (getArguments() != null) {mParam1 = getArguments().getString(ARG_PARAM1);mParam2 = getArguments().getString(ARG_PARAM2);}}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {// Inflate the layout for this fragmentreturn inflater.inflate(R.layout.fragment_blank1, container, false);}
}

ItemFragment:

<?xml version="1.0" encoding="utf-8"?>
<androidx.recyclerview.widget.RecyclerView 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:id="@+id/list"android:name="com.example.dynamicfragment.ItemFragment"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_marginLeft="16dp"android:layout_marginRight="16dp"app:layoutManager="LinearLayoutManager"tools:context=".ItemFragment"tools:listitem="@layout/fragment_item" />
package com.example.dynamicfragment;import android.content.Context;
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 android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;import com.example.dynamicfragment.placeholder.PlaceholderContent;/*** A fragment representing a list of Items.*/
public class ItemFragment extends Fragment {// TODO: Customize parameter argument namesprivate static final String ARG_COLUMN_COUNT = "column-count";// TODO: Customize parametersprivate int mColumnCount = 1;/*** Mandatory empty constructor for the fragment manager to instantiate the* fragment (e.g. upon screen orientation changes).*/public ItemFragment() {}// TODO: Customize parameter initialization@SuppressWarnings("unused")public static ItemFragment newInstance(int columnCount) {ItemFragment fragment = new ItemFragment();Bundle args = new Bundle();args.putInt(ARG_COLUMN_COUNT, columnCount);fragment.setArguments(args);return fragment;}@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);if (getArguments() != null) {mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT);}}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {View view = inflater.inflate(R.layout.fragment_item_list, container, false);// Set the adapterif (view instanceof RecyclerView) {Context context = view.getContext();RecyclerView recyclerView = (RecyclerView) view;if (mColumnCount <= 1) {recyclerView.setLayoutManager(new LinearLayoutManager(context));} else {recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount));}recyclerView.setAdapter(new MyItemRecyclerViewAdapter(PlaceholderContent.ITEMS));}return view;}
}

主程序

在主程序里,我们要实现点击按钮显示不同的Fragment。这里使用一种新的实现按钮方式,在声明MainActivity类的时候引用View.OnClickListener接口,然后在button1.setOnClickListener(this);中传入this,这样按钮被点击时就会自动调用后面写的OnClick函数。

OnClick函数被调用时,我们判断是哪一个按钮被点击了,然后根据按钮ID将不同的Fragment展现在帧布局上。

package com.example.dynamicfragment;import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;import android.os.Bundle;
import android.view.View;
import android.widget.Button;public class MainActivity extends AppCompatActivity implements View.OnClickListener{@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button button1 = findViewById(R.id.button1);Button button2 = findViewById(R.id.button2);button1.setOnClickListener(this);button2.setOnClickListener(this);}@Overridepublic void onClick(View view) {switch (view.getId()){case R.id.button1:replaceFragment(new BlankFragment1());break;case R.id.button2:replaceFragment(new ItemFragment());break;}}private void replaceFragment(Fragment fragment) {FragmentManager fragmentManager = getSupportFragmentManager();FragmentTransaction transaction = fragmentManager.beginTransaction();transaction.replace(R.id.framelayout, fragment);//创建replace事件transaction.addToBackStack(null);transaction.commit();//执行transaction中的事件}
}

还需要重点讲解一下的是replaceFragment函数中的栈,transaction.addToBackStack(null);中的null指代的是默认栈。加入该语句后,每次更新都会向栈中加入一个Fragment,且屏幕上显示的即是栈顶的Fragment。当我们点击返回按钮时,栈顶的Fragment被弹出,屏幕上显示下一个Fragment。

试验如下,交替点击两个按钮若干次,屏幕上会依次出现两个Fragment交替覆盖,而点击返回按钮后,最顶上Fragment则会被撤除。
在这里插入图片描述
在这里插入图片描述

http://www.yidumall.com/news/106057.html

相关文章:

  • 网站管理系统模板2023年国家免费技能培训
  • 自动化产品的网站建设百度代发排名
  • 湖北响应式网站建设设计百度词条
  • 开源程序做网站任务百度快照网址
  • 网站 建设设计微信管理系统
  • 定制网站建设公司哪家便宜上海seo关键词优化
  • 暴雪战网杭州seo顾问
  • 泉州网站建设公司首选公司关键词如何确定
  • 手机上传网站源码自己搭建网站
  • iis7配置asp网站网站排名
  • 网站做哪块简单网络域名怎么查
  • 淘宝网站建设可行性分析平台推广策划方案
  • 租车公司网站模板产品网站推广
  • php网站开发api互联网营销工具
  • 今标 网站建设网络推广站
  • 网站后台发邮件搜索关键词的网站
  • 东阳高端营销型网站建设品牌宜昌seo
  • 设计电子商务网站百度推广一天烧多少钱
  • 石家庄建站程序巨量数据分析入口
  • 西班牙语网站设计哪家好惊艳的网站设计
  • 安全认证的机票网站成就怎么做制作网页
  • 手机网站建设选 朗创营销全网推广引流黑科技
  • wordpress心型图片滚动seo中介平台
  • 长沙建设品牌网站市场推广怎么写
  • 织梦网站源码好吗免费刷赞网站推广免费
  • 烟台网站设计公司推荐网络营销策划书格式
  • 河北网站建设公司百度有效点击软件
  • 视频网站中滑动列表怎么做网站登录入口
  • php做的网站代码2023年8月份新冠病毒
  • 北京网站设计公司sx成都柚米科技15快速排名优化推广价格