ButterKnife

package com.example.dw.dw20181121moni.presenter;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import com.example.dw.dw20181121moni.R;
import com.example.dw.dw20181121moni.activity.MainActivity;
import com.example.dw.dw20181121moni.fragment.HomePagerFragment;
import com.example.dw.dw20181121moni.fragment.ListFragment;
import com.example.dw.dw20181121moni.fragment.MyFragment;
import com.example.dw.dw20181121moni.fragment.ShopCarFragment;
import com.example.dw.dw20181121moni.view.AppDelegate;
import com.hjm.bottomtabbar.BottomTabBar;

public class MainActivityPresenter extends AppDelegate implements View.OnClickListener {

    private Context context;
    private BottomTabBar mBottomTabBar;

    //返回视图
    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    //初始化方法
    @Override
    public void initData() {
        mBottomTabBar.init(((MainActivity)context).getSupportFragmentManager())
                .setImgSize(50,50)
                .setFontSize(12)
                .setTabPadding(4,6,10)
                .setChangeColor(Color.RED,Color.DKGRAY)
                .addTabItem("首页", R.drawable.homeno, HomePagerFragment.class)
                .addTabItem("列表", R.drawable.list_no, ListFragment.class)
                .addTabItem("购物车", R.drawable.car_no, ShopCarFragment.class)
                .addTabItem("我的", R.drawable.me_no, MyFragment.class);

    }

    //获取context
    @Override
    public void getContext(Context context) {
        this.context = context;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

        }
    }

    public void setData(BottomTabBar bottomTabBar) {
        this.mBottomTabBar = bottomTabBar;
    }
}
package com.example.dw.dw20181121moni.activity;

import com.example.dw.dw20181121moni.R;
import com.example.dw.dw20181121moni.presenter.BaseActivityPresenter;
import com.example.dw.dw20181121moni.presenter.MainActivityPresenter;
import com.hjm.bottomtabbar.BottomTabBar;

import butterknife.BindView;

public class MainActivity extends BaseActivityPresenter<MainActivityPresenter> {
    @BindView(R.id.main_bottomTabBar)
    public BottomTabBar bottomTabBar;
    @Override
    public Class getPresenter() {
        //返回对应的presenter
        return MainActivityPresenter.class;
    }

    @Override
    public void initView() {
        super.initView();
        presenter.setData(bottomTabBar);
    }
}
package com.example.dw.dw20181121moni.presenter;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

import com.example.dw.dw20181121moni.view.IDelegate;

import butterknife.ButterKnife;

public abstract class BaseActivityPresenter<T extends IDelegate> extends AppCompatActivity {

    protected T presenter;

    //每一个Activity都对应一个Presenter
    public abstract Class<T> getPresenter();
    public BaseActivityPresenter(){
        try {
            presenter = getPresenter().newInstance();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //创建布局
        presenter.create(getLayoutInflater(),null,savedInstanceState);
        //创建视图
        setContentView(presenter.getRootView());
        //获取context
        ButterKnife.bind(this);
        initView();
        presenter.getContext(this);
        //初始化方法
        presenter.initData();
    }

    public void initView() {

    }
}
package com.example.dw.dw20181121moni.view;

import android.os.Bundle;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public abstract class AppDelegate implements IDelegate {

    private View rootView;

    //获取布局
    @Override
    public void create(LayoutInflater inflater, ViewGroup viewGroup, Bundle bundle) {
        rootView = inflater.inflate(getLayoutId(), viewGroup, false);
    }

    protected abstract int getLayoutId();

    //获取视图
    @Override
    public View getRootView() {
        return rootView;
    }

    //获取控件
    SparseArray<View> views = new SparseArray<>();
    public <T extends View> T get(int id){
        View view = views.get(id);
        if (view == null){
            view = rootView.findViewById(id);
            views.put(id,view);
        }
        return (T) view;
    }

    //点击方法
    public void setClick(View.OnClickListener listener,int...ids){
        if (ids == null){
            return;
        }
        for(int id:ids){
            get(id).setOnClickListener(listener);
        }
    }
}
package com.example.dw.dw20181121moni.view;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public interface IDelegate {

    void initData();

    void create(LayoutInflater inflater, ViewGroup viewGroup, Bundle bundle);

    View getRootView();

    void getContext(Context context);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值