我有一个MainActivity,它是一个选项卡式活动。 有两个选项卡,每个选项卡中都有一个片段。 在第一个片段中,我有一个recyclerview,其中包含新闻列表 另外,我应该有一个收藏夹列表。 现在,我只需要在第二个片段中复制新闻的整个列表(第一个片段)。
我创建了一个接口,该接口由第一个和第二个片段实现。 在MainActivity中,我将addOnPageChangelistener()添加到了viewpager中。我真的不知道如何在Interface方法上传递数据以填充与第一个片段完全相同的第二个片段
NewsFragment(第一个片段)
class NewsFragment : Fragment(),OnFragmentResume {
lateinit var recyclerView: RecyclerView
lateinit var progBar: ProgressBar
var adapterRec:AdapterCust?=null
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.news_fragment, container, false)
recyclerView = view.findViewById(R.id.recyclerView)
recyclerView.hasFixedSize()
val itemDecoration = DividerItemDecoration(context, DividerItemDecoration.VERTICAL)
recyclerView.addItemDecoration(itemDecoration)
progBar = view.findViewById(R.id.progBar)
adapterRec = AdapterCust { articolo ->
val intent = Intent(context, WebViewUrl::class.java)
val desc = articolo.url
intent.putExtra("url", desc)
startActivity(intent)
}
recyclerView.adapter = adapterRec
val task = (SomeNameTask())
task.execute()
return view
}
inner class SomeNameTask : AsyncTask<Void, Void, List<Articolo>>() {
override fun onPostExecute(articoli: List<Articolo>) {
adapterRec?.items = articoli
val itemSingleton = ItemsSingleton.istance
itemSingleton.news = articoli
adapterRec?.completeItemsList = articoli
progBar.visibility = View.GONE
}
override fun doInBackground(vararg params: Void?): List<Articolo>? {
try {
val link = "https://api.hnpwa.com/v0/news/1.json"
val url = URL(link)
val connection: HttpURLConnection = url.openConnection() as HttpURLConnection
connection.requestMethod = "GET"
val inp = BufferedReader(InputStreamReader(connection.inputStream))
val response = StringBuffer()
val gson = Gson()
var input: String? = null
while ({ input = inp.readLine(); input }() != null) {
response.append(input)
}
val articoli = gson.fromJson<List<Articolo>>(response.toString(),
object : TypeToken<List<Articolo>>() {}.type
)
inp.close()
return articoli
} catch (e: Exception) {
return null
}
}
override fun onPreExecute() {
super.onPreExecute()
progBar.visibility = View.VISIBLE
}
}
override fun OnFragmentResumed() {
// ..miss implementation in the interface method..
}
}
MainActivity
class MainActivity : AppCompatActivity() {
lateinit var sectionsPagerAdapter: SectionsPagerAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
sectionsPagerAdapter = SectionsPagerAdapter(this, supportFragmentManager)
viewPager.adapter = sectionsPagerAdapter
viewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener{
override fun onPageScrollStateChanged(p0: Int) {
}
override fun onPageScrolled(p0: Int, p1: Float, p2: Int) {
}
override fun onPageSelected(index: Int){
((viewPager.adapter as FragmentPagerAdapter).getItem(index) as? OnFragmentResume)?.OnFragmentResumed() //interface method
}
})
tabs.setupWithViewPager(viewPager)
setSupportActionBar(toolbar)
}
收藏夹片段
class FavouriteFragment : Fragment(), OnFragmentResume {
lateinit var recyclerView: RecyclerView
var adapterCust:AdapterCust?=null
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.favourite_fragment, container, false)
recyclerView = view.findViewById(R.id.recyclerViewFav)
recyclerView.hasFixedSize()
val itemDecoration = DividerItemDecoration(context, DividerItemDecoration.VERTICAL)
recyclerView.addItemDecoration(itemDecoration)
adapterCust = AdapterCust { articolo ->
val intent = Intent(context, WebViewUrl::class.java)
val desc = articolo.url
intent.putExtra("url", desc)
startActivity(intent)
}
adapterCust!!.items = ItemsSingleton.istance.news
recyclerView.adapter = adapterCust
return view
}
override fun OnFragmentResumed() {
// miss implementation..
}
}