android动态添加Fragment_模拟微信界面
2019-04-13 21:17发布
生成海报
一、项目目录结构
二、activity_main.xml界面
三、activity_main.xml代码
四、fragment_wx.xml代码
五、fragment_contact.xml代码
七、fragment_discover.xml代码
八、fragment_me.xml代码
九、MainActivity.java代码package com.zgs.WeiXin;
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{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// [1]找到我们关心的控件
Button btn_contact = (Button) findViewById(R.id.btn_contact);
Button btn_discover = (Button) findViewById(R.id.btn_discover);
Button btn_me = (Button) findViewById(R.id.btn_me);
Button btn_wx = (Button) findViewById(R.id.btn_wx);
// [2]给按钮设置点击事件
btn_contact.setOnClickListener(this);
btn_discover.setOnClickListener(this);
btn_me.setOnClickListener(this);
btn_wx.setOnClickListener(this);
}
// [3]具体判断一下我点击的是哪个按钮
@Override
public void onClick(View v) {
//[4]获取fragment的管理者
FragmentManager fragmentManager = getFragmentManager();
//[5]开启事物
FragmentTransaction transaction = fragmentManager.beginTransaction();
switch (v.getId()) {
case R.id.btn_wx: // 说明点击了微信按钮
transaction.replace(R.id.ll, new WxFragment());
break;
case R.id.btn_contact: // 说明点击了微信按钮
transaction.replace(R.id.ll, new ContactFragment());
break;
case R.id.btn_discover: // 说明点击了微信按钮
transaction.replace(R.id.ll, new DiscoverFragment());
break;
case R.id.btn_me: // 说明点击了微信按钮
transaction.replace(R.id.ll, new MeFragment());
break;
}
//[6]最后一布 记得 提交事物
transaction.commit();
}
}
十、WxFragment.java代码package com.zgs.WeiXin;
import android.annotation.SuppressLint;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Toast;
@SuppressLint("InflateParams")
public class WxFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_wx, null);
view.findViewById(R.id.btn_test).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "我被点击了!!!", Toast.LENGTH_SHORT).show();
}
});
return view;
}
}
十一、ContactFragment.java代码package com.zgs.WeiXin;
import android.annotation.SuppressLint;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@SuppressLint("InflateParams")
public class ContactFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_contact, null);
return view;
}
}
十二、DiscoverFragment.java代码package com.zgs.WeiXin;
import android.annotation.SuppressLint;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@SuppressLint("InflateParams")
public class DiscoverFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_discover, null);
return view;
}
}
十三、MeFragment.java代码package com.zgs.WeiXin;
import android.annotation.SuppressLint;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@SuppressLint("InflateParams")
public class MeFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_me, null);
return view;
}
}
十四、效果演示
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮