Skip to content
Permalink
Browse files

Extension Function

  • Loading branch information...
imuhao committed Nov 4, 2017
1 parent e86e386 commit e2dfe36311e189980f81d07225a6b1a711354725
@@ -26,6 +26,7 @@ android {

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support:recyclerview-v7:26.1.0'
}
@@ -9,8 +9,10 @@ class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?): Unit {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
changeFragment(NewsFragment())
}


fun changeFragment(f: Fragment) {
val ft = supportFragmentManager.beginTransaction()
ft.replace(R.id.fl_container, f)
@@ -0,0 +1,30 @@
package com.caimuhao.kedditbysteps

import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.caimuhao.kedditbysteps.commons.inflate

/**
* @author Smile
* @time 2017/11/4 13:26
* @desc ${TODD}
*/
class NewsFragment : Fragment() {

private var newsList: RecyclerView? = null

override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
var view = container?.inflate(R.layout.fragment_news)
newsList = view?.findViewById(R.id.news_list)
newsList?.setHasFixedSize(true)
newsList?.layoutManager = LinearLayoutManager(context)

return view
}

}
@@ -0,0 +1,15 @@
package com.caimuhao.kedditbysteps.commons

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup

/**
* @author Smile
* @time 2017/11/4 13:49
* @desc ${TODD}
*/

fun ViewGroup.inflate(layoutId: Int, attachToRoot: Boolean = false): View {
return LayoutInflater.from(context).inflate(layoutId, this, attachToRoot)
}
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

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

0 comments on commit e2dfe36

Please sign in to comment.
You can’t perform that action at this time.