使用Butterknife与BaseActivity / Child Activities和MaterialDrawer

时间:2017-08-03 17:22:52

标签: java android android-activity butterknife materialdrawer

我正面临使用BaseActivity实现的多活动(不使用片段)实现MaterialDrawer(库MikePenz)的问题。另外,使用ButterKnife来绑定视图。

在SO中找到相关问题并逐一尝试,但在我的情况下,由于大多数人使用带有碎片的标准导航抽屉或者他们与ButterKnife无关而无法正常工作。

代码:

MainActivity类

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.toolbar)
    Toolbar toolbar;

    private List<TestModel> destinations;
    private DestinationAdapter mAdapter;
    ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        setSupportActionBar(toolbar);
        navManagement();
    }
}

activity_main layout

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/maincontainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.tvs.ui.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        >

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay"
             />

    </android.support.design.widget.AppBarLayout>

</android.support.constraint.ConstraintLayout>

StoreActivity

public class StoresActivity extends MainActivity {

    @BindView(R.id.searchEditText)
    EditText mSearchEditText; // errorRequired view 'searchEditText' with ID 2131558557 for field 'mSearchEditText' was not found
    @BindView(R.id.storesRecyclerView)
    RecyclerView recyclerView; // here too

    private List<TestModel> stores;
    private StoreAdapter mAdapter;

    //endregion
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stores);
        ButterKnife.bind(this);

        loadRecyclerView();
        testData();

    }
    }

activity_store布局

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
        android:id="@+id/searchEditText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/storesRecyclerView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:scrollbars="vertical">

    </android.support.v7.widget.RecyclerView>

</android.support.constraint.ConstraintLayout>

我理解错误的原因是当布局膨胀时调用MainActivity中的ButterKnife.Bind但在继承的活动中视图不会膨胀。导航抽屉加载但在点击项目时它失败是显而易见的原因。我不能使用FrameLayout,因为我使用的MaterialDrawer库没有(或者我不知道)有扩展的视图。

我尝试了几种广泛搜索SO的方法,例如SO1 SO2 Git Git2 Link但未成功。

感谢任何帮助深表感谢。提前谢谢。

3 个答案:

答案 0 :(得分:1)

MaterialDrawer会将视图扩展到您的布局。默认情况下,它会将DrawerLayout添加到您的活动的根目录中,并将其他内容(这是您的主要布局)再次作为子项添加到其中。

这个决定是为了让你在应用程序中实现Drawer非常简单,而无需改变任何特殊的东西。

Butterknife会在您调用bind方法时绑定视图。 MaterialDrawer的所有视图在您调用.build()时都受到约束,但由于MaterialDrawer的视图在内部绑定,因此您不必担心这些。

答案 1 :(得分:1)

Thanks @mikepenz for the response.

Posting it since it might help somebody

The issue was fixed by creating an abstract method in BaseActivity

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutResourceId());

        ButterKnife.bind(this); 
        setSupportActionBar(toolbar);
        navManagement();

    }
    protected abstract int getLayoutResourceId();

Concrete Activity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.activity_stores); not required
    ButterKnife.bind(this); 

    loadRecyclerView();
    testData();

}

@Override
protected int getLayoutResourceId() {
    return R.layout.activity_stores;
}

Please note that I was not using ButterKnife.Bind in the Concrete Activity and used @Nullable fields. But currently Binding in both Base and Implementing Activities.

答案 2 :(得分:0)

要完成@ user2695433的回答,您可以进一步处理:处理所有的刀具初始化并关闭BaseActivity,这样您就不会忘记发布资源!如果你的团队中有一些新的开发人员并且不知道butterknife如何工作,那将非常有用。这是:

--------在BaseActivity -------

protected Unbinder binder;
protected abstract int getLayoutResourceId();
...

  @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutResourceId());
        binder = ButterKnife.bind(this);
    }

@Override
protected void onDestroy() {
    super.onDestroy();
    binder.unbind(); //release ressource
}

-----在ConcreteActivity ------------

@BindView(R.id.title)
    TextView title; // butterknife stuff

@Override
    protected int getLayoutResourceId() { //define which Id
        return R.layout.activity_main;
    }
 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
  }