其他分享
首页 > 其他分享> > 39、Android--ButterKnife

39、Android--ButterKnife

作者:互联网

ButterKnife

ButterKnife从严格意义讲不算是依赖注入框架,它只是专注于Android系统的View注入框架,并不支持其他方面的注入。它可以减少大量的findViewById以及setOnClickListener代码,简化代码并提升开发效率。

基本使用

添加依赖

使用ButterKnife需要添加相应的依赖:

implementation "com.jakewharton:butterknife:10.2.3"
annotationProcessor  "com.jakewharton:butterknife-compiler:10.2.3"

在project/build.gradle中引入插件:

dependencies {
    classpath "com.android.tools.build:gradle:4.0.1"
    classpath 'com.jakewharton:butterknife-gradle-plugin:10.2.3'
}

除此之外,还需要在app/build.gradle引入相应的插件id:

plugins {
    id 'com.android.application'
    id 'com.jakewharton.butterknife'
}

绑定控件

用注解@BindView绑定控件id,代码如下所示:

public class MainActivity extends AppCompatActivity {
    @BindView(R.id.tv_text)
    TextView tvText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        tvText.setText("Bind View");
    }
}

用注解@BindViews绑定多个控件id,代码如下所示:

public class MainActivity extends AppCompatActivity {
    @BindViews({R.id.tv_text, R.id.tv_text1, R.id.tv_text2})
    List<TextView> textViews;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        for (int i = 0; i < textViews.size(); i++){
            textViews.get(i).setText("Bind View" + i);
        }
    }
}

绑定资源

可以用注解@BindString、@BindArray、@BindBool、@BindColor、@BindDimen、@BindDrawable和@BindBitmap。

@BindString(R.string.app_name)
String appName;
@BindArray(R.array.swordsman)
String[] swordsman;
@BindDimen(R.dimen.activity_horizontal_margin)
float margin;

绑定监听

用@OnClick来对“点击事件”进行监听,同理也可以用@OnLongClick来对“长按点击事件”进行监听,如下所示:

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
    }

    @OnClick(R.id.tv_text)
    public void onTextClick(View view){
        Toast.makeText(this, "Bind Click Event", Toast.LENGTH_SHORT).show();
    }

    @OnLongClick(R.id.tv_text)
    public void onTextLongClick(View view){
        Toast.makeText(this, "Bind Long Click Event", Toast.LENGTH_SHORT).show();
    }
}

除此之外,还可以通过@OnTextChanged、@OnTouch、@OnItemClick等事件进行监听。

可选绑定

@BindView或者其他的注解操作符,如果不能找到目标资源,则会引发异常。为了防止异常,可以添加@Nullable注解:

@Nullable
@BindView(R.id.tv_text)
TextView tvName;

其他应用

除了前面在Activity中使用ButterKnife之外,我们还可以在Fragment中使用它,如下所示:

public class BlankFragment extends Fragment {
    @BindView(R2.id.tv_text)
    TextView tvText;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View layout = inflater.inflate(R.layout.fragment_blank, container, false);
        ButterKnife.bind(this, layout);
        tvText.setText("Bind Text");
        return layout;
    }
}

在Adapter中我们也需要引用控件资源,也可以使用ButterKnife,如下所示:

public class RecycleAdapter extends RecyclerView.Adapter {
    private Context mContext;
    private List<String> mStrings;

    public RecycleAdapter(Context context, List<String> strings) {
        this.mContext = context;
        this.mStrings = strings;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(
                R.layout.item_recycleview, parent, false);
        MyViewHold hold = new MyViewHold(view);
        return hold;
    }
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ((MyViewHold)holder).mTvContent.setText(mStrings.get(position));
    }
    @Override
    public int getItemCount() {
        return mStrings.size();
    }
    class MyViewHold extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_item_content)
        TextView mTvContent;
        public MyViewHold(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}

其中Activity的代码如下所示:

public class MainActivity extends AppCompatActivity {
    @BindView(R.id.recycler_view)
    RecyclerView mRecyclerView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        List<String> strings = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            strings.add("String" + i);
        }
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        RecycleAdapter adapter = new RecycleAdapter(this, strings);
        mRecyclerView.setAdapter(adapter);
    }
}

模块开发

在Module中使用ButterKnife需要注意,在每个模块的build.gradle文件中都需要添加相应的注解:

annotationProcessor  "com.jakewharton:butterknife-compiler:10.2.3"

而在Module中进行绑定ID操作需要使用R2即可。

@BindView(R2.id.tv_text)
TextView tvText;

标签:39,layout,--,id,tv,ButterKnife,public,View
来源: https://www.cnblogs.com/pengjingya/p/15000392.html