动态添加fragment与手势切换(三)(android)
2014-09-23 16:48
435 查看
继续,现在开始写最重要的,实现代码了,我们需要建立一个activity ,注意我的例子中的activity 不是主页,这里直接上代码,看注析吧。
1、activity 的布局如下:
2、Java代码
1、activity 的布局如下:
<LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:background="#ffffff" xmlns:android="http://schemas.android.com/apk/res/android"> <RelativeLayout android:id="@+id/relativeLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/wbg" > <!-- 此layout 可以显示主要的信息内容 --> <FrameLayout android:id="@+id/frameLayout1" android:layout_width="match_parent" android:layout_height="wrap_content" > </FrameLayout> <!-- 此layout 可以显示的fragment数量与当前fragment位置--> <FrameLayout android:id="@+id/frameLayout2" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginBottom="100dp"> <LinearLayout android:id="@+id/layout_fragment_num" android:layout_width="match_parent" android:layout_height="44dp" android:orientation="horizontal" android:layout_gravity="bottom" android:background="@color/gray" android:gravity="center_vertical|center_horizontal" > </LinearLayout> </FrameLayout> <!-- 此layout 可以显示当前内容的附属信息--> <FrameLayout android:id="@+id/frameLayout3" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginBottom="0dp" > <LinearLayout android:id="@+id/layout_fragment_bottom" android:layout_width="match_parent" android:layout_height="100dp" android:orientation="horizontal" android:layout_gravity="bottom" android:background="#cccccc" > </LinearLayout> </FrameLayout> </RelativeLayout> </LinearLayout>
2、Java代码
package com.example.testandroid; import android.content.Context; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentTransaction; import android.view.GestureDetector; import android.view.GestureDetector.OnGestureListener; import android.view.Menu; import android.view.MenuItem; import android.view.MotionEvent; import android.view.Window; import android.widget.FrameLayout; import android.widget.LinearLayout; import android.widget.TextView; public class FragmentChangeActivity extends FragmentActivity implements OnGestureListener { private static TextView[] numTexts; public static Fragment[] fragments1; public static Fragment[] fragments2; /**定义手势检测实例*/ public static GestureDetector detector; /**做标签,记录当前是哪个fragment*/ public int MARK=0; /**定义手势两点之间的最小距离*/ final int DISTANT=50; private LinearLayout layout_fragment_num; private LinearLayout layout_fragment_bottom; private FrameLayout frameLayout1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE);//隐藏标题 setContentView(R.layout.activity_fragment_change); frameLayout1=(FrameLayout) this.findViewById(R.id.frameLayout1); //改变layout的透明度 layout_fragment_num=(LinearLayout) this.findViewById(R.id.layout_fragment_num); layout_fragment_num.getBackground().setAlpha(100);//0-255,透明-不透明 //改变layout的透明度 layout_fragment_bottom=(LinearLayout) this.findViewById(R.id.layout_fragment_bottom); layout_fragment_bottom.getBackground().setAlpha(0); //假设当前需要动态添加3个 initTextViewToLayout(3,this); changeTextViewColor(0); setfragment(); //创建手势检测器 // 注意前一个this代表的是一个Context,后一个this代表的是一个OnGestureListener。 detector=new GestureDetector(this,this); } /** * 动态添加TEXTVIEW */ private void initTextViewToLayout(int num,Context context){ numTexts=new TextView[num]; for(int i=0;i<num;i++){ TextView tv=new TextView(context); tv.setText("●"); tv.setTextColor(getResources().getColor(R.color.gray1)); numTexts[i]=tv; layout_fragment_num.addView(tv); } } /** * 改变TEXTVIEW的颜色 * @param mark */ private void changeTextViewColor(int mark){ for(int i=0;i<numTexts.length;i++){ numTexts[i].setTextColor(getResources().getColor(R.color.gray1)); } numTexts[mark].setTextColor(getResources().getColor(R.color.white)); } /**初始化fragment*/ public void setfragment() { fragments1=new Fragment[3]; fragments2=new Fragment[3]; fragments1[0]=new TabOfFragment01(); fragments1[1]=new TabOfFragment01(); fragments1[2]=new TabOfFragment01(); fragments2[0]=new TabOfFragment02(); fragments2[1]=new TabOfFragment02(); fragments2[2]=new TabOfFragment02(); getSupportFragmentManager().beginTransaction().replace(R.id.frameLayout1, fragments1[0]).commit(); getSupportFragmentManager().beginTransaction().replace(R.id.layout_fragment_bottom, fragments2[0]).commit(); } /** * 设置动画 * @param ft * @param slefId */ private void setAnim(FragmentTransaction ft,int slefId){ if(MARK>slefId) ft.setCustomAnimations(R.anim.in_to_left, R.anim.out_to_right); if(MARK<slefId) ft.setCustomAnimations(R.anim.in_from_right, R.anim.out_to_left); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.fragment_change, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } @Override public boolean onTouchEvent(MotionEvent event) { // TODO Auto-generated method stub //将该Activity上触碰事件交给GestureDetector处理 return detector.onTouchEvent(event); } @Override public boolean onDown(MotionEvent e) { // TODO Auto-generated method stub return false; } @Override public void onShowPress(MotionEvent e) { // TODO Auto-generated method stub } @Override public boolean onSingleTapUp(MotionEvent e) { // TODO Auto-generated method stub return false; } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { // TODO Auto-generated method stub return false; } @Override public void onLongPress(MotionEvent e) { // TODO Auto-generated method stub } @Override public boolean onFling(MotionEvent arg0, MotionEvent arg1, float velocityX, float velocityY) { // TODO Auto-generated method stub FragmentTransaction ft=getSupportFragmentManager().beginTransaction(); FragmentTransaction ft1=getSupportFragmentManager().beginTransaction(); Bundle bundle = new Bundle(); int tmpMark=1; System.out.println(arg0.getX()+"---"+arg1.getX()); //当是Fragment0的时候 if(MARK==0) { if(arg0.getX()>arg1.getX()+DISTANT) { tmpMark=1; bundle.putString("pageNo", "1"); fragments1[tmpMark].setArguments(bundle); setAnim(ft,tmpMark); setAnim(ft1,tmpMark); ft.replace(R.id.frameLayout1, fragments1[tmpMark]).commit(); ft1.replace(R.id.layout_fragment_bottom, fragments2[tmpMark]).commit(); this.changeTextViewColor(tmpMark); MARK=tmpMark; } else { } } //当是Fragment1的时候 else if (MARK>=1 && MARK<numTexts.length-1) { if(arg0.getX()>arg1.getX()+DISTANT) { tmpMark=MARK+1; bundle.putString("pageNo", ""+(tmpMark+1)); fragments1[tmpMark].setArguments(bundle); fragments2[tmpMark].setArguments(bundle); setAnim(ft,tmpMark); setAnim(ft1,tmpMark); ft.replace(R.id.frameLayout1, fragments1[tmpMark]).commit(); ft1.replace(R.id.layout_fragment_bottom, fragments2[tmpMark]).commit(); this.changeTextViewColor(tmpMark); MARK=tmpMark; } else if(arg1.getX()>arg0.getX()+DISTANT) { tmpMark=MARK-1; bundle.putString("pageNo", ""+(tmpMark+1)); fragments1[tmpMark].setArguments(bundle); fragments2[tmpMark].setArguments(bundle); setAnim(ft,tmpMark); setAnim(ft1,tmpMark); ft.replace(R.id.frameLayout1, fragments1[tmpMark]).commit(); ft1.replace(R.id.layout_fragment_bottom, fragments2[tmpMark]).commit(); this.changeTextViewColor(tmpMark); MARK=tmpMark; } else { } } //当是Fragment5的时候 else if(MARK==numTexts.length-1) { if(arg1.getX()>arg0.getX()+DISTANT) { tmpMark=(numTexts.length-1)-1; bundle.putString("pageNo", ""+(tmpMark+1)); fragments1[tmpMark].setArguments(bundle); fragments2[tmpMark].setArguments(bundle); setAnim(ft,tmpMark); setAnim(ft1,tmpMark); ft.replace(R.id.frameLayout1, fragments1[tmpMark]).commit(); ft1.replace(R.id.layout_fragment_bottom, fragments2[tmpMark]).commit(); this.changeTextViewColor(tmpMark); MARK=tmpMark; } else { } } return false; } }
相关文章推荐
- 动态添加fragment与手势切换(二)(android)
- 动态添加fragment与手势切换(一)(android)
- android--解--它们的定义tabhost(动态添加的选项+用自己的主动性横向滑动标签+手势切换标签页和内容特征)
- android--解决方案--自定义tabhost(动态添加选项+带自动水平滑动选项卡+手势切换选项卡及内容功能)
- Android Fragment的动态添加,Fragment,Activity以及Fragment之间的通信方式
- Android之ViewPager+Fragment实现页面点击切换和手势滑动
- Android Fragment基础之动态添加Fragment 实现两个Fragment之间的通信
- Android 动态添加Fragment
- android-动态添加包含WebView的Fragment显示空白
- Android中Fragment的动态加载和双屏切换问题
- Android 动态添加Fragment的实例代码
- Fragment 实现tab页卡切换并保存界面状态,动态添加Fragment
- Android Fragment详解,动态添加+静态使用+图+代码
- android动态添加Fragment_模拟微信界面
- 基于android的网络音乐播放器-添加viewpager和fragment实现滑动切换多个界面(二)
- Android Fragment用法详解(2)--动态添加Fragment
- android之碎片(fragment)二(动态添加)
- Android编程心得-使用ActionBar+Fragment+ViewPager实现动态切换Menu效果
- Android之ViewPager+Fragment实现页面点击切换和手势滑动
- Android之Fragment入门二(动态添加碎片)