Я занимаюсь разработкой новостного приложения и создал нижнюю навигацию с фрагментами, а в верхнем колонтитуле я получаю новостные данные, используя модификацию, но ниже отображается пустой белый экран пустой белый экран
нижемой MainActivity_kt, где я реализовал bottomnavigationdrawer с фрагментами
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val bottomNavigationView = findViewById<BottomNavigationView>(R.id.bottom)
bottomNavigationView.setOnNavigationItemSelectedListener {
var selectedFragment = Fragment()
when (it.itemId) {
R.id.top_headline -> selectedFragment = TopHeadlinesFragment()
R.id.espn_news -> selectedFragment = ESPNFragment()
R.id.bbc_sport -> selectedFragment = BBCSportFragment()
R.id.football_italia -> selectedFragment = FootballItaliaFragment()
}
val transaction = supportFragmentManager.beginTransaction()
transaction.replace(R.id.frame_layout, selectedFragment)
transaction.commit()
return@setOnNavigationItemSelectedListener true
}
}
}
ниже action_main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/frame_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:layout_gravity="bottom"
app:menu="@menu/bottomnews_nav"
android:id="@+id/bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</FrameLayout>
ниже моего TopHeadlinesAdapter
@Suppress("NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS")
class TopHeadlinesAdapter(val context: Context) :
RecyclerView.Adapter<TopHeadlinesAdapter.MyViewHolder>() {
var articleList: List<Article> = listOf()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.news_list, parent, false)
return MyViewHolder(view)
}
override fun getItemCount(): Int {
return articleList.size
}
@SuppressLint("NewApi")
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
holder.articleTitle.text = articleList.get(position).title
holder.articleSourceName.text = articleList.get(position).source.name
Picasso.get().load(articleList.get(position).urlToImage).into(holder.image)
val input = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX")
val output = SimpleDateFormat("dd/MM/yyyy")
var d = Date()
try {
d = input.parse(articleList[5].publishedAt)
} catch (e: ParseException) {
try {
val fallback = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'")
fallback.timeZone = TimeZone.getTimeZone("UTC")
d = fallback.parse(articleList[5].publishedAt)
} catch (e2: ParseException) {
// TODO handle error
val formatted = output.format(d)
val timelinePoint = LocalDateTime.parse(formatted)
val now = LocalDateTime.now()
var elapsedTime = Duration.between(timelinePoint, now)
println(timelinePoint)
println(now)
elapsedTime.toMinutes()
holder.articleTime.text = "${elapsedTime.toMinutes()}"
}
}
}
fun setMovieListItems(articleList: List<Article>) {
this.articleList = articleList
notifyDataSetChanged()
}
@SuppressLint("NewApi")
fun example() {
}
class MyViewHolder(itemView: View?) : RecyclerView.ViewHolder(itemView!!) {
val image: ImageView = itemView!!.findViewById(R.id.imageView)
val articleTitle: TextView = itemView!!.findViewById(R.id.articleTitle)
val articleSourceName: TextView = itemView!!.findViewById(R.id.articleSourceName)
val imageCategory: ImageView = itemView!!.findViewById(R.id.imageCategory)
val articleTime: TextView = itemView!!.findViewById(R.id.articleTime)
}
}
ниже news_list.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp">
<ImageView
android:id="@+id/imageView"
android:layout_width="100dp"
android:layout_height="85dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:contentDescription="bbc"
tools:background="@color/colorPrimary" />
<TextView
android:id="@+id/articleTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_toEndOf="@id/imageView"
android:layout_toRightOf="@id/imageView"
android:ellipsize="end"
android:lines="3"
android:maxLines="3"
android:text="1\n2\n3\n" />
<ImageView
android:id="@+id/imageCategory"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_below="@id/articleTitle"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_toEndOf="@id/imageView"
android:layout_toRightOf="@id/imageView"
android:src="@drawable/ic_espn"
tools:background="@color/colorPrimary" />
<TextView
android:id="@+id/articleSourceName"
android:layout_width="wrap_content"
android:layout_height="32dp"
android:layout_below="@id/articleTitle"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_toEndOf="@id/imageCategory"
android:layout_toRightOf="@id/imageCategory"
android:gravity="center|start"
android:text="Onefootbal" />
<TextView
android:id="@+id/articleTime"
android:layout_width="wrap_content"
android:layout_height="32dp"
android:layout_below="@id/articleTitle"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_toEndOf="@id/articleSourceName"
android:layout_toRightOf="@id/articleSourceName"
android:gravity="center|start"
android:text="- 1h"
android:textColor="@android:color/darker_gray"
tools:ignore="NotSibling" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
ниже TopHeadlinesFragment, где я получаю данные новостей, используя модификацию
class TopHeadlinesFragment : Fragment() {
var topHeadlinesAdapter: TopHeadlinesAdapter? = null
//3
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(
R.layout.fragment_top_headlines
, container, false
)
val recyclerView = view.findViewById (R.id.recyclerView) as RecyclerView
recyclerView.layoutManager = LinearLayoutManager(context)
recyclerView.adapter = topHeadlinesAdapter
val apiInterface = SportNewsInterface.create().getNews()
apiInterface.enqueue(object : Callback<SportNewsResponse> {
override fun onResponse(
call: Call<SportNewsResponse>?,
response: Response<SportNewsResponse>?
) {
if (response?.body() != null) topHeadlinesAdapter?.setMovieListItems(response.body()!!.articles)
}
override fun onFailure(call: Call<SportNewsResponse>?, t: Throwable?) {
}
})
return view
}
}
ниже фрагмента_top_headlines.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
ниже моего logcat
2019-10-10 15:23:07.818 27558-27558/yodgorbek.komilov.musobaqayangiliklari E/RecyclerView: No adapter attached; skipping layout
2019-10-10 15:23:08.324 27558-27558/yodgorbek.komilov.musobaqayangiliklari E/RecyclerView: No adapter attached; skipping layout
2019-10-10 15:23:09.497 27558-27558/yodgorbek.komilov.musobaqayangiliklari E/RecyclerView: No adapter attached; skipping layout
2019-10-10 15:23:12.277 27558-27558/yodgorbek.komilov.musobaqayangiliklari E/RecyclerView: No adapter attached; skipping layout
2019-10-10 15:23:14.761 27558-27558/yodgorbek.komilov.musobaqayangiliklari E/RecyclerView: No adapter attached; skipping layout
2019-10-10 15:23:21.146 27558-27558/yodgorbek.komilov.musobaqayangiliklari E/RecyclerView: No adapter attached; skipping layout
2019-10-10 15:23:21.452 27558-27558/yodgorbek.komilov.musobaqayangiliklari E/RecyclerView: No adapter attached; skipping layout