学了一小段时间的Android,主要接触的是UI设计,打交道最多莫过于fragment了吧。在Android3.0引入了fragment的概念后,几乎在所以的Android的应用中都可以看见其身影,已经有很多前辈高人都已经详细介绍过fragmrnt,我这里就不多说什么了。

这里本来是想模仿一下微信的切换效果,怎奈水平不足,这里就献丑贴出半成品的代码,希望有大神能指点一下。废话不多说,上代码。先从简单的开始吧,这里是我一共用了3个fragment,这里就只贴出第一个的fragment的布局,别的两个基本一样,比较简朴。

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="center_horizontal|center_vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This the first fragment"
/>
</LinearLayout>

接下来的是使用fragment片段,这里也只贴出第一个的。

package com.example.fragments;

import com.example.viewfragtext.R;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; public class Fragmentone extends Fragment
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.fragment1, container, false);
}
}

接下来便要开始实现切换的功能,这里是底部切换组件(tabfoot)的布局。

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="44dp"
android:orientation="horizontal"
android:background="@drawable/tabfootbg"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:id="@+id/lay1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
style="@style/Linearlayout_Style"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/fratext1"
android:text="@string/chat"
android:textColor="@color/black"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/lay2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
style="@style/Linearlayout_Style">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/fratext2"
android:text="@string/find"
android:textColor="@color/black"/>
</LinearLayout>
<LinearLayout
android:id="@+id/lay3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
style="@style/Linearlayout_Style">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/fratext3"
android:text="@string/tongxunlu"
android:textColor="@color/black"/>
</LinearLayout>
</LinearLayout>

这里分别是自定义的style和color的代码。

<style name="Linearlayout_Style">
<item name="android:orientation">vertical</item>
<item name="android:gravity">center</item>
<item name="android:clickable">true</item>
<item name="android:onClick">LayoutOnclick</item>
</style> <?xml version="1.0" encoding="UTF-8"?>
<resources >
<color name="lightseagreen">#20b2aa</color><!--亮海蓝色 -->
<color name="black">#000000</color><!-- 黑色 --> </resources>

别的设计都完成了,马上对应的是MainActivity的设计,这是其布局

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"> <fragment
android:name="com.example.fragments.Fragmentone"
android:id="@+id/fragment1"
android:layout_height="0dp"
android:layout_weight="1.0"
android:layout_width="fill_parent"
/>
<fragment
android:name="com.example.fragments.Fragmenttwo"
android:id="@+id/fragment2"
android:layout_height="0dp"
android:layout_weight="1.0"
android:layout_width="fill_parent"
/>
<fragment
android:name="com.example.fragments.Fragmentthree"
android:id="@+id/fragment3"
android:layout_height="0dp"
android:layout_weight="1.0"
android:layout_width="fill_parent" /> <include layout="@layout/tabfoot"/> </LinearLayout>

最后遍是实现在主活动中实现点击底部切换和滑动的换的功能。这里滑动功能我是采用手势(Gesture)实现的。

 package com.example.viewfragtext;

 import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView; public class MainActivity extends FragmentActivity implements OnGestureListener
{
public static Fragment[] fragments;
public static LinearLayout[] linearLayouts;
public static TextView[] textViews;
/**定义手势检测实例*/
public static GestureDetector detector;
/**做标签,记录当前是哪个fragment*/
public int MARK=0;
/**定义手势两点之间的最小距离*/
final int DISTANT=50; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//分别实例化和初始化fragement、lineatlayout、textview
setfragment();
setlinearLayouts();
settextview();
//创建手势检测器
detector=new GestureDetector(this); } @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
/**初始化fragment*/
public void setfragment()
{
fragments=new Fragment[3];
fragments[0]=getSupportFragmentManager().findFragmentById(R.id.fragment1);
fragments[1]=getSupportFragmentManager().findFragmentById(R.id.fragment2);
fragments[2]=getSupportFragmentManager().findFragmentById(R.id.fragment3);
getSupportFragmentManager().beginTransaction().hide(fragments[0]).hide(fragments[1]).hide(fragments[2])
.show(fragments[0]).commit(); }
/**初始化linerlayout*/
public void setlinearLayouts()
{
linearLayouts=new LinearLayout[3];
linearLayouts[0]=(LinearLayout)findViewById(R.id.lay1);
linearLayouts[1]=(LinearLayout)findViewById(R.id.lay2);
linearLayouts[2]=(LinearLayout)findViewById(R.id.lay3);
linearLayouts[0].setBackgroundResource(R.drawable.lay_select_bg);
}
/**初始化textview*/
public void settextview()
{
textViews=new TextView[3];
textViews[0]=(TextView)findViewById(R.id.fratext1);
textViews[1]=(TextView)findViewById(R.id.fratext2);
textViews[2]=(TextView)findViewById(R.id.fratext3);
textViews[0].setTextColor(getResources().getColor(R.color.lightseagreen));
}
/**点击底部linerlayout实现切换fragment的效果*/
public void LayoutOnclick(View v)
{
resetlaybg();//每次点击都重置linearLayouts的背景、textViews字体颜色
switch (v.getId()) {
case R.id.lay1:
getSupportFragmentManager().beginTransaction().hide(fragments[0]).hide(fragments[1]).hide(fragments[2])
.show(fragments[0]).commit();
linearLayouts[0].setBackgroundResource(R.drawable.lay_select_bg);
textViews[0].setTextColor(getResources().getColor(R.color.lightseagreen));
MARK=0;
break; case R.id.lay2:
getSupportFragmentManager().beginTransaction().hide(fragments[0]).hide(fragments[1]).hide(fragments[2])
.show(fragments[1]).commit();
linearLayouts[1].setBackgroundResource(R.drawable.lay_select_bg);
textViews[1].setTextColor(getResources().getColor(R.color.lightseagreen));
MARK=1;
break;
case R.id.lay3:
getSupportFragmentManager().beginTransaction().hide(fragments[0]).hide(fragments[1]).hide(fragments[2])
.show(fragments[2]).commit();
linearLayouts[2].setBackgroundResource(R.drawable.lay_select_bg);
textViews[2].setTextColor(getResources().getColor(R.color.lightseagreen));
MARK=2;
break;
default:
break;
}
}
/**重置linearLayouts、textViews*/
public void resetlaybg()
{
for(int i=0;i<3;i++)
{
linearLayouts[i].setBackgroundResource(R.drawable.tabfootbg);
textViews[i].setTextColor(getResources().getColor(R.color.black));
} } @Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
//将该Activity上触碰事件交给GestureDetector处理
return detector.onTouchEvent(event);
}
@Override
public boolean onDown(MotionEvent arg0) {
// TODO Auto-generated method stub
return false;
} /**滑动切换效果的实现*/
@Override
public boolean onFling(MotionEvent arg0, MotionEvent arg1, float arg2,
float arg3) {
// TODO Auto-generated method stub
resetlaybg();
//当是Fragment0的时候
if(MARK==0)
{
if(arg1.getX()>arg0.getX()+DISTANT)
{
getSupportFragmentManager().beginTransaction().hide(fragments[0]).hide(fragments[1]).hide(fragments[2])
.show(fragments[1]).commit();
linearLayouts[1].setBackgroundResource(R.drawable.lay_select_bg);
textViews[1].setTextColor(getResources().getColor(R.color.lightseagreen));
MARK=1;
}
else
{
linearLayouts[0].setBackgroundResource(R.drawable.lay_select_bg);
textViews[0].setTextColor(getResources().getColor(R.color.black));
} }
//当是Fragment1的时候
else if (MARK==1)
{
if(arg1.getX()>arg0.getX()+DISTANT)
{
getSupportFragmentManager().beginTransaction().hide(fragments[0]).hide(fragments[1]).hide(fragments[2])
.show(fragments[2]).commit();
linearLayouts[2].setBackgroundResource(R.drawable.lay_select_bg);
textViews[2].setTextColor(getResources().getColor(R.color.lightseagreen));
MARK=2;
}
else if(arg0.getX()>arg1.getX()+DISTANT)
{
getSupportFragmentManager().beginTransaction().hide(fragments[0]).hide(fragments[1]).hide(fragments[2])
.show(fragments[0]).commit();
linearLayouts[0].setBackgroundResource(R.drawable.lay_select_bg);
textViews[0].setTextColor(getResources().getColor(R.color.lightseagreen));
MARK=0;
}
else
{
linearLayouts[1].setBackgroundResource(R.drawable.lay_select_bg);
textViews[1].setTextColor(getResources().getColor(R.color.black));
}
}
//当是Fragment2的时候
else if(MARK==2)
{
if(arg0.getX()>arg1.getX()+DISTANT)
{
getSupportFragmentManager().beginTransaction().hide(fragments[0]).hide(fragments[1]).hide(fragments[2])
.show(fragments[1]).commit();
linearLayouts[1].setBackgroundResource(R.drawable.lay_select_bg);
textViews[1].setTextColor(getResources().getColor(R.color.lightseagreen));
MARK=1;
}
else
{
linearLayouts[2].setBackgroundResource(R.drawable.lay_select_bg);
textViews[2].setTextColor(getResources().getColor(R.color.black));
}
}
return false;
} @Override
public void onLongPress(MotionEvent arg0) {
// TODO Auto-generated method stub } @Override
public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2,
float arg3) {
// TODO Auto-generated method stub
return false;
} @Override
public void onShowPress(MotionEvent arg0) {
// TODO Auto-generated method stub } @Override
public boolean onSingleTapUp(MotionEvent arg0) {
// TODO Auto-generated method stub
return false;
} }

最后的效果图

转载请注明出处:http://www.cnblogs.com/zhrxidian/p/3801545.html

Android之fragment点击切换和滑动切换结合的更多相关文章

  1. Android典型界面设计——ViewPage+Fragment实现区域顶部tab滑动切换

    一.问题描写叙述 本系列将结合案例应用,陆续向大家介绍一些Android典型界面的设计,首先说说tab导航,导航分为一层和两层(底部区块+区域内头部导航).主要实现方案有RadioGroup+View ...

  2. Android 使用TabLayout、ViewPager和Fragment实现顶部菜单可滑动切换

    效果图如下 首先,要使用控件需要添加design library,在Android Studio中添加 compile 'com.android.support:design:23.4.0' 然后是布 ...

  3. Android Studio之多个Activity的滑动切换(二)

    1.因为Android界面上的全部控件一般都位于Layout控件(比方RelativeLayout)之上,而布局控件能够设置响应touch事件,所以能够通过布局控件的setOnTouchListen来 ...

  4. Android防微信首页左右滑动切换

    大家看到微信首页切换效果有没有觉得很炫,滑动切换,点击底部bar瞬间切换,滑动切换渐变效果,线上效果图: 之前也在博客上看到别人的实现,再次基础上,我做了些优化.首先说下实现原理,大神略过,o(╯□╰ ...

  5. [Android] 使用Include布局+Fragment滑动切换屏幕

        前面的文章已经讲述了"随手拍"项目图像处理的技术部分,该篇文章主要是主界面的布局及屏幕滑动切换,并结合鸿洋大神的视频和郭神的第一行代码(强推两人Android博客),完毕了 ...

  6. Fragment+ViewPager实现仿微信点击和滑动切换界面

    这是在我写的新闻App中实现的界面切换 贴出切换界面的主要代码: xml代码: <span style="font-size:14px;"> <android.s ...

  7. Android自定义顶部栏及侧滑菜单和fragment+viewpag滑动切换的实现

    嘿嘿嘿,关于android滑动的操作,是不是经常都会用到呢. 我肯定也要学习一下啦. https://blog.csdn.net/u013184970/article/details/82882107 ...

  8. Android开发之ViewPager+ActionBar+Fragment实现响应式可滑动Tab

     今天我们要实现的这个效果呢,在Android的应用中十分地常见,我们可以看到下面两张图,无论是系统内置的联系人应用,还是AnyView的阅读器应用,我们总能找到这样的影子,当我们滑动屏幕时,Tab可 ...

  9. Android使用Fragment来实现ViewPager的功能(解决切换Fragment状态不保存)以及各个Fragment之间的通信

    以下内容为原创,转载请注明:http://www.cnblogs.com/tiantianbyconan/p/3364728.html 我前两天写过一篇博客<Android使用Fragment来 ...

随机推荐

  1. ASCII是指128个字符(不是256个)和ASCII Extended Characters(就是那些奇怪的外文字符)

    ASCII第一次以规范标准的型态发表是在1967年,最后一次更新则是在1986年,至今为止共定义了128个字元:其中33个字元无法显示(一些终端提供了扩展,使得这些字符可显示为诸如笑脸.扑克牌花式等8 ...

  2. SaaS是个不错的生意模式和创业的选择(独立SAAS厂商的三大优势)

    1. 软件独特而又强大,界面绝美2. 数据存在本国3. 数据不留底 4. 可随时寻求卖身或者合作,不受制于人 --------------------------------------------- ...

  3. 通过预编译头文件来提高C++ Builder的编译速度

    C++ Builder是最快的C++编译器之一,从编译速度来说也可以说是最快的win32C++编译器了.除了速度之外,C++builder的性能也在其它C++编译器的之上,但许多Delphi程序员仍受 ...

  4. 9.DataPager

    ListView搭配DataPager控件实现分页.有两种使用方式:一是将DataPager声明到ListView中:一种是DataPager\ListView没有嵌套关系,然后将DataPager的 ...

  5. POJ3087 Shuffle'm Up(模拟)

    题目链接. AC代码如下; #include <iostream> #include <cstdio> #include <cstring> #include &l ...

  6. POJ 2289 Jamie's Contact Groups(多重匹配+二分)

    题意: Jamie有很多联系人,但是很不方便管理,他想把这些联系人分成组,已知这些联系人可以被分到哪个组中去,而且要求每个组的联系人上限最小,即有一整数k,使每个组的联系人数都不大于k,问这个k最小是 ...

  7. 后缀自动机(SAM)模板

    struct SAM{ ],fa[maxn],len[maxn],cnt,last; void Init() { memset(ch,,sizeof(ch)); memset(fa,,sizeof(f ...

  8. 【链表】【模拟】Codeforces 706E Working routine

    题目链接: http://codeforces.com/problemset/problem/706/E 题目大意: 给一个N*M的矩阵,Q个操作,每次把两个同样大小的子矩阵交换,子矩阵左上角坐标分别 ...

  9. C语言赋值运算符

    赋值运算符: 分类: 简单赋值 int a ;  a=10; 复合运算符 int a ;  a+=4; 复合位运算符 int a; a&=1:

  10. 【背包型动态规划】灵魂分流药剂(soultap) 解题报告

    问题来源 BYVoid魔兽世界模拟赛 [问题描述] 皇家炼金师赫布瑞姆刚刚发明了一种用来折磨一切生物的新产品,灵魂分流药剂.灵魂分流药剂的妙处在于能够给服用者带来巨大的痛苦,但是却不会让服用者死去,而 ...