MainActivity.kt--->
News.kt--->
NewsTitleFragment.kt --->
}
NewsContentFragment--->
NewsContentActivity--->
package com.example.fredric.simplenews
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
News.kt--->
package com.example.fredric.simplenews
data class News(
var title: String,
var content: String
)NewsTitleFragment.kt --->
package com.example.fredric.simplenews
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 android.widget.TextView
import java.util.*
class NewsTitleFragment : Fragment() {
var isTwoPane: Boolean = false
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
var view = inflater?.inflate(R.layout.news_title_frag ,container,false)
var newsTitleRecycleView = view?.findViewById<RecyclerView>(R.id.news_title_recycle_view)
var layoutManager = LinearLayoutManager(activity)
newsTitleRecycleView?.layoutManager = layoutManager
var newsAdapter = NewsAdapter(getNews())
newsTitleRecycleView?.adapter = newsAdapter
return view
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
isTwoPane = if(activity.findViewById<View>(R.id.news_content_layout) != null) true else false
}
fun getNews() : ArrayList<News> {
var newsList = ArrayList<News>()
for(i in 1..50) {
var title = "This is news title: ${i}"
var content = getRandomLengthContent("This is news content ${i}.")
var news = News(title,content)
newsList?.add(news)
}
return newsList
}
fun getRandomLengthContent(content:String) : String {
var rnd = Random()
var len = rnd.nextInt(20) + 1
var sb = StringBuilder()
for(i in 1..len) {
sb.append(content)
}
return sb.toString()
}
inner class NewsAdapter : RecyclerView.Adapter<NewsAdapter.ViewHolder> {
inner class ViewHolder : RecyclerView.ViewHolder {
var newsTitleText: TextView? = null
constructor(view: View?) : super(view) {
newsTitleText = view?.findViewById<TextView>(R.id.news_title)
}
}
var mNewsList: ArrayList<News>? = null
constructor(newsList: ArrayList<News>) {
this.mNewsList = newsList
}
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder {
var view = LayoutInflater.from(parent?.context).inflate(R.layout.news_item, parent, false)
val holder = ViewHolder(view)
//Init the first news
if(isTwoPane == true) {
assert(mNewsList!!.size >0)
var news = mNewsList?.get(0)
var newsContentFragment =fragmentManager.findFragmentById(R.id.news_content_fragment) as NewsContentFragment
newsContentFragment.refresh(news?.title!!,news?.content!!)
}
view.setOnClickListener({ view ->
var news = mNewsList?.get(holder.adapterPosition)
if (isTwoPane == true) {
var newsContentFragment =fragmentManager!!.findFragmentById(R.id.news_content_fragment) as NewsContentFragment
newsContentFragment.refresh(news?.title!!,news?.content!!)
}else {
NewsContentActivity.actionStart(activity,news?.title!!,news?.content!!)
}
})
return holder
}
override fun getItemCount(): Int {
return mNewsList!!.size
}
override fun onBindViewHolder(holder: ViewHolder?, position: Int) {
var news = mNewsList?.get(position)
holder?.newsTitleText?.setText(news?.title)
}
}
NewsContentFragment--->
package com.example.fredric.simplenews
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
class NewsContentFragment : Fragment() {
var contentView:View? = null
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View {
contentView = inflater?.inflate(R.layout.news_content_frag,container,false)
return contentView!!
}
fun refresh(newsTitle:String,newsContent:String) {
var visibilityLayout = contentView?.findViewById<View>(R.id.visibility_layout)
visibilityLayout?.visibility = View.VISIBLE
var newsTitleText = contentView?.findViewById<TextView>(R.id.news_title)
var newsContentText = contentView?.findViewById<TextView>(R.id.news_content)
newsTitleText?.setText(newsTitle)
newsContentText?.setText(newsContent)
}
}NewsContentActivity--->
package com.example.fredric.simplenews
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.support.design.widget.Snackbar
import android.support.v7.app.AppCompatActivity
import kotlinx.android.synthetic.main.news_content.*
class NewsContentActivity : AppCompatActivity() {
companion object {
fun actionStart(context: Context?, newsTitle:String,newsContent:String ) {
val intent = Intent(context,NewsContentActivity::class.java)
intent.putExtra("news_title",newsTitle)
intent.putExtra("news_content",newsContent)
context?.startActivity(intent)
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.news_content)
var newsTitle = intent.getStringExtra("news_title")
var newsContent = intent.getStringExtra("news_content")
var newsContentFragment = supportFragmentManager.findFragmentById(R.id.news_content_fragment) as NewsContentFragment
newsContentFragment.refresh(newsTitle,newsContent)
}
}
layout/activity_main.xml,
</LinearLayout>
layout-sw600dp/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
layout/activity_main.xml,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".MainActivity">
<fragment
android:id="@+id/news_title_fragment"
android:name="com.example.fredric.simplenews.NewsTitleFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
layout-sw600dp/activity_main.xml
<?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="match_parent">
<fragment
android:id="@+id/news_title_fragment"
android:name="com.example.fredric.simplenews.NewsTitleFragment"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"/>
<FrameLayout
android:id="@+id/news_content_layout"
android:layout_width="0dp"
android:layout_weight="3"
android:layout_height="match_parent"
>
<fragment
android:id="@+id/news_content_fragment"
android:name="com.example.fredric.simplenews.NewsContentFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
</LinearLayout>
news_content.xml,
news_content_frag.xml,
</RelativeLayout>
news_item_frag.xml,
news_item.xml,
news_content.xml,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".NewsContentActivity">
<fragment
android:id="@+id/news_content_fragment"
android:name="com.example.fredric.simplenews.NewsContentFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
news_content_frag.xml,
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/visibility_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="invisible">
<TextView
android:id="@+id/news_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="10dp"
android:textSize="20sp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#000"/>
<TextView
android:id="@+id/news_content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:padding="15dp"
android:textSize="18sp"
/>
</LinearLayout>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:background="#000"/>
news_item_frag.xml,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/news_title_recycle_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>news_item.xml,
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/news_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="end"
android:textSize="18sp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="15dp"
android:paddingBottom="15dp"
/>
平板模式:
手机模式:
评论
发表评论