-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
030541d
commit f2bfc6e
Showing
15 changed files
with
234 additions
and
6 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
30
mydemo/src/main/kotlin/com/fashare/mydemo/data/HomeInfo.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |