|
1 | 1 | package com.example.jingbin.designpattern; |
2 | 2 |
|
3 | 3 | import android.content.Intent; |
| 4 | +import android.databinding.DataBindingUtil; |
4 | 5 | import android.os.Bundle; |
5 | 6 | import android.support.v7.app.AppCompatActivity; |
| 7 | +import android.support.v7.widget.GridLayoutManager; |
6 | 8 | import android.view.View; |
7 | | -import android.widget.Button; |
8 | 9 |
|
9 | 10 | import com.example.jingbin.designpattern.adapter.AdapterActivity; |
10 | 11 | import com.example.jingbin.designpattern.builder.BuilderActivity; |
11 | 12 | import com.example.jingbin.designpattern.command.CommandActivity; |
| 13 | +import com.example.jingbin.designpattern.databinding.ActivityMainBinding; |
12 | 14 | import com.example.jingbin.designpattern.decorator.DecoratorActivity; |
13 | 15 | import com.example.jingbin.designpattern.facade.FacadeActivity; |
14 | 16 | import com.example.jingbin.designpattern.factory.FactoryActivity; |
15 | 17 | import com.example.jingbin.designpattern.flyweight.FlyweightActivity; |
16 | 18 | import com.example.jingbin.designpattern.observer.ObserverActivity; |
17 | 19 | import com.example.jingbin.designpattern.prototype.PrototypeActivity; |
| 20 | +import com.example.jingbin.designpattern.proxy.ProxyActivity; |
18 | 21 | import com.example.jingbin.designpattern.singleton.SingletonActivity; |
19 | 22 | import com.example.jingbin.designpattern.state.StateActivity; |
20 | 23 | import com.example.jingbin.designpattern.strategy.StrategyActivity; |
21 | 24 | import com.example.jingbin.designpattern.templatemethod.TemplateMethodActivity; |
22 | 25 |
|
23 | | -import butterknife.BindView; |
24 | | -import butterknife.ButterKnife; |
| 26 | +import java.util.Arrays; |
| 27 | + |
| 28 | +import me.jingbin.library.ByRecyclerView; |
| 29 | +import me.jingbin.library.adapter.BaseByViewHolder; |
| 30 | +import me.jingbin.library.adapter.BaseRecyclerAdapter; |
25 | 31 |
|
26 | 32 | /** |
27 | 33 | * Created by jingbin on 2016/10/21. |
28 | 34 | */ |
29 | | -public class MainActivity extends AppCompatActivity implements View.OnClickListener { |
| 35 | +public class MainActivity extends AppCompatActivity { |
| 36 | + |
| 37 | + private ActivityMainBinding binding; |
| 38 | + private String[] patterns = { |
| 39 | + "观察者模式", "工厂模式", "单例设计模式", |
| 40 | + "策略模式", "适配器模式", "命令模式", |
| 41 | + "装饰者模式", "外观模式", "模板方法模式", |
| 42 | + "状态模式", "建造者模式", "原型模式", |
| 43 | + "享元模式", "代理模式" |
| 44 | + }; |
| 45 | + private Class[] classes = { |
| 46 | + ObserverActivity.class, FactoryActivity.class, SingletonActivity.class, |
| 47 | + StrategyActivity.class, AdapterActivity.class, CommandActivity.class, |
| 48 | + DecoratorActivity.class, FacadeActivity.class, TemplateMethodActivity.class, |
| 49 | + StateActivity.class, BuilderActivity.class, PrototypeActivity.class, |
| 50 | + FlyweightActivity.class, ProxyActivity.class |
| 51 | + }; |
30 | 52 |
|
31 | | - @BindView(R.id.bt_observer) |
32 | | - Button btObserver; |
33 | | - @BindView(R.id.bt_factory) |
34 | | - Button btFactory; |
35 | | - @BindView(R.id.bt_singleton) |
36 | | - Button btSingleton; |
37 | | - @BindView(R.id.bt_strategy) |
38 | | - Button btStrategy; |
39 | | - @BindView(R.id.bt_adapter) |
40 | | - Button btAdapter; |
41 | | - @BindView(R.id.bt_command) |
42 | | - Button btCommand; |
43 | | - @BindView(R.id.bt_decorator) |
44 | | - Button btDecorator; |
45 | | - @BindView(R.id.bt_facade) |
46 | | - Button btFacade; |
47 | | - @BindView(R.id.bt_template_method) |
48 | | - Button btTemplateMethod; |
49 | | - @BindView(R.id.bt_state) |
50 | | - Button btState; |
51 | | - @BindView(R.id.bt_builder) |
52 | | - Button btBuilder; |
53 | | - @BindView(R.id.bt_prototype) |
54 | | - Button btPrototype; |
55 | | - @BindView(R.id.bt_flyweight) |
56 | | - Button btFlyweight; |
57 | 53 |
|
58 | 54 | @Override |
59 | 55 | protected void onCreate(Bundle savedInstanceState) { |
60 | 56 | super.onCreate(savedInstanceState); |
61 | | - setContentView(R.layout.activity_main); |
62 | | - ButterKnife.bind(this); |
63 | | - initListener(); |
| 57 | + binding = DataBindingUtil.setContentView(this, R.layout.activity_main); |
| 58 | + initView(); |
64 | 59 | } |
65 | 60 |
|
66 | | - private void initListener() { |
67 | | - btObserver.setOnClickListener(this); |
68 | | - btFactory.setOnClickListener(this); |
69 | | - btSingleton.setOnClickListener(this); |
70 | | - btStrategy.setOnClickListener(this); |
71 | | - btAdapter.setOnClickListener(this); |
72 | | - btCommand.setOnClickListener(this); |
73 | | - btDecorator.setOnClickListener(this); |
74 | | - btFacade.setOnClickListener(this); |
75 | | - btTemplateMethod.setOnClickListener(this); |
76 | | - btState.setOnClickListener(this); |
77 | | - btBuilder.setOnClickListener(this); |
78 | | - btPrototype.setOnClickListener(this); |
79 | | - btFlyweight.setOnClickListener(this); |
80 | | - } |
81 | | - |
82 | | - @Override |
83 | | - public void onClick(View v) { |
84 | | - switch (v.getId()) { |
85 | | - case R.id.bt_observer:// 观察者模式 |
86 | | - startActivity(new Intent(this, ObserverActivity.class)); |
87 | | - break; |
88 | | - case R.id.bt_factory:// 工厂模式 |
89 | | - startActivity(new Intent(this, FactoryActivity.class)); |
90 | | - break; |
91 | | - case R.id.bt_singleton:// 单例设计模式 |
92 | | - startActivity(new Intent(this, SingletonActivity.class)); |
93 | | - break; |
94 | | - case R.id.bt_strategy:// 策略模式 |
95 | | - startActivity(new Intent(this, StrategyActivity.class)); |
96 | | - break; |
97 | | - case R.id.bt_adapter:// 适配器模式 |
98 | | - startActivity(new Intent(this, AdapterActivity.class)); |
99 | | - break; |
100 | | - case R.id.bt_command:// 命令模式 |
101 | | - startActivity(new Intent(this, CommandActivity.class)); |
102 | | - break; |
103 | | - case R.id.bt_decorator:// 装饰者模式 |
104 | | - startActivity(new Intent(this, DecoratorActivity.class)); |
105 | | - break; |
106 | | - case R.id.bt_facade:// 外观模式 |
107 | | - startActivity(new Intent(this, FacadeActivity.class)); |
108 | | - break; |
109 | | - case R.id.bt_template_method:// 模板方法模式 |
110 | | - startActivity(new Intent(this, TemplateMethodActivity.class)); |
111 | | - break; |
112 | | - case R.id.bt_state:// 状态模式 |
113 | | - startActivity(new Intent(this, StateActivity.class)); |
114 | | - break; |
115 | | - case R.id.bt_builder:// 建造者模式 |
116 | | - startActivity(new Intent(this, BuilderActivity.class)); |
117 | | - break; |
118 | | - case R.id.bt_prototype:// 原型模式 |
119 | | - startActivity(new Intent(this, PrototypeActivity.class)); |
120 | | - break; |
121 | | - case R.id.bt_flyweight:// 享元模式 |
122 | | - startActivity(new Intent(this, FlyweightActivity.class)); |
123 | | - break; |
124 | | - default: |
125 | | - break; |
126 | | - } |
| 61 | + private void initView() { |
| 62 | + GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2); |
| 63 | + binding.recyclerView.setLayoutManager(gridLayoutManager); |
| 64 | + binding.recyclerView.setAdapter(new BaseRecyclerAdapter<String>(R.layout.item_main, Arrays.asList(patterns)) { |
| 65 | + @Override |
| 66 | + protected void bindView(BaseByViewHolder holder, String title, int position) { |
| 67 | + holder.setText(R.id.bt_button, title); |
| 68 | + } |
| 69 | + }); |
| 70 | + binding.recyclerView.setOnItemClickListener(new ByRecyclerView.OnItemClickListener() { |
| 71 | + @Override |
| 72 | + public void onClick(View v, int position) { |
| 73 | + startActivity(new Intent(v.getContext(), classes[position])); |
| 74 | + } |
| 75 | + }); |
127 | 76 | } |
128 | 77 | } |
0 commit comments