Fragment实现点击替换翻页

来源:互联网 发布:海康网络摄像头价位 编辑:程序博客网 时间:2024/06/05 20:50

效果图



主界面java代码

package com.xiaoke.fragment;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;//布局文件中用FrameLayout,由于不是用的viewpager所有导v4包里面的public class MainActivity extends Activity implements OnClickListener {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//获取点击事件Button btn;btn = (Button) findViewById(R.id.btn_a1_id);btn.setOnClickListener(this);btn = (Button) findViewById(R.id.btn_a2_id);btn.setOnClickListener(this);btn = (Button) findViewById(R.id.btn_a3_id);btn.setOnClickListener(this);btn = (Button) findViewById(R.id.btn_a4_id);btn.setOnClickListener(this);// addToBackStack添加到回退栈,addToBackStack与ft.add(R.id.fragment, new// MyFragment())效果相当// ft.addToBackStack("test");}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.btn_a1_id: {//添加界面前的赋值FragmentManager fm = this.getFragmentManager();FragmentTransaction ft = fm.beginTransaction();//替换布局中的FrameLayout,是替换不是覆盖ft.replace(R.id.fragment, new Yaowen());ft.commit();}break;case R.id.btn_a2_id: {FragmentManager fm = this.getFragmentManager();FragmentTransaction ft = fm.beginTransaction();ft.replace(R.id.fragment, new ShiPing());ft.commit();}break;case R.id.btn_a3_id: {FragmentManager fm = this.getFragmentManager();FragmentTransaction ft = fm.beginTransaction();ft.replace(R.id.fragment, new Yule());ft.commit();}break;case R.id.btn_a4_id: {FragmentManager fm = this.getFragmentManager();FragmentTransaction ft = fm.beginTransaction();ft.replace(R.id.fragment, new Tiyu());ft.commit();}break;}}}

其他页面界面,由于其他页面只是名字不同,就只贴一个上去

package com.xiaoke.fragment;import android.app.Fragment;import android.graphics.Color;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.ImageView;import android.widget.TextView;public class ShiPing extends Fragment {@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {//获取自己创建的布局文件View iv=(View) inflater.inflate(R.layout.a, null);iv.setBackgroundColor(Color.BLUE);return iv;}@Overridepublic void onViewCreated(View view, Bundle savedInstanceState) {//找到布局文件中的控件并对布局文件中的控件赋值ImageView iv=(ImageView)view.findViewById(R.id.iv_a1_id);TextView tv=(TextView) view.findViewById(R.id.tv_a1_id);tv.setText("视频");iv.setImageResource(R.drawable.ic_launcher);}}

布局文件

主界面文件

<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"    android:orientation="vertical" >    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal" >        <Button            android:id="@+id/btn_a1_id"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"             android:text="要闻"            />        <Button            android:id="@+id/btn_a2_id"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"             android:text="视频"/>        <Button            android:id="@+id/btn_a3_id"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"             android:text="娱乐"/>        <Button            android:id="@+id/btn_a4_id"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="体育" />    </LinearLayout>    <FrameLayout        android:id="@+id/fragment"        android:layout_width="match_parent"        android:layout_height="match_parent" >    </FrameLayout></LinearLayout>

其他界面

<?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" >    <ImageView        android:id="@+id/iv_a1_id"        android:layout_width="wrap_content"        android:layout_height="wrap_content" />    <TextView         android:id="@+id/tv_a1_id"        android:layout_width="match_parent"        android:layout_height="match_parent"        /></LinearLayout>


0 0
原创粉丝点击