fragment实现隐藏及界面切换效果

所属分类: 软件编程 / Android 阅读数: 48
收藏 0 赞 0 分享

在前文中的效果中(Android如何创建自定义ActionBar),点击屏幕下方的 TextView 以此来实现 5 种 fragment 界面的切换。

       由于网络数据的加载存在于不同的界面之中,当快速的切换界面时,就会出现程序的出错。因为快速的切换时,当前界面的数据还在读取,就切换到下一个界面,下一个界面也开始加载数据,每次界面的切换都会加载数据。这样就会出错(在本文中,fragment 是使用 replace() 方法来加载界面的,)。所以可以使每个 fragment 只加载一次来减少数据的加载次数。当然可以使用缓存技术来解决问题。

       本文中只使用 fragment 的隐藏或者加载来实现每个界面只加载一次。这时需要多定义一个 Fragment 变量,以充当中间的变量,来实现 fragment 的隐藏。

       上文中界面切换的效果,其实很简单,即:点击当前 TextView 使其颜色改变,其他的 TextView 的颜色都变为相同颜色即可。这时可以把这些变化封装为一个方法。减少代码量。

 MainActivity.java :

package com.crazy.gemi;
 
import android.app.SearchManager;
import android.content.Intent;
import android.graphics.Color;
import android.provider.SearchRecentSuggestions;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.TextView;
 
import com.crazy.gemi.ui.cheaper.CheaperFragment;
import com.crazy.gemi.ui.cheaper.SearchSuggestionSampleProvider;
import com.crazy.gemi.ui.favor.FavorFragment;
import com.crazy.gemi.ui.more.MoreFragment;
import com.crazy.gemi.ui.near.NearFragment;
import com.crazy.gemi.ui.pocket.PocketFragment;
 
public class MainActivity extends FragmentActivity
    implements View.OnClickListener, CheaperFragment.SearchResult{
 
  private TextView[] textView = new TextView[5];
  private View[] views = new View[5];
  // 其中的 firstFragment 相当于是个中间变量
  private Fragment firstFragment, nearFragment, cheaperFragment, favorFragment, pocketFragmnet, moreFragment;
 
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
 
    init();
    initFragment();
  }
 
  private void init() {
 
    textView[0] = (TextView)findViewById(R.id.near);
    textView[1] = (TextView)findViewById(R.id.search_cheaper);
    textView[2] = (TextView)findViewById(R.id.favor);
    textView[3] = (TextView)findViewById(R.id.pocket);
    textView[4] = (TextView)findViewById(R.id.more);
 
    views[0] = findViewById(R.id.near_top_line);
    views[1] = findViewById(R.id.cheaper_top_line);
    views[2] = findViewById(R.id.favor_top_line);
    views[3] = findViewById(R.id.pocket_top_line);
    views[4] = findViewById(R.id.more_top_line);
 
    textView[0].setOnClickListener(this);
    textView[1].setOnClickListener(this);
    textView[2].setOnClickListener(this);
    textView[3].setOnClickListener(this);
    textView[4].setOnClickListener(this);
 
  }
 
  private void initFragment() {
    firstFragment = FavorFragment.newInstance();
    favorFragment = firstFragment;
    // 最先加载的 fragment
    getSupportFragmentManager().beginTransaction().
        add(R.id.frame_layout, favorFragment).commit();
    textView[2].setTextColor(Color.BLACK);
    views[2].setBackgroundColor(Color.parseColor("#FF6600"));
  }
 
  @Override
  public void onClick(View v) {
    switch (v.getId()) {
      case R.id.near:
//        getSupportFragmentManager().beginTransaction().
//            replace(R.id.frame_layout, NearFragment.newInstance()).commit();
 
        if(nearFragment==null){
          nearFragment= NearFragment.newInstance();
        }
        switchContent(firstFragment, nearFragment, getSupportFragmentManager().beginTransaction());
        firstFragment = nearFragment;
 
        selectStringAndBackgroundColor(0);
        break;
      case R.id.search_cheaper:
        if(cheaperFragment==null){
          cheaperFragment= CheaperFragment.newInstance();
        }
        switchContent(firstFragment, cheaperFragment, getSupportFragmentManager().beginTransaction());
        firstFragment = cheaperFragment;
 
        selectStringAndBackgroundColor(1);
        break;
      case R.id.favor:
        if(favorFragment==null){
          favorFragment= FavorFragment.newInstance();
        }
        switchContent(firstFragment, favorFragment, getSupportFragmentManager().beginTransaction());
        firstFragment = favorFragment;
 
        selectStringAndBackgroundColor(2);
        break;
      case R.id.pocket:
        if(pocketFragmnet==null){
          pocketFragmnet= PocketFragment.newInstance();
        }
        switchContent(firstFragment, pocketFragmnet, getSupportFragmentManager().beginTransaction());
        firstFragment = pocketFragmnet;
 
        selectStringAndBackgroundColor(3);
        break;
      case R.id.more:
        if(moreFragment==null){
          moreFragment= MoreFragment.newInstance();
        }
        switchContent(firstFragment, moreFragment, getSupportFragmentManager().beginTransaction());
        firstFragment = moreFragment;
 
        selectStringAndBackgroundColor(4);
        break;
    }
  }
 
  /**
   * 通过 position 的位置改变文字和 View 的颜色
   * @param position
   */
  private void selectStringAndBackgroundColor(int position){
    int sum = textView.length;
    for (int i = 0; i < sum; i++) {
      if (position == i) {
        textView[i].setTextColor(Color.BLACK);
        views[i].setBackgroundColor(Color.parseColor("#FF6600"));
      } else {
        textView[i].setTextColor(Color.GRAY);
        views[i].setBackgroundColor(Color.parseColor("#f0f0f0"));
      }
    }
  }
 
  /**
   * 判断是否添加了界面,以保存当前状态
   */
  public void switchContent(Fragment from, Fragment to,
               FragmentTransaction transaction) {
 
    if (!to.isAdded()) { // 先判断是否被add过
 
      transaction.hide(from).add(R.id.frame_layout, to)
          .commit(); // 隐藏当前的fragment,add下一个到Activity中
    } else {
      transaction.hide(from).show(to).commit(); // 隐藏当前的fragment,显示下一个
    }
 
  }
 
  
}

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

更多精彩内容其他人还在看

使用ViewPager实现android软件使用向导功能实现步骤

现在的大部分android软件,都是使用说明,就是第一次使用该软件时,会出现向导,可以左右滑动,然后就进入应用的主界面了,下面我们就实现这个功能
收藏 0 赞 0 分享

android在异步任务中关闭Cursor的代码方法

android在异步任务中如何关闭Cursor?在我们开发应用的时候,很多时候会遇到这种问题,下面我们就看看代码如何实现
收藏 0 赞 0 分享

Android自定义桌面功能代码实现

android自定义桌面其实很简单,看一个例子就明白了
收藏 0 赞 0 分享

android将图片转换存到数据库再从数据库读取转换成图片实现代码

有时候我们想把图片存入到数据库中,尽管这不是一种明智的选择,但有时候还是不得以会用到,下面说说将图片转换成byte[]数组存入到数据库中去,并从数据库中取出来转换成图像显示出来
收藏 0 赞 0 分享

TextView显示系统时间(时钟功能带秒针变化

用System.currentTimeMillis()可以获取系统当前的时间,我们可以开启一个线程,然后通过handler发消息,来实时的更新TextView上显示的系统时间,可以做一个时钟的功能
收藏 0 赞 0 分享

Android用ListView显示SDCard文件列表的小例子

本文简单实现了用ListView显示SDCard文件列表,目录的回退等功能暂不讨论,获取文件列表,files即为所选择目录下的所有文件列表
收藏 0 赞 0 分享

Android拦截外拨电话程序示例

这篇文章主要介绍了Android拦截外拨电话的示例,大家参考使用吧
收藏 0 赞 0 分享

通过Html网页调用本地安卓(android)app程序代码

如何使用html网页和本地app进行传递数据呢?经过研究,发现还是有方法的,总结了一下,大致有一下几种方式
收藏 0 赞 0 分享

android Textview文字监控(Textview使用方法)

以手机号充值为例,当用户输入最后一位数时候,进行汇率的变换,本文就实现类似这样的功能
收藏 0 赞 0 分享

Android ListView长按弹出菜单二种实现方式示例

这篇文章主要介绍了Android ListView长按弹出菜单的方法,大家参考实现
收藏 0 赞 0 分享
查看更多