Skip to content

Commit

Permalink
update demo
Browse files Browse the repository at this point in the history
  • Loading branch information
fashare2015 committed May 30, 2017
1 parent 030541d commit f2bfc6e
Show file tree
Hide file tree
Showing 15 changed files with 234 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions app/src/main/java/com/fashare/hellokotlin/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


}
}
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ buildscript {
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.2'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand All @@ -15,6 +16,7 @@ buildscript {
allprojects {
repositories {
jcenter()
maven{ url "https://jitpack.io" }
}
}

Expand Down
24 changes: 24 additions & 0 deletions mydemo/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
apply plugin: 'com.android.application'
apply plugin: 'android-apt'

android {
compileSdkVersion rootProject.ext.compileSdkVersion as Integer
Expand Down Expand Up @@ -26,6 +27,29 @@ dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile "com.android.support:appcompat-v7:$supportLibsVersion"
compile "com.android.support:recyclerview-v7:$supportLibsVersion"

// Butter Knife
compile 'com.jakewharton:butterknife:8.2.1'
apt 'com.jakewharton:butterknife-compiler:8.2.1'

// glide
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'jp.wasabeef:glide-transformations:2.0.1'

// Rxjava
compile 'io.reactivex:rxjava:1.1.0'
compile 'io.reactivex:rxandroid:1.1.0'

// Retrofit
compile 'com.squareup.retrofit2:retrofit:2.0.0-beta4'
compile 'com.squareup.retrofit2:adapter-rxjava:2.0.0-beta4'
compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta4'

// Gson
compile 'com.google.code.gson:gson:2.6.2'

compile 'com.github.fashare2015:NoViewHolder:1.0.1'
}

// kotlin
Expand Down
21 changes: 16 additions & 5 deletions mydemo/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.fashare.mydemo">

<application android:allowBackup="true"
android:label="@string/app_name"
android:supportsRtl="true"
>
<uses-permission android:name="android.permission.INTERNET"/>

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>

<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>

</manifest>
</manifest>
17 changes: 17 additions & 0 deletions mydemo/src/main/kotlin/com/fashare/mydemo/Api.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.fashare.mydemo

import com.fashare.mydemo.data.HomeInfo
import retrofit2.http.GET
import rx.Observable

/**
* Created by apple on 17-5-31.
*/
interface Api {
companion object{
val BASE_URL: String = "http://news-at.zhihu.com/"
}

@GET("api/4/news/latest")
fun getHomeInfo(): Observable<HomeInfo>
}
76 changes: 76 additions & 0 deletions mydemo/src/main/kotlin/com/fashare/mydemo/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.fashare.mydemo

import android.app.Activity
import android.os.Bundle
import android.support.v4.widget.SwipeRefreshLayout
import android.support.v7.app.AppCompatActivity
import android.widget.Toast
import butterknife.BindView
import butterknife.ButterKnife
import com.fashare.mydemo.data.HomeInfo
import com.fashare.no_view_holder.NoViewHolder
import com.fashare.no_view_holder.annotation.click.BindItemClick
import com.fashare.no_view_holder.widget.NoOnItemClickListener
import retrofit2.Retrofit
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import rx.android.schedulers.AndroidSchedulers
import rx.schedulers.Schedulers

class MainActivity : AppCompatActivity() {

// retrofit 网络请求
val retrofit = Retrofit.Builder().baseUrl(Api.BASE_URL)
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build()

// NoViewHolder! 所有 view 的容器
var mNoViewHolder: NoViewHolder? = null

// 下拉刷新
@BindView(R.id.srl_refresh)
var mSrlRefresh: SwipeRefreshLayout? = null

// 点击事件
@BindItemClick(id = R.id.rv_story)
internal var clickMeiZhi = NoOnItemClickListener<HomeInfo.Story> { view, data, pos -> toast("click: $pos, $data") }

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

ButterKnife.bind(this)

mNoViewHolder = NoViewHolder.Builder(this)
.initView(HomeInfo())
.build()

initView()
loadData()
}

private fun initView() {
mSrlRefresh?.setOnRefreshListener { loadData() }
}

private fun loadData() {
mSrlRefresh?.setRefreshing(true)

retrofit.create(Api::class.java)
.getHomeInfo()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({
mNoViewHolder?.notifyDataSetChanged(it) // mHomeInfo 发生变化, 通知 UI 及时刷新
}, {
toast("服务器跑路啦~")
}, {
mSrlRefresh?.setRefreshing(false)
})
}
}

fun Activity.toast(msg: String) {
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show()
}
30 changes: 30 additions & 0 deletions mydemo/src/main/kotlin/com/fashare/mydemo/data/HomeInfo.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.fashare.mydemo.data

import com.fashare.mydemo.R
import com.fashare.no_view_holder.annotation.BindImageView
import com.fashare.no_view_holder.annotation.BindImageViews
import com.fashare.no_view_holder.annotation.BindRecyclerView
import com.fashare.no_view_holder.annotation.BindTextView
import java.util.*

/**
* Created by apple on 17-5-31.
*/
class HomeInfo(){
@BindRecyclerView(id = R.id.rv_story, layout = R.layout.item_story)
val stories: List<Story> = Collections.emptyList()

inner class Story{
@BindTextView(id = R.id.tv_title)
val title: String = ""

@BindImageViews(
BindImageView(id = R.id.iv_image, placeHolder = R.drawable.ic_launcher)
)
val images: List<String> = Collections.emptyList()

override fun toString(): String {
return title
}
}
}
Binary file added mydemo/src/main/res/drawable/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions mydemo/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/srl_refresh"
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.fashare.mydemo.MainActivity">

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

</android.support.v4.widget.SwipeRefreshLayout>
23 changes: 23 additions & 0 deletions mydemo/src/main/res/layout/item_story.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="6dp">

<ImageView
android:id="@+id/iv_image"
android:layout_width="90dp"
android:layout_height="90dp"/>

<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:text="@string/app_name"
android:textSize="18sp"
android:textColor="@android:color/black"/>

</LinearLayout>
6 changes: 6 additions & 0 deletions mydemo/src/main/res/values-w820dp/dimens.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<resources>
<!-- Example customization of dimensions originally defined in res/values/dimens.xml
(such as screen margins) for screens with more than 820dp of available width. This
would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively). -->
<dimen name="activity_horizontal_margin">64dp</dimen>
</resources>
6 changes: 6 additions & 0 deletions mydemo/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
</resources>
5 changes: 5 additions & 0 deletions mydemo/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
</resources>
11 changes: 11 additions & 0 deletions mydemo/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>

</resources>

0 comments on commit f2bfc6e

Please sign in to comment.