当前位置 : 主页 > 手机开发 > android >

Android fragment实现多个页面切换效果

来源:互联网 收集:自由互联 发布时间:2021-08-03
现在的APP首页大部分屏幕的下方显示一行Tab标签选项,点击不同的标签就可以切换到不同的界面。如下图: 我们之前都是用TabHost来实现,但是殊不知,TabHost并非是那么的简单,它的可

现在的APP首页大部分屏幕的下方显示一行Tab标签选项,点击不同的标签就可以切换到不同的界面。如下图:

我们之前都是用TabHost来实现,但是殊不知,TabHost并非是那么的简单,它的可扩展性非常的差,不能随意地定制Tab项显示的内容,而且运行还要依赖于ActivityGroup。ActivityGroup原本主要是用于为每一个TabHost的子项管理一个单独的Activity,但目前已经被废弃了。下面就借助Fragment来完成类似于TabHost一般的效果。

先实现主界面布局main_layout.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
 
<FrameLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</FrameLayout>
 
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#000000" />
 
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#ffffff"
android:orientation="horizontal" >
 
<RelativeLayout
android:id="@+id/message_layout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" >
 
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:orientation="vertical" >
 
<ImageView
android:id="@+id/message_image"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center_horizontal"
android:src="@drawable/ic_launcher" />
 
<TextView
android:id="@+id/message_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="消息"
android:textColor="#82858b" />
</LinearLayout>
</RelativeLayout>
 
<View
android:layout_width="0.5dp"
android:layout_height="match_parent"
android:background="#000000" />
 
<RelativeLayout
android:id="@+id/contacts_layout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" >
 
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:orientation="vertical" >
 
<ImageView
android:id="@+id/contacts_image"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center_horizontal"
android:src="@drawable/ic_launcher" />
 
<TextView
android:id="@+id/contacts_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="联系人"
android:textColor="#82858b" />
</LinearLayout>
</RelativeLayout>
 
<View
android:layout_width="0.5dp"
android:layout_height="match_parent"
android:background="#000000" />
 
<RelativeLayout
android:id="@+id/news_layout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" >
 
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:orientation="vertical" >
 
<ImageView
android:id="@+id/news_image"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center_horizontal"
android:src="@drawable/ic_launcher" />
 
<TextView
android:id="@+id/news_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="动态"
android:textColor="#82858b" />
</LinearLayout>
</RelativeLayout>
 
<View
android:layout_width="0.5dp"
android:layout_height="match_parent"
android:background="#000000" />
 
<RelativeLayout
android:id="@+id/setting_layout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" >
 
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:orientation="vertical" >
 
<ImageView
android:id="@+id/setting_image"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center_horizontal"
android:src="@drawable/ic_launcher" />
 
<TextView
android:id="@+id/setting_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="设置"
android:textColor="#82858b" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
 
</LinearLayout>

这段布局代码虽然有点长,但其实主要就分为两部分。第一个部分就是FrameLayout,这里只是给FrameLayout的id设置成content,并没有在里面添加任何具体的内容,因为具体的内容是要在后面动态进行添加的。第二个部分就是FrameLayout下面的LinearLayout,这个LinearLayout中包含的就是整个类似于TabHost的布局。可以看到,我们将这个LinearLayout又等分成了四份,每一份中都会显示一个ImageView和一个TextView。ImageView用于显示当前Tab的图标,TextView用于显示当前Tab的标题。

既然是等分成了四分,那接下来我们自然要去分别实现四个Fragment和它们的布局了。新建一个message_layout.xml作为消息界面的布局,代码如下所示:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
 
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="vertical" >
 
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="@drawable/ic_launcher" />
 
<TextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="10dp"
android:text="这是消息界面"
android:textSize="20sp" />
</LinearLayout>
 
</RelativeLayout>

其他三个界面类似就不一 一列出。然后要去创建对应这个布局的Fragment。新建MessageFragment继承自Fragment,代码如下所示:

public class MessageFragment extends Fragment{
 private TextView tv;
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
  Bundle savedInstanceState) {
 View messageLayout = inflater.inflate(R.layout.message, container, false);
 tv=(TextView) messageLayout.findViewById(R.id.message);
 tv.setText("哈哈哈哈哈哈");
 return messageLayout;
 }
 
}

我们也依次创建其他三个布局的Fragment。最后是MainActivity,代码如下:

public class MainActivity extends Activity implements OnClickListener {
/**
* 用于展示消息的Fragment
*/
private MessageFragment messageFragment;
 
/**
* 用于展示联系人的Fragment
*/
private ContactsFragment contactsFragment;
 
/**
* 用于展示动态的Fragment
*/
private NewsFragment newsFragment;
 
/**
* 用于展示设置的Fragment
*/
private SettingFragment settingFragment;
 
/**
* 消息界面布局
*/
private View messageLayout;
 
/**
* 联系人界面布局
*/
private View contactsLayout;
 
/**
* 动态界面布局
*/
private View newsLayout;
 
/**
* 设置界面布局
*/
private View settingLayout;
/**
* 用于对Fragment进行管理
*/
private FragmentManager fragmentManager;
 
@Override
protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 initViews();
 fragmentManager = getFragmentManager();
 // 第一次启动时选中第0个tab
 setTabSelection(0);
}
 
/**
* 在这里获取到每个需要用到的控件的实例,并给它们设置好必要的点击事件。
*/
 
private void initViews() {
 messageLayout = findViewById(R.id.message_layout);
 contactsLayout = findViewById(R.id.contacts_layout);
 newsLayout = findViewById(R.id.news_layout);
 settingLayout = findViewById(R.id.setting_layout);
 messageLayout.setOnClickListener(this);
 contactsLayout.setOnClickListener(this);
 newsLayout.setOnClickListener(this);
 settingLayout.setOnClickListener(this);
}
 
@Override
public void onClick(View v) {
 switch (v.getId()) {
 case R.id.message_layout:
  // 当点击了消息tab时,选中第1个tab
  setTabSelection(0);
  break;
 case R.id.contacts_layout:
  // 当点击了联系人tab时,选中第2个tab
  setTabSelection(1);
  break;
 case R.id.news_layout:
  // 当点击了动态tab时,选中第3个tab
  setTabSelection(2);
  break;
 case R.id.setting_layout:
  // 当点击了设置tab时,选中第4个tab
  setTabSelection(3);
  break;
 default:
 break;
}
 
}
 
/**
* 根据传入的index参数来设置选中的tab页。
*
* @param index
* 每个tab页对应的下标。0表示消息,1表示联系人,2表示动态,3表示设置。
*/
private void setTabSelection(int index) {
 // 每次选中之前先清楚掉上次的选中状态
 clearSelection();
 // 开启一个Fragment事务
 FragmentTransaction transaction = fragmentManager.beginTransaction();
 // 先隐藏掉所有的Fragment,以防止有多个Fragment显示在界面上的情况
 hideFragments(transaction);
 switch (index) {
 case 0:
  messageLayout.setBackgroundColor(0xff0000ff);
 
  if (messageFragment == null) {
  // 如果MessageFragment为空,则创建一个并添加到界面上
  messageFragment = new MessageFragment();
  transaction.add(R.id.content, messageFragment);
  } else {
  // 如果MessageFragment不为空,则直接将它显示出来
  transaction.show(messageFragment);
  }
  break;
 case 1:
 // 当点击了联系人tab时,改变控件的图片和文字颜色
 contactsLayout.setBackgroundColor(0xff0000ff);
 if (contactsFragment == null) {
  // 如果ContactsFragment为空,则创建一个并添加到界面上
  contactsFragment = new ContactsFragment();
  transaction.add(R.id.content, contactsFragment);
 } else {
  // 如果ContactsFragment不为空,则直接将它显示出来
  transaction.show(contactsFragment);
 }
 break;
 case 2:
 // 当点击了动态tab时,改变控件的图片和文字颜色
  newsLayout.setBackgroundColor(0xff0000ff);
  if (newsFragment == null) {
  // 如果NewsFragment为空,则创建一个并添加到界面上
  newsFragment = new NewsFragment();
  transaction.add(R.id.content, newsFragment);
  } else {
  // 如果NewsFragment不为空,则直接将它显示出来
  transaction.show(newsFragment);
  }
  break;
 case 3:
  default:
  // 当点击了设置tab时,改变控件的图片和文字颜色
  settingLayout.setBackgroundColor(0xff0000ff);
  if (settingFragment == null) {
  // 如果SettingFragment为空,则创建一个并添加到界面上
  settingFragment = new SettingFragment();
  transaction.add(R.id.content, settingFragment);
  } else {
  // 如果SettingFragment不为空,则直接将它显示出来
  transaction.show(settingFragment);
  }
 break;
 }
 transaction.commit();
}
 
/**
* 将所有的Fragment都置为隐藏状态。
*
* @param transaction
* 用于对Fragment执行操作的事务
*/
private void hideFragments(FragmentTransaction transaction) {
 if (messageFragment != null) {
  transaction.hide(messageFragment);
 }
 if (contactsFragment != null) {
  transaction.hide(contactsFragment);
 }
 if (newsFragment != null) {
  transaction.hide(newsFragment);
 }
 if (settingFragment != null) {
  transaction.hide(settingFragment);
 }
}
 
/**
* 清除掉所有的选中状态。
*/
private void clearSelection() {
 messageLayout.setBackgroundColor(0xffffffff);
 contactsLayout.setBackgroundColor(0xffffffff);
 newsLayout.setBackgroundColor(0xffffffff);
 settingLayout.setBackgroundColor(0xffffffff);
 }
}

这样就实现了上面图中的效果了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。

网友评论