Android软件框架的搭建笔记

软件框架的搭建,Android通用的,是学习尚硅谷之后的笔记整理。

Android软件框架的搭建笔记_第1张图片

下面四个按钮直接的切换,不同的fragment之间的跳转。

关键代码

布局页面activity_mian.xml



    
    
    
    
    
    

        

        

        

        

    

titllebar.xml




    

 

BaseFragment.java

package com.example.base;

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

/**
 * CommonFragment的所有的基类,四个页面的整体布局的管理页面,都要继承该类
 */
public abstract class BaseFragment extends Fragment {
    protected Context mContext;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return initView();
    }

    /**
     *
     * @return
     * 强制子类重写,实现子类特有的ui
     */
    protected abstract View initView();

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData();
    }

    /**
     * 当需要初始化或者联网绑定数据,展示数据等等
     */
    protected  void initData(){

    }
}

四个页面的fragment

CommonFrameFragment.java

package com.example.fragment;

import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.adapter.CommmonFragmentAdapter;
import com.example.base.BaseFragment;
import com.example.soft.R;


public class CommonFrameFragment extends BaseFragment {

    public static final String TAG = CommonFrameFragment.class.getSimpleName();
    private TextView textView;
    private ListView mListView;
    private String[] datas;
    private CommmonFragmentAdapter adapter;

    @Override
    protected View initView() {
        Log.e(TAG, "常用框架页面被初始化了。。。。。");
        View view = View.inflate(mContext, R.layout.fragment_common_frame, null);
        mListView = view.findViewById(R.id.listview);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView adapterView, View view, int i, long l) {
                String data = datas[i];
                Toast.makeText(mContext,"data=="+i,Toast.LENGTH_SHORT).show();
            }
        });
        return view;
    }

    @Override
    protected void initData() {
        super.initData();
        Log.e(TAG, "常用框架数据被初始化了。。。。。");
        //准备数据
        datas = new String[]{"apple", "grape", "peach", "apricot", "pear"};
        //设置适配器
        adapter = new CommmonFragmentAdapter(mContext, datas);
        mListView.setAdapter(adapter);
    }
}

ThirdPartyFragment.java

package com.example.fragment;

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

import com.example.base.BaseFragment;


public class ThirdPartyFragment extends BaseFragment {

    public static final String TAG = ThirdPartyFragment.class.getSimpleName();
    private TextView textView;

    @Override
    protected View initView() {
        Log.e(TAG,"常用第三方页面被初始化了。。。。。");
        textView = new TextView(mContext);
        textView.setTextSize(20);
        textView.setGravity(Gravity.CENTER);
        textView.setTextColor(Color.RED);
        return textView;
    }

    @Override
    protected void initData() {
        super.initData();
        Log.e(TAG,"常用第三方数据被初始化了。。。。。");
        textView.setText("第三方CommonFrameFragment页面");
    }
}

CustomerFragment.java

package com.example.fragment;

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

import com.example.base.BaseFragment;


public class CustomerFragment extends BaseFragment {

    public static final String TAG = CustomerFragment.class.getSimpleName();
    private TextView textView;

    @Override
    protected View initView() {
        Log.e(TAG,"自定义页面被初始化了。。。。。");
        textView = new TextView(mContext);
        textView.setTextSize(20);
        textView.setGravity(Gravity.CENTER);
        textView.setTextColor(Color.RED);
        return textView;
    }

    @Override
    protected void initData() {
        super.initData();
        Log.e(TAG,"常自定义数据被初始化了。。。。。");
        textView.setText("自定义页面");
    }
}

OtherFragment.java

package com.example.fragment;

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

import com.example.base.BaseFragment;


public class OtherFragment extends BaseFragment {

    public static final String TAG = OtherFragment.class.getSimpleName();
    private TextView textView;

    @Override
    protected View initView() {
        Log.e(TAG,"其他页面被初始化了。。。。。");
        textView = new TextView(mContext);
        textView.setTextSize(20);
        textView.setGravity(Gravity.CENTER);
        textView.setTextColor(Color.RED);
        return textView;
    }

    @Override
    protected void initData() {
        super.initData();
        Log.e(TAG,"其他数据被初始化了。。。。。");
        textView.setText("其他页面");
    }
}

只提供第一个的页面fragment数据

CommmonFragmentAdapter.java

package com.example.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Point;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;


/**
 * 常用框架的fragment
 */
public class CommmonFragmentAdapter extends BaseAdapter{
    private final Context mContext;
    private final String[] mDatas;

    public CommmonFragmentAdapter(Context context, String[] datas){
        this.mContext = context;
        this.mDatas = datas;
    }
    @Override
    public int getCount() {
        return mDatas.length;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }


    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        TextView textView = new TextView(mContext);
        textView.setTextColor(Color.RED);
        textView.setPadding(10,10,10,10);
        textView.setTextSize(20);
        textView.setText(mDatas[i]);
        return textView;
    }
}

主类 MainActivity.java

package com.example.soft;

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

import com.example.base.BaseFragment;
import com.example.fragment.CommonFrameFragment;
import com.example.fragment.CustomerFragment;
import com.example.fragment.OtherFragment;
import com.example.fragment.ThirdPartyFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends FragmentActivity {

    private static final String TAG = "MainActivity";
    private RadioGroup mRg_main;
    private List mBaseFragments;
    private int position;

    //上一次的
    private Fragment mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate: ");
        initView();
        initFragment();
        setListener();
    }

    private void setListener() {
        mRg_main.setOnCheckedChangeListener(new MyOnCheckChangeListener());
        //默认选中
        mRg_main.check(R.id.rb_common_frame);
    }

    class MyOnCheckChangeListener implements RadioGroup.OnCheckedChangeListener {

        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int i) {
            switch (i) {
                case R.id.rb_common_frame:
                    position = 0;

                    break;
                case R.id.rb_thirdparty:
                    position = 1;

                    break;
                case R.id.rb_custom:
                    position = 2;

                    break;
                case R.id.rb_other:
                    position = 3;

                    break;
            }
            //根据位置得到对应的fragment
            BaseFragment to = getFragment();
            //替换
            switchFragment(mContext,to);
        }

        /**
         * @param from 刚显示的fragment,马上就要被隐藏
         * @param to   切换到,马上就要显示
         */
        private void switchFragment(Fragment from, Fragment to) {
            if (from != to) {
                mContext = to;
                FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                //才切换
                //判断有没有被添加
                if (!to.isAdded()) {
                    //to没有被添加
                    //from隐藏
                    if (from != null) {
                        ft.hide(from);
                    }
                    //添加to
                    if (to != null) {
                        ft.add(R.id.fl_content,to).commit();
                    }
                } else {
                    //to 已添加
                    //from隐藏
                    if (from != null) {
                        ft.hide(from);
                    }
                    //显示to
                    if (to != null) {
                        ft.show(to).commit();
                    }
                }
            }

        }
//        private void switchFragment(BaseFragment fragment) {
//            //得到fragmentManager
//            FragmentManager fm = getSupportFragmentManager();
//            //开启事务
//            FragmentTransaction transaction = fm.beginTransaction();
//            //替换
//            transaction.replace(R.id.fl_content,fragment);
//            //提交
//            transaction.commit();
//        }

        /**
         * 根据位置得到对应的fragment、
         *
         * @return
         */
        public BaseFragment getFragment() {
            BaseFragment fragment = mBaseFragments.get(position);
            return fragment;
        }
    }

    private void initFragment() {
        mBaseFragments = new ArrayList<>();
        mBaseFragments.add(new CommonFrameFragment());//常用
        mBaseFragments.add(new ThirdPartyFragment());//第三方
        mBaseFragments.add(new CustomerFragment());//自定义
        mBaseFragments.add(new OtherFragment());//其他
    }


    private void initView() {
        setContentView(R.layout.activity_main);
        mRg_main = findViewById(R.id.rg_bottom_tag);

    }
}

 

drawable的一下资源

rb_common_frame_drawable_selector.xml



    
    

rb_custom_drawable_selector.xml



    
    

rb_other_drawable_selector.xml



    
    

rb_style_textcolor_selector.xml



    
    

rb_thirdparty_drawable_selector.xml



    
    

rb_video_textcolor_selector.xml



    
    

图片

Android软件框架的搭建笔记_第2张图片

 

 

 

 

到此,一个简单的Android的框架就搭建完成了,自己的笔记,方便日后查看。

 

 

你可能感兴趣的:(android,移动开发)