可以在每个项目中自动包含 viewBinding 吗?

huangapple go评论111阅读模式
英文:

Is it possible to include viewBinding in every project automatically?

问题

这是一个相当长的代码更改过程:

public class MainActivity extends AppCompatActivity {
    ActivityMainBinding mainBinding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        mainBinding = ActivityMainBinding.inflate(getLayoutInflater());
        super.onCreate(savedInstanceState);
        setContentView(mainBinding.getRoot());
    }
}

每当我创建新项目或新的活动时都要执行这个更改。有没有可能自动化这个过程呢?

英文:

It's quite long to change the code:

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

to:

public class MainActivity extends AppCompatActivity {
    ActivityMainBinding mainBinding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        mainBinding = ActivityMainBinding.inflate(getLayoutInflater());
        super.onCreate(savedInstanceState);
        setContentView(mainBinding.getRoot());
    }
} 

every time I create a new project or an activity. Is it possible to automate this process?

答案1

得分: 2

以下是翻译好的部分:

无法自动化该流程,因为我们必须自己提供 layoutId。Activity 不会自动绑定到布局。你可以创建一个 BaseActivity,并让所有的 Activity 都继承它。以下是带有绑定的模板代码:

public abstract class BaseActivity<B extends ViewDataBinding> extends AppCompatActivity {
    protected abstract int getContentViewId();
    protected B binding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, getContentViewId());
    }
}

class MainActivity extends BaseActivity<ActivityMainBinding> {
    @Override
    public int getContentViewId() {
        return R.layout.activity_main;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 现在你可以直接在这里访问 binding
    }
}

这仅仅是用于绑定的部分。你还可以在 BaseActivity 中添加一些可重用的方法,在任何 Activity 中使用,无需重复编写它们。同时,super.onCreate(savedInstanceState) 应该是调用的第一行代码。

英文:

You can not automate the process since we have to provide the layoutId ourself. Activity is not gonna bind to a layout automatically. What you can do is Create a BaseActivity and inherit it from all your Activites. Below is a template with binding .

public abstract class BaseActivity&lt;B extends ViewDataBinding&gt; extends AppCompatActivity {
protected abstract int getContentViewId();
protected B binding;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    binding = DataBindingUtil.setContentView(this, getContentViewId())

}
}

class MainActivity extends BaseActivity&lt;ActivityMainBinding&gt; {
    @Override
    public int getContentViewId() {
        return R.layout.activity_main;
    }
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // now you can directly access binding here
    }
}

This is just for Binding you can Also add some reusable method in BaseActivity and use them in Any Activity without writing them again. and super.onCreate(savedInstanceState) should be first line for call .

答案2

得分: 1

不,我认为这是不可能的,但你可以创建**Live Template**以频繁获取它。

或者,简单来说,你可以将该文件夹复制为模板。每当你需要创建一个新项目时,只需复制/粘贴并更改项目名称即可。

谢谢。

英文:

No, I think it's not possible but you can make Live Template to get it frequently.

Or Simple that you can make a copy of that folder as a template. Whenever you need to create a new project it will be easy to copy/paste and just change the name of the project.

Thank you.

huangapple
  • 本文由 发表于 2020年9月15日 19:48:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/63901193.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定