Fragment案例
Fragment也是常用的在Android中,之前只是总结了下知识点,今天写了个例子,对只是巩固了下,下面是例子:
MainActivity.class
package com.example.fragmentdemo1;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.example.fragment.A;
public class MainActivity extends FragmentActivity implements OnClickListener{
private LinearLayout ll3,ll2,ll4,ll1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ll1 = (LinearLayout) findViewById(R.id.ll1);
ll1.setOnClickListener(this);
ll2 = (LinearLayout) findViewById(R.id.ll2);
ll2.setOnClickListener(this);
ll3 = (LinearLayout) findViewById(R.id.ll3);
ll3.setOnClickListener(this);
ll4 = (LinearLayout) findViewById(R.id.ll4);
ll4.setOnClickListener(this);
onClick(ll1);
}
private A fgA,fgB,fgC,fgD;
@Override
public void onClick(View v) {
ImageView icon;
switch (v.getId()) {
case R.id.ll1:{
if(R.id.ll1 != curFragMent){
this.resetIconBackground();
curFragMent = R.id.ll1;
icon = (ImageView) ll1.findViewById(R.id.iv1);
icon.setBackgroundResource(R.drawable.icon);
if(fgD == null){
fgD = new A();
}
getFragmentManager().beginTransaction().replace(R.id.fl_main/*fl_drivingcar_main*/,fgD).commit();
}
break;
}
case R.id.ll2:{
if(R.id.ll2 != curFragMent){
this.resetIconBackground();
curFragMent = R.id.ll2;
icon = (ImageView) ll2.findViewById(R.id.iv2);
icon.setBackgroundResource(R.drawable.icon);
if(fgC == null){
fgC = new A(2);
}
getFragmentManager().beginTransaction().replace(R.id.fl_main/*fl_drivingcar_main*/,fgC).commit();
}
break;
}
case R.id.ll3:{//消息
if(R.id.ll3 != curFragMent){
this.resetIconBackground();
curFragMent = R.id.ll3;
icon = (ImageView) ll3.findViewById(R.id.iv3);
icon.setBackgroundResource(R.drawable.icon);
if(fgA == null){
fgA = new A(3);
}
// fgA.enableTitleListener();\
getFragmentManager().beginTransaction().replace(R.id.fl_main/*fl_drivingcar_main*/,fgA).commit();
}
break;
}
case R.id.ll4:{
if(R.id.ll4 != curFragMent){
this.resetIconBackground();
curFragMent = R.id.ll4;
icon = (ImageView) ll4.findViewById(R.id.iv4);
icon.setBackgroundResource(R.drawable.icon);
if(fgB == null){
fgB = new A(4);
}
getFragmentManager().beginTransaction().replace(R.id.fl_main/*fl_drivingcar_main*/, fgB).commit();
}
break;
}
}
}
private int curFragMent = 0;
private void resetIconBackground(){
ImageView icon;
switch (curFragMent) {
case R.id.ll1:{
icon = (ImageView) ll1.findViewById(R.id.iv1);
icon.setBackgroundResource(R.drawable.ic_launcher);
break;
}
case R.id.ll2:{
icon = (ImageView) ll2.findViewById(R.id.iv2);
icon.setBackgroundResource(R.drawable.ic_launcher);
break;
}
case R.id.ll3:{
icon = (ImageView) ll3.findViewById(R.id.iv3);
icon.setBackgroundResource(R.drawable.ic_launcher);
break;
}
case R.id.ll4:{
icon = (ImageView) ll4.findViewById(R.id.iv4);
icon.setBackgroundResource(R.drawable.ic_launcher);
break;
}
}
}
}
A.class
package com.example.fragment;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.example.fragmentdemo1.R;
public class A extends Fragment{
int tv_type;
TextView tv;
public A() {
}
public A(int tv_type) {
this.tv_type = tv_type;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.activity_content, null);
tv=(TextView) view.findViewById(R.id.tv_type);
tv.setText(tv_type+"");
return view;
}
}
activity_bottom.xml
<?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="77dp"
android:orientation="vertical" >
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#D3D3D3" />
<LinearLayout
android:baselineAligned="false"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ECECEC"
android:orientation="horizontal"
android:paddingBottom="10dp"
android:paddingTop="10dp" >
<LinearLayout
android:id="@+id/ll1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:orientation="vertical" >
<ImageView
android:id="@+id/iv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ic_launcher" />
<TextView
android:clickable="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:id="@+id/ll2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center" >
<ImageView
android:id="@+id/iv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ic_launcher" />
<!-- <TextView
android:id="@+id/msg_number"
android:clickable="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ic_launcher"
android:layout_alignBaseline="@id/message_icon"
android:gravity="center"
android:layout_marginLeft="15dp"
android:textSize="8sp"/> -->
</RelativeLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="222"/>
</LinearLayout>
<LinearLayout
android:id="@+id/ll3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:orientation="vertical" >
<ImageView
android:id="@+id/iv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ic_launcher" />
<TextView
android:clickable="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="不错"/>
</LinearLayout>
<LinearLayout
android:id="@+id/ll4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:orientation="vertical" >
<ImageView
android:id="@+id/iv4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ic_launcher" />
<TextView
android:clickable="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="444"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
activity_content.xml
<?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="horizontal"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是内容"/>
<TextView
android:id="@+id/tv_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是标识"/>
</LinearLayout>
activity_main.xml
<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">
<include
android:layout_width="match_parent"
android:layout_height="57dp"
layout="@layout/activity_title"
android:layout_alignParentTop="true" />
<FrameLayout
android:id="@+id/fl_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<include
android:layout_width="match_parent"
android:layout_height="57dp"
layout="@layout/activity_bottom"
android:layout_alignParentBottom="true" />
</RelativeLayout>
activity_title.xml
<?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="6dp"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="12dp"
android:text="111" />
</LinearLayout>
蒸鹅心鸭: 额,为什么我这边安装了就有了,不用配置环境变量
m0_52537868: 始终报错,楼主能帮忙解答一下吗?
爱吃西蓝花i: 写的很好呀,为什么没有热度呢,加油喔博主!kisskiss
Crypto_Thinker: Execution failed for task ':app:installDebug'. > com.android.builder.testing.api.DeviceException: No connected devices!这是什么意思啊,博主
water___Wang: 受教了~