继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

详细到让你尖叫的技术(最简单的架构fragment+radioGroup)

繁华开满天机
关注TA
已关注
手记 134
粉丝 15
获赞 68

你只需要复制粘贴,便可运行
1.首先:欢迎界面
<?xml version="1.0" encoding="utf-8"?>
<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"android:background="@android:color/white"tools:context="com.atguigu.mobileplayer.SplashActivity">

<ImageView
    android:id="@+id/iv_icon"
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:layout_centerInParent="true"
    android:src="@drawable/login_icon" />
<LinearLayout
    android:gravity="center"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/iv_icon"
    android:layout_centerHorizontal="true"
    android:orientation="horizontal">

    <ProgressBar
        android:layout_gravity="center_vertical"
        android:layout_width="30dp"
        android:layout_height="30dp" />

    <TextView
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="5dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="正在启动中..."
        android:textColor="#000000"
        android:textSize="20sp" /></LinearLayout>

</RelativeLayout>

package com.atguigu.jiagou1;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.MotionEvent;

public class SplashActivity extends Activity {

private Handler handler = new Handler();private static final String TAG = SplashActivity.class.getSimpleName();//SplashActivity@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);

    handler.postDelayed(new Runnable() {        @Override
        public void run() {            //执行在主线程,并且在2秒后执行
            startMainActivity();
        }
    }, 2000);
}private boolean isStartMain = false;private void startMainActivity() {    if(!isStartMain){
        isStartMain = true;
        Intent intent = new Intent(this,MainActivity.class);
        startActivity(intent);        //关闭当前页面
        finish();
    }

}
@Overridepublic boolean onTouchEvent(MotionEvent event) {
    Log.e(TAG, "action==" + event.getAction());
    startMainActivity();    return super.onTouchEvent(event);
}@Overrideprotected void onDestroy() {    super.onDestroy();

// handler.removeCallbacksAndMessages(null);

}

}

主页面mainActivity

<?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"><!--标题拦截--><include layout="@layout/titlebar"/><!--用Framelayou替换各个Fragment--><FrameLayout
    android:id="@+id/fl_content"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1" /><!--底部的RadioGroup-切换不同的页面-不同Fragment--><RadioGroup
    android:id="@+id/rg_main"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#11000000"
    android:gravity="center_vertical"
    android:orientation="horizontal"
    android:padding="5dp">

    <RadioButton
        android:id="@+id/rb_local_video"
        style="@style/bottom_tab_style"
        android:drawableTop="@drawable/rb_local_video_drawable_selector"
        android:text="本地视频" />
    <RadioButton
        android:id="@+id/rb_local_audio"
        style="@style/bottom_tab_style"
        android:drawableTop="@drawable/rb_local_audio_drawable_selector"
        android:text="本地音乐" />
    <RadioButton
        android:id="@+id/rb_net_video"
        style="@style/bottom_tab_style"
        android:drawableTop="@drawable/rb_net_video_drawable_selector"
        android:text="网络视频" />
    <RadioButton
        android:id="@+id/rb_net_audio"
        style="@style/bottom_tab_style"
        android:drawableTop="@drawable/rb_net_audio_drawable_selector"
        android:text="网络音频" /></RadioGroup>

</LinearLayout>
package com.atguigu.jiagou1;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.widget.RadioGroup;

import com.atguigu.jiagou1.base.BaseFragment;
import com.atguigu.jiagou1.fragment.AudioFragment;
import com.atguigu.jiagou1.fragment.NetAudioFragment;
import com.atguigu.jiagou1.fragment.NetVideoFragment;
import com.atguigu.jiagou1.fragment.VideoFragment;

import java.util.ArrayList;

public class MainActivity extends FragmentActivity {

private RadioGroup rg_main;/**
 * 各个页面的Fragment
 */private ArrayList<BaseFragment> baseFragments;/**
 * 不同Fragment在列表中的位置
 */private int position = 0;private Fragment mContent;
@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);
    initView();
    initFragment();
    setListener();
}/**
 * 设置监听RadioGroup
 */private void setListener() {
    rg_main.setOnCheckedChangeListener(new MyOnCheckedChangeListener());
    rg_main.check(R.id.rb_local_video);
}class MyOnCheckedChangeListener implements RadioGroup.OnCheckedChangeListener {    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {        switch (checkedId) {            case R.id.rb_local_video://0
                position = 0;                break;            case R.id.rb_local_audio://1
                position = 1;                break;            case R.id.rb_net_video://2
                position = 2;                break;            case R.id.rb_net_audio://3
                position = 3;                break;
        }        //根据位置从集合中取对应的Fragment
        Fragment toFragment = getFragment(position);        //把对应的Fragment绑定到Activity中
        switchFragment(mContent, toFragment);

    }
}/**
 * @param fromFragment 刚才显示过的Fragment -隐藏
 * @param toFragment   马上要显示的Fragment-显示,添加
 */private void switchFragment(Fragment fromFragment, Fragment toFragment) {    if (mContent != toFragment) {
        mContent = toFragment;        if (toFragment != null) {

            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();            //判断toFragment是否添加
            if (!toFragment.isAdded()) {                //如果没有添加
                //隐藏fromFragment
                if (fromFragment != null) {
                    ft.hide(fromFragment);
                }                //添加toFragment
                ft.add(R.id.fl_content, toFragment).commit();                //提交

// ft.commit();

            } else {                //如果添加了
                //隐藏fromFragment
                if (fromFragment != null) {
                    ft.hide(fromFragment);
                }                //显示toFragment
                ft.show(toFragment).commit();                //提交
            }

        }
    }

}
/**
 * 根据位置取对应的Frament
 *
 * @param position
 * @return
 */private BaseFragment getFragment(int position) {    if (baseFragments != null && baseFragments.size() > 0) {        return baseFragments.get(position);//返回四个Fragment中的某一个
    }    return null;
}
/**
 * 初始化Fragment
 */private void initFragment() {
    baseFragments = new ArrayList<>();
    baseFragments.add(new VideoFragment());//本地视频Fragment
    baseFragments.add(new AudioFragment());//本地音频Fragment
    baseFragments.add(new NetVideoFragment());//网络视频Fragment
    baseFragments.add(new NetAudioFragment());//网络音频Fragment}private void initView() {
    setContentView(R.layout.activity_main);
    rg_main = (RadioGroup) findViewById(R.id.rg_main);

}

}

提供BaseFragment VideoFragment,AudioFragment,NetVideoFragment,NetAudioFragment继承BaseFragment

package com.atguigu.jiagou1.base;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
*

  • 作用:基类,公共类

  • VideoFragment,AudioFragment,NetVideoFragment,NetAudioFragment继承BaseFragment
    */

public abstract class BaseFragment extends Fragment {

/**
 * 上下文
 */public Context mContext;/**
 * 当BaseFragment被创建的时候被系统调用
 * @param savedInstanceState
 */@Overridepublic void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);
    mContext = getActivity();
}
/**
 * 当创建视图的时候调用该方法
 * @param inflater
 * @param container
 * @param savedInstanceState
 * @return
 */@Nullable@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

// return super.onCreateView(inflater, container, savedInstanceState);

    return initView();
}
/**
 * 抽象方法,由孩子实现;达到自己特有效果
 * @return
 */public abstract View initView();
/**
 * 当系统创建Activity完成的时候回调这个方法
 * 绑定数据
 * @param savedInstanceState
 */@Overridepublic void onActivityCreated(@Nullable Bundle savedInstanceState) {    super.onActivityCreated(savedInstanceState);
    initData();
}
/**
 * 当孩子需要联网请求数据,绑定数据等重写该方法;此方法执行在initView之后
 */public void initData() {

}

}
package com.atguigu.jiagou1.fragment;

import android.graphics.Color;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import com.atguigu.jiagou1.base.BaseFragment;

/**

  • 作用:本地音频
    */

public class AudioFragment extends BaseFragment {

private TextView textView;@Overridepublic View initView() {
    Log.e("TAG","本地音频视图(页面)初始化了...");
    textView = new TextView(mContext);
    textView.setTextSize(30);
    textView.setTextColor(Color.RED);
    textView.setGravity(Gravity.CENTER);    return textView;
}@Overridepublic void initData() {    super.initData();
    Log.e("TAG","本地音频数据初始化了...");
    textView.setText("本地音频的内容");
}

}
package com.atguigu.jiagou1.fragment;

import android.graphics.Color;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import com.atguigu.jiagou1.base.BaseFragment;

/**

  • 作用:网络音频
    */

public class NetAudioFragment extends BaseFragment {

private TextView textView;@Overridepublic View initView() {
    Log.e("TAG","网络音频视图(页面)初始化了...");
    textView = new TextView(mContext);
    textView.setTextSize(30);
    textView.setTextColor(Color.RED);
    textView.setGravity(Gravity.CENTER);    return textView;
}@Overridepublic void initData() {    super.initData();
    Log.e("TAG","网络音频数据初始化了...");
    textView.setText("网络音频的内容");
}

}
package com.atguigu.jiagou1.fragment;

import android.graphics.Color;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import com.atguigu.jiagou1.base.BaseFragment;

/**

  • 作用:本地视频
    */

public class VideoFragment extends BaseFragment {

private TextView textView;@Overridepublic View initView() {
    Log.e("TAG","本地视频视图(页面)初始化了...");
    textView = new TextView(mContext);
    textView.setTextSize(30);
    textView.setTextColor(Color.RED);
    textView.setGravity(Gravity.CENTER);    return textView;
}@Overridepublic void initData() {    super.initData();
    Log.e("TAG","本地视频数据初始化了...");
    textView.setText("本地视频的内容");
}

}
package com.atguigu.jiagou1.fragment;

import android.graphics.Color;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import com.atguigu.jiagou1.base.BaseFragment;

/**

  • 作者:尚硅谷-杨光福 on 2016/9/6 10:52

  • 作用:网络视频
    */

public class NetVideoFragment extends BaseFragment {

private TextView textView;@Overridepublic View initView() {
    Log.e("TAG","网络视频视图(页面)初始化了...");
    textView = new TextView(mContext);
    textView.setTextSize(30);
    textView.setTextColor(Color.RED);
    textView.setGravity(Gravity.CENTER);    return textView;
}@Overridepublic void initData() {    super.initData();
    Log.e("TAG","网络视频数据初始化了...");
    textView.setText("网络视频的内容");
}

}
package com.atguigu.jiagou1.base;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.atguigu.jiagou1.R;

/**

  • 作用:自定义标题栏
    */

public class Titlebar extends LinearLayout implements View.OnClickListener {

private View tv_search;private View rl_game;private View iv_history;private Context mContext;@Overrideprotected void onFinishInflate() {    super.onFinishInflate();
    tv_search =  getChildAt(1);
    rl_game = getChildAt(2);
    iv_history = getChildAt(3);

    tv_search.setOnClickListener(this);
    rl_game.setOnClickListener(this);
    iv_history.setOnClickListener(this);
}/**
 * 在代码中创建的时候通常用这个方法
 * @param context
 */public Titlebar(Context context) {    this(context,null);
}/**
 * 在布局文件中使用该类的时候,系统通过这个构造方法实例化
 * @param context
 * @param attrs
 */public Titlebar(Context context, AttributeSet attrs) {    this(context, attrs,0);
}public Titlebar(Context context, AttributeSet attrs, int defStyleAttr) {    super(context, attrs, defStyleAttr);    this.mContext = context;
}@Overridepublic void onClick(View v) {    switch (v.getId()){        case R.id.tv_search:
            Toast.makeText(mContext, "搜索", Toast.LENGTH_SHORT).show();            break;        case R.id.rl_game:
            Toast.makeText(mContext, "游戏", Toast.LENGTH_SHORT).show();            break;        case R.id.iv_history:
            Toast.makeText(mContext, "历史记录", Toast.LENGTH_SHORT).show();            break;
    }
}

}
<?xml version="1.0" encoding="utf-8"?>
<com.atguigu.jiagou1.base.Titlebar xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"
android:layout_height="55dp"
android:background="#ff3097fd"
android:gravity="center_vertical"
android:orientation="horizontal"><ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:layout_marginLeft="8dp"
    android:src="@drawable/ic_topbanner_logo" /><TextView
    android:id="@+id/tv_search"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="8dp"
    android:layout_weight="1"
    android:background="@drawable/tv_search_bg_selector"
    android:clickable="true"
    android:drawableLeft="@drawable/tv_search_drawable_selector"
    android:drawablePadding="8dp"
    android:text="全网搜索"
    android:textColor="@drawable/tv_search_textcolor_selector"
    android:textSize="14sp" /><RelativeLayout
    android:id="@+id/rl_game"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="8dp">

    <TextView
        android:id="@+id/tv_game"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:drawableLeft="@drawable/ic_topbanner_game" />

    <ImageView
        android:layout_width="6dp"
        android:layout_height="6dp"
        android:layout_alignRight="@id/tv_game"
        android:background="@drawable/shap" /></RelativeLayout><ImageView
    android:id="@+id/iv_history"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:background="@drawable/ic_topbanner_record" />

</com.atguigu.jiagou1.base.Titlebar>
//图片和基本的选择器就不发了,你去尽情的扩展吧

原文链接:http://www.apkbus.com/blog-859161-62031.html

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP