GankClient(JAVA)技術淺析(一)--ButterKnife

最近根據Gank.io 參照着GankClient-Kotlin 寫一個小項目GankClient總結一下技術,這裏暫時使用java編寫,運用到了:

  • ButterKnife(黃油刀)xml視圖依賴注入,簡化findViewById()手工繁瑣代碼;
  • Retrofit2+OkHttp3+RxJava2構建當前流行的網絡框架;
  • Glide圖片加載庫,減少圖片OOM問題;
  • MVP架構 分離Activity的model層功能,只作爲view層,增加presenter層構建連接;
  • Dagger2對mvp架構使用依賴注入 ,進行進一步隔離model層,view層, presenter層;
  • 下面介紹一下各部分用法

ButterKnife

JakeWharton大神的https://github.com/JakeWharton/butterknife

在model的builde.gradle

dependencies {
  compile 'com.jakewharton:butterknife:8.8.1'
  annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
}

在Acitivty中使用

選中activity_detail,按alt+Insert,即可看到Butterknife插件
Butterknife插件使用

Butterknife插件使用

結果如下
DetailActivity

public class DetailActivity extends AppCompatActivity {


    @BindView(R.id.wv_detail)
    WebView wvDetail;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);
        ButterKnife.bind(this);
        ``` 
    }

activity_detail.xml

<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:layout_width="match_parent"
    android:layout_height="match_parent"
   tools:context="com.wega.gankclient.home.detail.DetailActivity">


    <WebView
        android:id="@+id/wv_detail"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="0dp"
        android:layout_marginEnd="0dp"
        android:layout_marginStart="0dp"
        android:layout_marginTop="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

在Fragment使用

插件步驟與在Acitivty一致,結果有些不同

BaseListFragment

public   class BaseListFragment<T>  extends Fragment  
    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.swipe_refresh_layout)
    SwipeRefreshLayout mSwipeRefreshLayout;
    Unbinder unbinder;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

            mLayout = inflater.inflate(setLayout(), container, false);
            unbinder = ButterKnife.bind(this, mLayout);

        return mLayout;
    }
@Override
    public void onDestroyView() {
        super.onDestroyView();
         unbinder.unbind();
    }

fragment_base_list.xml

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

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipe_refresh_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">


        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            />

    </android.support.v4.widget.SwipeRefreshLayout>
</FrameLayout>

github

GankClient源碼地址:

https://github.com/LinweiJ/GankClient

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章