当前位置: 移动技术网 > IT编程>移动开发>Android > fragment实现隐藏及界面切换效果

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

2019年08月01日  | 移动技术网IT编程  | 我要评论

马依萌,sf999,bbgyy

在前文中的效果中(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,显示下一个
    }
 
  }
 
  
}

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网