zoukankan      html  css  js  c++  java
  • 极致精简的fragment实现导航栏切换demo

    一个小demo。用button+fragment实现导航栏切换界面,适合刚接触的新手看一下。

    效果图




    点击第二个后




    源码:

    主界面

    <span style="font-size:18px;"><span style="font-size:14px;">package com.example.fragment;
    
    import android.app.FragmentManager;
    import android.app.FragmentTransaction;
    import android.os.Bundle;
    import android.support.v4.app.FragmentActivity;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    
    public class MainActivity extends FragmentActivity implements OnClickListener {
    
    	Button b1, b2;
    	FragmentManager fragmentManager;
    	FragmentTransaction fragmentTransaction;
    	ArticleFragment fragment;
    	ArticleFragment1 fragment1;
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    		fragmentManager = getFragmentManager();
    
    		fragment = new ArticleFragment();
    		fragment1 = new ArticleFragment1();
    		FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    		fragmentTransaction.replace(R.id.fg, fragment); 
    		fragmentTransaction.commit();
    
    		initView();
    	}
    
    	private void initView() {
    		// TODO Auto-generated method stub
    		b1 = (Button) findViewById(R.id.b1);
    		b2 = (Button) findViewById(R.id.b2);
    		b1.setOnClickListener(this);
    		b2.setOnClickListener(this);
    
    		
    	}
    
    	@Override
    	public void onClick(View v) {
    		// TODO Auto-generated method stub
    		switch (v.getId()) {
    		case R.id.b1:
    			FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    			fragmentTransaction.replace(R.id.fg, fragment); 
    			fragmentTransaction.commit();
    			break;
    		case R.id.b2:
    			FragmentTransaction fragmentTransaction1 = fragmentManager.beginTransaction();
    			fragmentTransaction1.replace(R.id.fg, fragment1); 
    			fragmentTransaction1.commit();
    			break;
    		default:
    			break;
    		}
    	}
    }
    


    主界面布局:

    <span style="font-size:18px;"><span style="font-size:14px;"><RelativeLayout 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"
        tools:context="${relativePackage}.${activityClass}" >
    
        <LinearLayout
            android:id="@+id/fg"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_alignParentBottom="true"
            android:weightSum="1" >
    
            <Button
                android:id="@+id/b1"
                android:layout_width="0dp"
                android:layout_height="40dp"
                android:layout_weight="0.5"
                android:text="第一个" />
    
            <Button
                android:id="@+id/b2"
                android:layout_width="0dp"
                android:layout_height="40dp"
                android:layout_weight="0.5"
                android:text="第二个" />
        </LinearLayout>
    
    </RelativeLayout>

    第一个fragment界面

    <span style="font-size:18px;"><span style="font-size:14px;">package com.example.fragment;
    
    import android.app.Fragment;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    
    public class ArticleFragment extends Fragment {
    
    	@Override
    	public View onCreateView(LayoutInflater inflater, ViewGroup container,
    			Bundle savedInstanceState) {
    		// TODO Auto-generated method stub
    		return inflater.inflate(R.layout.article_view, container,false);
    	}
    	
    }
    

    第一个fragment的布局
    <span style="font-size:18px;"><span style="font-size:14px;"><?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" >
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent" 
            >
            <ImageView 
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/homepage"/>
            
        </ScrollView>
       
    </LinearLayout>
    
    


    第二个Fragment界面

    <span style="font-size:18px;"><span style="font-size:14px;">package com.example.fragment;
    
    import android.app.Fragment;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    
    public class ArticleFragment1 extends Fragment {
    
    	@Override
    	public View onCreateView(LayoutInflater inflater, ViewGroup container,
    			Bundle savedInstanceState) {
    		// TODO Auto-generated method stub
    		return inflater.inflate(R.layout.article_view1, container,false);
    	}
    	
    }
    


    第二个fragment的布局文件
    <span style="font-size:18px;"><span style="font-size:14px;"><?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" >
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent" 
            >
            <ImageView 
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/section"/>
            
        </ScrollView>
       
            
    
    </LinearLayout>
    


    源码下载:

    http://download.csdn.net/detail/shoneworn/8316915




  • 相关阅读:
    注册以及密码验证
    轮播图,渐显,可以左右点击
    节点移动
    数据持久化
    Objective-C Autorelease Pool 的实现原理(转)
    iOS应用架构谈 view层的组织和调用方案(转)
    iOS 开源项目
    iOS开发系列--无限循环的图片浏览器
    富文本常用封装(NSAttributedString浅析)(转)
    OS开发UI篇—ios应用数据存储方式(XML属性列表-plist)(转)
  • 原文地址:https://www.cnblogs.com/shoneworn/p/5078119.html
Copyright © 2011-2022 走看看