Android Fragment替换动画及后退处理



package com.example.fragmentanimation;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.KeyEvent;

import com.example.fragmentanimation.OneFragment.OnNewFragemntListener;

public class MainActivity extends FragmentActivity implements OnNewFragemntListener {
	
	private static final String TAG = MainActivity.class.getSimpleName();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		findView();
	}

	private void findView() {
		FragmentManager manager = getSupportFragmentManager();
		FragmentTransaction ft = manager.beginTransaction();
		ft.setCustomAnimations(R.anim.push_left_in,R.anim.push_left_in); 
		
		OneFragment oneFragment = (OneFragment) manager.findFragmentByTag("one");
		if(oneFragment==null){
			oneFragment = new OneFragment();
			Log.e(TAG, "new OneFragment");
		}
		
		ft.replace(R.id.fl_container, oneFragment, "one");
		ft.commit();
	}

	@Override
	public void onNewFragemnt() {
		
		FragmentManager manager = getSupportFragmentManager();
		FragmentTransaction ft = manager.beginTransaction();
		//设置替换和退栈的动画
		ft.setCustomAnimations(R.anim.push_left_in,R.anim.push_left_in,R.anim.back_left_in,R.anim.back_right_out); 
//		ft.setCustomAnimations(R.anim.push_left_in,R.anim.push_left_in); 
		
		TwoFragment twoFragment = (TwoFragment) manager.findFragmentByTag("two");
		if(twoFragment==null){
			Log.e(TAG, "new TwoFragment");
			twoFragment = new TwoFragment();
		}
		
		ft.replace(R.id.fl_container, twoFragment, "two");
		
		ft.addToBackStack(null);
		
		ft.commit();
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		
		FragmentManager fm = getSupportFragmentManager();
		
		int count = fm.getBackStackEntryCount();
		
		if(keyCode==KeyEvent.KEYCODE_BACK && count>0){
			
			Log.e(TAG, "back to list "+count);
			
			fm.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE); 
			
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
}



如果后退也需要动画的的话,需要用 FragmentTransaction.setCustomAnimations(R.anim.push_left_in,R.anim.push_left_in,R.anim.back_left_in,R.anim.back_right_out); 
同时设置 enter/exit 和popEnter/popExit动画


package com.example.fragmentanimation;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

public class OneFragment extends Fragment implements OnClickListener {

	private static final String TAG = OneFragment.class.getSimpleName();
	
	private OnNewFragemntListener mCallback;

	@Override
	public void onAttach(Activity activity) {
		try {
			mCallback = (OnNewFragemntListener) activity;
		} catch (ClassCastException e) {
			throw new ClassCastException(activity.toString()
					+ " must implement OnNewFragemntListener");
		}
		super.onAttach(activity);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		Log.e(TAG, "onCreateView");

		View view = inflater.inflate(R.layout.fragment_one, null);
		findView(view);
		
		return view;
	}

	private void findView(View view) {
		
		Button bt_open = (Button) view.findViewById(R.id.bt_open);
		bt_open.setOnClickListener(this);
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.bt_open:
			
			Log.e(TAG, "open TwoFragment");
			//回调Activity中的方法
			mCallback.onNewFragemnt();
			
			break;

		default:
			break;
		}
	}
	/**
	 * 后退 回调接口
	 */
	public interface OnNewFragemntListener {

		public void onNewFragemnt();
	}
	
}


package com.example.fragmentanimation;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

public class TwoFragment extends Fragment implements OnClickListener {

	private static final String TAG = TwoFragment.class.getSimpleName();
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		Log.e(TAG, "onCreateView");
		
		View view = inflater.inflate(R.layout.fragment_two, null);
		findView(view);
		
		return view;
	}

	private void findView(View view) {
		
		Button bt_back = (Button) view.findViewById(R.id.bt_back);
		
		bt_back.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.bt_back:
			
			Log.e(TAG, "back to previous");
			
			//后退
			getActivity().getSupportFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
			
			break;

		default:
			break;
		}
	}
	
}




工程源码下载:http://download.csdn.net/detail/fx_sky/6607155









  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
以下是一个例子: 1. 在res/layout文件夹中创建一个名为fragment_webview.xml的布局文件,用于包含WebView。 ``` <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <WebView android:id="@+id/webview" android:layout_width="match_parent" android:layout_height="match_parent" /> <ProgressBar android:id="@+id/progressBar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" /> </LinearLayout> ``` 2. 在Fragment类中,声明WebView和ProgressBar,并在onCreateView方法中将它们实例化并添加到布局中。 ``` public class WebViewFragment extends Fragment { private WebView mWebView; private ProgressBar mProgressBar; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_webview, container, false); mWebView = view.findViewById(R.id.webview); mProgressBar = view.findViewById(R.id.progressBar); mWebView.setWebViewClient(new MyWebViewClient()); mWebView.loadUrl("https://www.example.com"); return view; } private class MyWebViewClient extends WebViewClient { @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { mProgressBar.setVisibility(View.VISIBLE); } @Override public void onPageFinished(WebView view, String url) { mProgressBar.setVisibility(View.GONE); } } } ``` 3. 在WebViewClient中重写onPageStarted和onPageFinished方法,以显示和隐藏ProgressBar。 这样,当WebView开始加载页面时,ProgressBar将显示在中心,并在页面加载完成后自动消失。 注意:需要在AndroidManifest.xml文件中添加Internet权限,以便WebView可以加载网页。 ``` <uses-permission android:name="android.permission.INTERNET" /> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值