首页 >> 大全

fragment(一)加载方式

2024-01-06 大全 26 作者:考证青年

静态加载:

把当成普通类来使用。

1.继承类,重载的方法,此方法主要是创建的布局

2.在的XMl文件中,通过标签就可以使用我们自定义的,这里通过指定标签的name属性来确定使用哪一个。

例子:

的布局:

xmlns:=""

xmlns:tools=""

:=""

:=""

:="@color/"

tools:="com.....">

:id="@+id/"

:=""

:=""

:=""

:=""

:="20dp"

:text="@/"/>

的代码:

package com.example.liaoli.fragmentdemo.fragment;import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;import com.example.liaoli.fragmentdemo.R;public class ContentFragment extends Fragment {private TextView title;public static ContentFragment newInstance(String param1, String param2) {ContentFragment fragment = new ContentFragment();return fragment;}public ContentFragment() {}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {// Inflate the layout for this fragmentView view = inflater.inflate(R.layout.fragment_content, container, false);title = (TextView) view.findViewById(R.id.content);title.setText("我是内容");return view ;}}

的布局文件:

xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="50dp"android:background="@color/entertainment_items_bg"tools:context="com.example.liaoli.fragmentdemo.fragment.TitleFragment">android:id="@+id/title"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:text="@string/hello_blank_fragment"android:textSize="25dp" />android:id="@+id/share_bt"android:layout_width="60dp"android:layout_height="40dp"android:layout_centerVertical="true"android:layout_alignParentRight="true"android:text="分享"android:onClick="share"/>

的代码:

package com.example.liaoli.fragmentdemo.fragment;import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;import com.example.liaoli.fragmentdemo.R;public class TitleFragment extends Fragment {private TextView title;public static TitleFragment newInstance(String param1, String param2) {TitleFragment fragment = new TitleFragment();return fragment;}public TitleFragment() {// Required empty public constructor}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {// Inflate the layout for this fragmentView view = inflater.inflate(R.layout.fragment_title, container, false);title = (TextView) view.findViewById(R.id.title);title.setText("我是标题");Button share = (Button) view.findViewById(R.id.share_bt);share.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Toast.makeText(getActivity(), "分享这篇文章", Toast.LENGTH_SHORT).show();}});return view ;}}

的布局文件

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:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context="com.example.liaoli.fragmentdemo.StaticLoadActivity">android:id="@+id/title_fragment"android:layout_width="fill_parent"android:layout_height="wrap_content"android:name="com.example.liaoli.fragmentdemo.fragment.TitleFragment"/>android:id="@+id/content_fragment"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_below="@+id/title_fragment"android:name="com.example.liaoli.fragmentdemo.fragment.ContentFragment"/>

的代码:

package com.example.liaoli.fragmentdemo;import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;public class StaticLoadActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_static_load);}}

效果

liaoli

二,动态加载

在程序的运行过程中动态的添加,删除,替换

的布局文件

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="@color/top_item_bg"tools:context="com.example.liaoli.fragmentdemo.fragment.TopNewsFragment">android:id="@+id/content"android:layout_width="wrap_content"android:gravity="center"android:layout_gravity="center"android:layout_height="wrap_content"android:textSize="20dp"android:text="头条新闻条目列表" />

的代码:

package com.example.liaoli.fragmentdemo.fragment;import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;import com.example.liaoli.fragmentdemo.R;public class TopNewsFragment extends Fragment {public static TopNewsFragment newInstance(String param1, String param2) {TopNewsFragment fragment = new TopNewsFragment();return fragment;}public TopNewsFragment() {}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {// Inflate the layout for this fragmentreturn inflater.inflate(R.layout.fragment_top_news, container, false);}}

的布局

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="@color/hot_items_bg"tools:context="com.example.liaoli.fragmentdemo.fragment.HotNewsFragment">android:id="@+id/content"android:layout_width="wrap_content"android:gravity="center"android:layout_gravity="center"android:layout_height="wrap_content"android:textSize="20dp"android:text="热点新闻条目列表" />

的代码

package com.example.liaoli.fragmentdemo.fragment;import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;import com.example.liaoli.fragmentdemo.R;public class HotNewsFragment extends Fragment {public static HotNewsFragment newInstance(String param1, String param2) {HotNewsFragment fragment = new HotNewsFragment();return fragment;}public HotNewsFragment() {// Required empty public constructor}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {// Inflate the layout for this fragmentreturn inflater.inflate(R.layout.fragment_hot_news, container, false);}}

其他两个 :t 和的代码和布局与上面两个几乎差不多,再次不在多说

的布局:

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:orientation="vertical"tools:context="com.example.liaoli.fragmentdemo.StaticLoadActivity">android:id="@+id/news_fragment"android:layout_width="fill_parent"android:layout_height="0dp"android:layout_weight="1"/>layout="@layout/bottom_bar"/>

.xml

xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="@color/bottom_bar_bg"android:orientation="horizontal">android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="头条"android:onClick="tops"/>android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:onClick="hots"android:text="热点" />android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="娱乐"android:onClick="entertainments"/>android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="体育"android:onClick="sports"/>

.xml

xml version="1.0" encoding="utf-8"?>
name="bottom_bar_bg">#ff224ename="top_item_bg">#5477ffname="hot_items_bg">#dc97ffname="entertainment_items_bg">#ffd60dname="sport_items_bg">#21ff74

的代码:

package com.example.liaoli.fragmentdemo;import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;import com.example.liaoli.fragmentdemo.fragment.EntertainmentFragment;
import com.example.liaoli.fragmentdemo.fragment.HotNewsFragment;
import com.example.liaoli.fragmentdemo.fragment.SportsFragment;
import com.example.liaoli.fragmentdemo.fragment.TopNewsFragment;public class DynamicLoadActivity extends AppCompatActivity {private TopNewsFragment topNewsFragment;private HotNewsFragment hotNewsFragment;private EntertainmentFragmententertainmentFragment;private SportsFragment sportsFragment;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_dynamic_load);initdata();}private void initdata() {FragmentManager fragmentManager = getSupportFragmentManager();FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();if (topNewsFragment == null) {topNewsFragment = new TopNewsFragment();}fragmentTransaction.add(R.id.news_fragment, topNewsFragment);fragmentTransaction.commit();}public void tops(View view) {FragmentManager fragmentManager = getSupportFragmentManager();FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();if (topNewsFragment == null) {topNewsFragment = new TopNewsFragment();}fragmentTransaction.replace(R.id.news_fragment, topNewsFragment);fragmentTransaction.commit();}public void hots(View view) {FragmentManager fragmentManager = getSupportFragmentManager();FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();if (hotNewsFragment == null) {hotNewsFragment= new HotNewsFragment();}fragmentTransaction.replace(R.id.news_fragment, hotNewsFragment);fragmentTransaction.commit();}public void entertainments(View view) {FragmentManager fragmentManager = getSupportFragmentManager();FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();if (entertainmentFragment == null) {entertainmentFragment= new EntertainmentFragment();}fragmentTransaction.replace(R.id.news_fragment, entertainmentFragment);fragmentTransaction.commit();}public void sports(View view) {FragmentManager fragmentManager = getSupportFragmentManager();FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();if (sportsFragment == null) {sportsFragment= new SportsFragment();}fragmentTransaction.replace(R.id.news_fragment, sportsFragment);fragmentTransaction.commit();}}

效果:

点击头条按钮

_加载方式是什么意思_加载方式疲劳分析

点击热点按钮:

加载方式是什么意思__加载方式疲劳分析

点击娱乐按钮:

加载方式疲劳分析__加载方式是什么意思

点击体育按钮

_加载方式疲劳分析_加载方式是什么意思

常用的三个类,

封装

封装了对的管理

封装了对的事务处理流程

的获取方式

3.0以上()

v4中nager

处理操作事物的方法如下

1.开启一个事务:

FragmentManager fragmentManager = getSupportFragmentManager();FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();//开启一个事务

2.向中添加

add(int ,)

(int, , )with a null tag.

add(,)

(int, , )with a 0 .

fragmentTransaction.add(R.id.news_fragment, topNewsFragment);

3.删除一个,如果被移除的没有添加到回退栈中,这个实例将被销毁

()

an .

4.隐藏一个

此时仅仅是不可见,而并没有被销毁

hide()

Hides an .

5.显示之前隐藏的

show()

Shows a .

6.将view从UI中移除

此方法跟不一样,此时还是有维护管理

()

the given from the UI.

7.重建的View布局,并附添加到UI并显示出来

()

Re- a after it had been from the UI ().

8.替换,通过源码我们会知道,其实就是先删除,在添加

(int ,,)

an that was added to a .

(int ,)

(int, , )with a null tag.

在一个事务的开启到提交的过程中可以多次添加、移除、替换等相关操作。

在我们使用的过程中一定要弄清楚事务处理的各个方法那些会销毁视图,那些会销毁实例。

常见错误:State loss这样的错误。主要是因为:方法一定要在.()之前调用。

关于我们

最火推荐

小编推荐

联系我们


版权声明:本站内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 88@qq.com 举报,一经查实,本站将立刻删除。备案号:桂ICP备2021009421号
Powered By Z-BlogPHP.
复制成功
微信号:
我知道了