ButterKnife
来源:互联网 发布:宁波二手房交易数据 编辑:程序博客网 时间:2024/05/21 21:39
ButterKnife的使用
11/12/2017 11:21:52 PM
前言
源码地址:ButterKnifeDemo
这是一种依赖注入框架,当然是为了我们更好的便捷开发,当然这只是一个控件级别的注入框架,需要对象类型的话请使用Dagger2,这是jackWharton Google大神写的软件,在使用的过程中我们还能学学人家是怎么样写代码的(Adapter里的ViewHolder绑定问题).代码将更加的清晰可读性更好.
一.使用其中的一些技巧
建立build,相应的版本请参见github:
dependencies { testCompile 'junit:junit:4.12' annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'}
1.1 配合插件使用
Android ButterKnife Zelezny,安装之后重启即可,使用方法,在layout文件上:Fn + alt + Insert –> Generate –> Generate ButterKnife Injections,可选择性的生成,可更改名称
1.2 每一个新建的moudle中都自动有ButterKnife
在AndroidStudio\plugins\android\lib\templates\gradle-projects\NewAndroidModule\recipe.xml.ftl中添加如下代码,以后新创建的工程默认会添加Butterknife依赖:
<dependency mavenUrl="com.jakewharton:butterknife:7.0.1" />
二.Activity,Fragment,Adapter三大块中的使用
2.1 Activity
这个使用比较简单了,直接贴代码即可:
XMl:
<?xml version="1.0" encoding="utf-8"?><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"tools:context="com.u9time.butterknife.MainActivity"> <LinearLayout android:id="@+id/ll" android:layout_width="match_parent" android:layout_height="50dp"> <Button android:id="@+id/btn_1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="btn_1" /> <Button android:id="@+id/btn_2" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="btn_2" /> <Button android:id="@+id/btn_3" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="btn_3" /> <Button android:id="@+id/btn_4" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="btn_4" /> </LinearLayout> <android.support.v4.view.ViewPager android:id="@+id/vp" android:layout_width="match_parent" android:layout_height="match_parent" /></LinearLayout>
Activity
package com.u9time.butterknife;import android.os.Bundle;import android.support.v4.app.Fragment;import android.support.v4.view.ViewPager;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.Button;import android.widget.LinearLayout;import java.util.ArrayList;import butterknife.BindArray;import butterknife.BindString;import butterknife.BindView;import butterknife.ButterKnife;import butterknife.OnClick;public class MainActivity extends AppCompatActivity { @BindView(R.id.btn_1) Button btn1; @BindView(R.id.btn_2) Button btn2; @BindView(R.id.btn_3) Button btn3; @BindView(R.id.btn_4) Button btn4; @BindView(R.id.ll) LinearLayout ll; @BindView(R.id.vp) ViewPager vp; /** * 有很多的额绑定,可以是字符串,数组,颜色,图片等等. */ @BindString(R.string.hello_blank_fragment) String hello_blank_fragment; @BindArray(R.array.city) String[] city; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); initView(); } private void initView() { ArrayList<Fragment> fragmentArrayList = new ArrayList<>(); for (int i = 0; i < 3; i++) { fragmentArrayList.add(new BlankFragment()); } vp.setAdapter(new VpAdapter(getSupportFragmentManager(), fragmentArrayList)); } @OnClick({R.id.btn_1, R.id.btn_2, R.id.btn_3, R.id.btn_4, R.id.ll}) public void onViewClicked(View view) { switch (view.getId()) { case R.id.btn_1: break; case R.id.btn_2: break; case R.id.btn_3: break; case R.id.btn_4: break; case R.id.ll: break; } }}
2.2 Fragment
其中需要注意是
ButterKnife.bind(this, view)
代码XMl:
<FrameLayout 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"tools:context="com.u9time.butterknife.BlankFragment"> <ListView android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="match_parent" /></FrameLayout>
Fragment:
package com.u9time.butterknife;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;import android.widget.ListView;import butterknife.BindView;import butterknife.ButterKnife;import butterknife.Unbinder;public class BlankFragment extends Fragment { @BindView(R.id.listview) ListView listview; Unbinder unbinder; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_blank, container, false); unbinder = ButterKnife.bind(this, view); return view; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); String[] data = new String[50]; for (int i = 0; i < 50; i++) { data[i] = String.valueOf(i); } listview.setAdapter(new LsAdapter(getContext(),data)); } @Override public void onDestroyView() { super.onDestroyView(); unbinder.unbind(); }}
2.3 Adapter
需要注意自己ViewHolder的写法
XMl:
<?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"> <TextView android:id="@+id/tv" android:layout_width="match_parent" android:layout_height="40dp" android:gravity="center" /></LinearLayout>
Adapter:
package com.u9time.butterknife;import android.content.Context;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.TextView;import butterknife.BindView;import butterknife.ButterKnife;/** * Created by Lenovo on 2017/11/12. */class LsAdapter extends BaseAdapter { private Context context; private String[] data; public LsAdapter(Context context, String[] data) { this.context = context; this.data = data; } @Override public int getCount() { return data.length; } @Override public Object getItem(int position) { return data[position]; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { convertView = View.inflate(context, R.layout.ls_item, null); holder = new ViewHolder(convertView); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.setData(data[position]); return convertView; } static class ViewHolder { @BindView(R.id.tv) TextView tv; private String data; ViewHolder(View view) { ButterKnife.bind(this, view); } public void setData(String data) { this.data = data; tv.setText(data); } }}
三.主次要点
这个仅仅只是一种注释框架并不属于真真的核心技术,当然不可否认确实很厉害,但是我们一般只是用作来注入控件和点击事件的,其他的数组及字符串等可只作为了解内容.
总结
只是工具,会用即可.
阅读全文
0 0
- ButterKnife
- ButterKnife
- ButterKnife
- ButterKnife
- ButterKnife
- ButterKnife
- ButterKnife
- butterknife
- ButterKnife
- ButterKnife
- Butterknife
- butterknife
- ButterKnife
- ButterKnife
- butterknife
- butterknife
- ButterKnife
- ButterKnife
- JavaScript中的文档碎片DocumentFragment
- 1. Two Sum
- 我的学习记录33
- shell脚本部分基础详解
- Throwable#initCause(Throwable) 方法
- ButterKnife
- 分析下面代码有什么问题?
- F
- 个人笔记——杂七杂八
- 关于VHDL的一些总结
- HDU2027 统计元音【入门】
- 【Tensorflow网络架构简单实现】 用Tensorflow实现VGG模型
- C++ Web 编程
- Androidstudio_Plugs