Android碎片Fragment的点击切换
程序员文章站
2022-05-14 21:08:22
...
Fragment碎片的简单使用
1.MainActivity
点击切换Fragment碎片
package com.example.fragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends ActionBarActivity implements OnClickListener{
//知识点1:Fragment 碎片
// 核心 Activity与view的结合体
// 既可以作逻辑处理等价于Activity
// 又可以作view 试图控件
Button button1,button2;
// 使用步骤
// 1.新建类去继承Fragment
// 2.重写oncrateview
// 3.新建布局文件并修改2中方法
// 4.在Activity的布局文件里使用(name:)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1=(Button) findViewById(R.id.button1);
button2=(Button) findViewById(R.id.button2);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
}
public void button2(View view){
}
public void button1(View view){
}
//Fragment替换
private void replaceFragment(Fragment fragment) {
// TODO Auto-generated method stub
FragmentManager fragmentManager=getSupportFragmentManager();
FragmentTransaction fragmentTransaction =fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.myfragment, fragment);
fragmentTransaction.addToBackStack(null);//返回栈
fragmentTransaction.commit();
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch (arg0.getId()) {
case R.id.button1:
FragmentManager fragmentManager=getSupportFragmentManager();
FragmentTransaction fragmentTransaction =fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.myfragment, new MyFragment1());
fragmentTransaction.commit();
break;
case R.id.button2:
replaceFragment(new MyFragment2());
break;
default:
break;
}
}
@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;
}
@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);
}
}
2.布局文件里效果和代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context="com.example.fragment.MainActivity" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="button1"
android:text="Button1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="button2"
android:text="Button2" />
</LinearLayout>
<fragment
android:id="@+id/myfragment"
android:name="com.example.fragment.MyFragment1"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1" />
</LinearLayout>
3.Fragment1第一个碎片
R.layout.myfragment1为要加载的布局
第二个碎片同理
package com.example.fragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class MyFragment1 extends Fragment {
// Fragment 特点一:必须依赖Activity
@Override
//onCreateView加载布局用的
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View v=inflater.from(getActivity()).inflate(R.layout.myfragment1, container, false);
return v;
}
}
推荐阅读
-
Android实现可点击展开的TextView
-
Android 入门第十讲02-广播(广播概述,使用方法(系统广播,自定义广播,两个activity之间的交互和传值),EventBus使用方法,数据传递,线程切换,Android的系统广播大全)
-
ImageView点击可变暗的实例代码(android代码技巧)
-
Android编程实现左右滑动切换背景的方法
-
Android编程之点击按钮的响应方式小结【3种方式】
-
Android点击EditText文本框之外任何地方隐藏键盘的解决办法
-
Android使用Fragment打造万能页面切换框架
-
Android 自定义view模板并实现点击事件的回调
-
Android Activity与Fragment之间的跳转实例详解
-
Android编程实现两个Activity相互切换而不使用onCreate()的方法