Android碎片Fragment的点击切换
来源:互联网 发布:淘宝店推广引流的技巧 编辑:程序博客网 时间:2024/05/17 00:08
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:) @Overrideprotected 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 stubFragmentManager fragmentManager=getSupportFragmentManager();FragmentTransaction fragmentTransaction =fragmentManager.beginTransaction();fragmentTransaction.replace(R.id.myfragment, fragment);fragmentTransaction.addToBackStack(null);//返回栈 fragmentTransaction.commit();}@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubswitch (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;}}@Overridepublic 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;}@Overridepublic 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;
}
}
阅读全文
0 0
- Android碎片Fragment的点击切换
- Android Fragment之间的点击切换
- ActionBar + Fragment 实现两个碎片的切换
- Fragment碎片切换
- Android碎片(Fragment)的创建
- fragment的点击切换页面
- android 点击按钮切换不同的fragment布局
- android 点击底部图片进行切换不同的Fragment
- android fragment的切换
- Activity与在Android的碎片Fragment
- Android Fragment 关于碎片的一切
- Android嵌入页面的碎片Fragment
- android之碎片fragment的使用
- Android Fragment 碎片的初步使用
- Android的fragment页面点击按钮切换到下一个fragment页面,由主activity控制
- Android中如何在Fragment中添加点击事件切换新的Fragment
- Android简单的Fragment+Viewpager切换(可滑动和点击切换)
- Android Fragment 碎片
- HDU 1250 Hat's Fibonacci
- ubuntu安装tftp
- Largest Rectangle in a Histogram
- 【Bzoj1060】时态同步
- windows 下 sourceTree 乱码问题
- Android碎片Fragment的点击切换
- 和为S的连续正数序列(java版)
- POJ 1313 Booklet Printing 笔记
- 直接插入排序与希尔排序
- 第一次写博客
- 性能测试中用LambdaProbe监控Tomcat
- 百度的科学计算器(简单)
- 机器学习torch代码训练过程中常见bug解决经验
- laravel(二)-目录结构