Fragment(一)-创新互联
MainActivity:
创新互联公司服务项目包括曲靖网站建设、曲靖网站制作、曲靖网页制作以及曲靖网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,曲靖网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到曲靖省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!package com.example.fragmentmanage;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener {
private Button btn1;
private Button btn2;
private FirstFragment firstFragment;
private SencondFragment sencondFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
setDefaultFragment();
}
private void initView() {
btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
}
private void setDefaultFragment() {
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
firstFragment = new FirstFragment();
transaction.replace(R.id.fragme_layout, firstFragment);
transaction.commit();
}
@Override
public void onClick(View arg0) {
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
switch (arg0.getId()) {
case R.id.btn1:
if (firstFragment == null) {
firstFragment = new FirstFragment();
}
transaction.replace(R.id.fragme_layout, firstFragment);
break;
case R.id.btn2:
if (sencondFragment == null) {
sencondFragment = new SencondFragment();
}
transaction.replace(R.id.fragme_layout, sencondFragment);
break;
default:
break;
}
transaction.commit();
}
}
activity_main:
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > android:id="@+id/fragme_layout" android:layout_width="wrap_content" android:layout_height="wrap_content" />
FirstFragment:
package com.example.fragmentmanage;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FirstFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_first, null);
}
}
SencondFragment:
package com.example.fragmentmanage;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FirstFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_first, null);
}
}
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
网站栏目:Fragment(一)-创新互联
转载来源:http://pwwzsj.com/article/dcipod.html