安卓创建一个fragment的样例
这是一只菜鸡的博客!看了网上的关于fragment的视频讲解,觉得fragmen也就那么回事 ,但是第一次真正在Android Studio上写fragment时,简直是中了毒。这中间的过程就不说了 。。。。
初写(一个极其简单样例。。。)
1.fragment动态切换
2.fragment静态加载
3.activity与fragment事件共享
一。样例截图
二。结构
三。布局文件
1.下面是activity_main.xml布局。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
android:baselineAligned="false">
<fragment
android:id="@+id/choice_fragment"
android:name="com.example.zwh.fragmenttest.choice_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<FrameLayout
android:id="@+id/detail_event"
android:layout_width="match_parent"
android:layout_height="49dp"
android:layout_weight="9"
android:orientation="vertical">
</FrameLayout>
</LinearLayout>
2.接下来是fragment_choice_fragment.xml
<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="wrap_content"
android:orientation="horizontal"
tools:context=".choice_fragment">
<TextView
android:id="@+id/hot_spot"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="热点" />
<TextView
android:id="@+id/news"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="新闻" />
<TextView
android:id="@+id/accent"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="事实" />
<TextView
android:id="@+id/set"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="设置" />
<!-- TODO: Update blank fragment layout -->
</LinearLayout>
上面的就是下面这四个TextView了
3.fragment_choice_1.xml布局文件
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".Choice_1">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这是第一个fragment"
android:textSize="20dp"/>
</LinearLayout>
就只有一个TextView而已。
四。Java类
1.choice_fragment.java
public class choice_fragment extends Fragment {
OnClick clickListener;
public TextView textView1;
public TextView textView2;
public TextView textView3;
public TextView textView4;
public interface OnClick {
public void onClick(int id);
}
public choice_fragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_choice_fragment, container, false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
textView1 = getActivity().findViewById(R.id.hot_spot);
textView2 = getActivity().findViewById(R.id.news);
textView3 = getActivity().findViewById(R.id.accent);
textView4 = getActivity().findViewById(R.id.set);
textView1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clickListener.onClick(v.getId());
}
});
textView2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clickListener.onClick(v.getId());
}
});
textView3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clickListener.onClick(v.getId());
}
});
textView4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clickListener.onClick(v.getId());
}
});
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
clickListener = (OnClick) context;
} catch (ClassCastException e) {
throw new ClassCastException(" must implement OnArticleSelectedListener");
}
}
@Override
public void onDetach() {
super.onDetach();
}
}
2.MainActivity.java
package com.example.zwh.fragmenttest;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements choice_fragment.OnClick {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.activity_main);
FragmentManager fragmentManager;
FragmentTransaction fragmentTransaction;
fragmentManager = getSupportFragmentManager();
fragmentTransaction=fragmentManager.beginTransaction();
Choice_1 choice_1=new Choice_1();
fragmentTransaction.add(R.id.detail_event,choice_1).commit();
}
@Override
public void onClick(int id) {
FragmentManager fragmentManager=getSupportFragmentManager();
FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
switch (id) {
case R.id.hot_spot:
Choice_1 choice_1 = new Choice_1();
fragmentTransaction.replace(R.id.detail_event, choice_1);
Toast.makeText(this,"hotspot",Toast.LENGTH_SHORT).show();
break;
case R.id.news:
Choice_2 choice_2 = new Choice_2();
fragmentTransaction.replace(R.id.detail_event, choice_2);
Toast.makeText(this,"news",Toast.LENGTH_SHORT).show();
break;
case R.id.accent:
Choice_3 choice_3 = new Choice_3();
fragmentTransaction.replace(R.id.detail_event, choice_3);
Toast.makeText(this,"accent",Toast.LENGTH_SHORT).show();
break;
case R.id.set:
Choice_4 choice_4 = new Choice_4();
fragmentTransaction.replace(R.id.detail_event, choice_4);
Toast.makeText(this,"set",Toast.LENGTH_SHORT).show();
break;
}
fragmentTransaction.commit();
}
}
3.Choice.java
主要差别就是引入布局不同而已,这个是R.layout.fragment_choice_1
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_choice_1, container, false);
}
五。[点这里看]原作者的文章是如何实现activity与fragment事件共享的。(http://www.cnblogs.com/mengdd/archive/2013/01/11/2856374.html)
六。小菜鸡的博客。