From 4880ab335d5f04170b2ae8b1fa6c8b0c8080655b Mon Sep 17 00:00:00 2001 From: RAJ MANJREKAR Date: Mon, 10 Oct 2022 21:13:53 +0530 Subject: [PATCH] OnClick on Top Cards --- .idea/misc.xml | 2 + README.md | 10 -- app/build.gradle | 2 +- .../adapters/CustomAdapterForTopHeadlines.kt | 80 ---------------- .../fragmentClasses/GeneralFragment.kt | 92 ++++--------------- .../layout/list_item_for_top_headlines.xml | 20 +--- 6 files changed, 24 insertions(+), 182 deletions(-) delete mode 100644 app/src/main/java/com/example/newsapp/adapters/CustomAdapterForTopHeadlines.kt diff --git a/.idea/misc.xml b/.idea/misc.xml index ea7f5db..e79fde7 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -60,6 +60,8 @@ + + diff --git a/README.md b/README.md index 520d4aa..ebff5f8 100644 --- a/README.md +++ b/README.md @@ -41,8 +41,6 @@ Kotlin-based Android news app helps users stay up-to-date on the latest headline Note: To run the app you have to add NewsAPI.org key. This API has limited access to daily request for News. * Generate API key from News api * Paste the key in 'gradle.properties' *(Create the file if not exists)* - - For Linux/Mac: `/home/.gradle/gradle.properties` - - For Windows: `C:\Users\\.gradle\gradle.properties` API_KEY="" * Rebuild app @@ -64,14 +62,6 @@ Note: To run the app you have to add NewsAPI.org key. This API has limited acces - **If a maintainer reports your pull request as spam, it will not be counted towards your participation in Hacktoberfest.** -### Todos Features: - -* Add language support for various languages -* Diversify news feed by adding different country/language options (through API endpoints) -* Improve home page card-slider/Carousel -* And many more. - - ### 📝 License Copyright (c) 2022 Raj Manjrekar diff --git a/app/build.gradle b/app/build.gradle index 9ced633..b89d3ee 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -20,7 +20,7 @@ android { // Please ensure you have a valid API KEY for https://newsapi.org/ // to use this app -// A valid key will need to be entered +// A valid key will need to be entered in gradle.properties file buildConfigField("String", "API_KEY", "\"5a3e054de1834138a2fbc4a75ee69053\"") } diff --git a/app/src/main/java/com/example/newsapp/adapters/CustomAdapterForTopHeadlines.kt b/app/src/main/java/com/example/newsapp/adapters/CustomAdapterForTopHeadlines.kt deleted file mode 100644 index cafb06f..0000000 --- a/app/src/main/java/com/example/newsapp/adapters/CustomAdapterForTopHeadlines.kt +++ /dev/null @@ -1,80 +0,0 @@ -package com.example.newsapp.adapters - -import android.content.Context -import android.view.LayoutInflater -import android.view.View -import android.view.ViewGroup -import android.widget.ImageView -import android.widget.TextView -import androidx.recyclerview.widget.RecyclerView -import com.example.newsapp.MainActivity -import com.example.newsapp.NewsModel -import com.example.newsapp.R -import com.example.newsapp.utils.Constants.TOP_HEADLINES_COUNT -import com.squareup.picasso.Picasso - - -class CustomAdapterForTopHeadlines(private var newsList: List) : - RecyclerView.Adapter() { - - private lateinit var mListener: OnItemClickListener - private lateinit var context: Context - - interface OnItemClickListener { - fun onItemClick(position: Int) - } - - fun setOnItemClickListener(listener: OnItemClickListener) { - mListener = listener - } - - class ViewHolder(ItemView: View, listener: OnItemClickListener) : - RecyclerView.ViewHolder(ItemView) { - - val image: ImageView = itemView.findViewById(R.id.img) - val headLine: TextView = itemView.findViewById(R.id.headline) - - init { - ItemView.setOnClickListener { - listener.onItemClick(adapterPosition) - } - } - } - - override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { - - val view = LayoutInflater.from(parent.context) - .inflate(R.layout.list_item_for_top_headlines, parent, false) - context = parent.context - return ViewHolder(view, mListener) - } - - - override fun onBindViewHolder(holder: ViewHolder, position: Int) { - - val pos = position % TOP_HEADLINES_COUNT - val newsData: NewsModel = newsList[pos] - - holder.headLine.text = newsData.headLine - - val imgUrl = newsData.image - if (imgUrl.isNullOrEmpty()) { - Picasso.get() - .load(R.drawable.samplenews) - .fit() - .centerCrop() - .into(holder.image) - } else { - Picasso.get() - .load(imgUrl) - .fit() - .centerCrop() - .error(R.drawable.samplenews) - .into(holder.image) - } - } - - override fun getItemCount(): Int { - return Int.MAX_VALUE - } -} \ No newline at end of file diff --git a/app/src/main/java/com/example/newsapp/fragmentClasses/GeneralFragment.kt b/app/src/main/java/com/example/newsapp/fragmentClasses/GeneralFragment.kt index c2f532c..7c7269d 100644 --- a/app/src/main/java/com/example/newsapp/fragmentClasses/GeneralFragment.kt +++ b/app/src/main/java/com/example/newsapp/fragmentClasses/GeneralFragment.kt @@ -2,12 +2,11 @@ package com.example.newsapp.fragmentClasses import android.content.Intent import android.os.Bundle -import android.os.Handler -import android.os.Looper import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.ImageView +import android.widget.TextView import androidx.fragment.app.Fragment import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView @@ -16,8 +15,6 @@ import com.example.newsapp.NewsModel import com.example.newsapp.R import com.example.newsapp.ReadNewsActivity import com.example.newsapp.adapters.CustomAdapter -import com.example.newsapp.adapters.CustomAdapterForTopHeadlines -import com.example.newsapp.utils.Constants.DEFAULT_SWIPER_DELAY import com.example.newsapp.utils.Constants.NEWS_CONTENT import com.example.newsapp.utils.Constants.NEWS_DESCRIPTION import com.example.newsapp.utils.Constants.NEWS_IMAGE_URL @@ -27,7 +24,6 @@ import com.example.newsapp.utils.Constants.NEWS_TITLE import com.example.newsapp.utils.Constants.NEWS_URL import com.example.newsapp.utils.Constants.TOP_HEADLINES_COUNT import com.example.newsapp.utils.Constants.INITIAL_POSITION -import com.jama.carouselview.CarouselScrollListener import com.jama.carouselview.CarouselView import com.jama.carouselview.enums.IndicatorAnimationType import com.jama.carouselview.enums.OffsetType @@ -35,12 +31,8 @@ import com.squareup.picasso.Picasso class GeneralFragment : Fragment() { - private lateinit var mainHandler: Handler - private lateinit var swiper: Runnable private lateinit var recyclerView: RecyclerView -// private lateinit var recyclerViewTop: RecyclerView private lateinit var carouselView: CarouselView -// private lateinit var topAdapter: CustomAdapterForTopHeadlines private lateinit var adapter: CustomAdapter private lateinit var newsDataForTopHeadlines: List private lateinit var newsDataForDown: List @@ -67,54 +59,42 @@ class GeneralFragment : Fragment() { carouselView.apply { size = newsDataForTopHeadlines.size -// resource = R.layout.carousel_item autoPlay = true indicatorAnimationType = IndicatorAnimationType.THIN_WORM carouselOffset = OffsetType.CENTER setCarouselViewListener { view, position -> - // Example here is setting up a full image carousel val imageView = view.findViewById(R.id.img) -// imageView.setImageDrawable() Picasso.get() .load(newsDataForTopHeadlines[position].image) .fit() .centerCrop() .error(R.drawable.samplenews) .into(imageView) - } - // After you finish setting up, show the CarouselView - show() - } - // Top headlines items Slider - mainHandler = Handler(Looper.getMainLooper()) - swiper = object : Runnable { - override fun run() { -// recyclerViewTop.smoothScrollToPosition(position) - position++ - mainHandler.postDelayed(this, DEFAULT_SWIPER_DELAY) - } - } + val newsTitle = view.findViewById(R.id.headline) + newsTitle.text = newsDataForTopHeadlines[position].headLine - carouselView.carouselScrollListener = object : CarouselScrollListener { - override fun onScrollStateChanged( - recyclerView: RecyclerView, - newState: Int, - position: Int - ) { - if (newState == RecyclerView.SCROLL_STATE_DRAGGING) { - mainHandler.removeCallbacks(swiper) - } - } + view.setOnClickListener { - override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) { + val intent = Intent(context, ReadNewsActivity::class.java).apply { + putExtra(NEWS_URL, newsDataForTopHeadlines[position].url) + putExtra(NEWS_TITLE, newsDataForTopHeadlines[position].headLine) + putExtra(NEWS_IMAGE_URL, newsDataForTopHeadlines[position].image) + putExtra(NEWS_DESCRIPTION, newsDataForTopHeadlines[position].description) + putExtra(NEWS_SOURCE, newsDataForTopHeadlines[position].source) + putExtra(NEWS_PUBLICATION_TIME, newsDataForTopHeadlines[position].time) + putExtra(NEWS_CONTENT, newsDataForTopHeadlines[position].content) + } + startActivity(intent) + + } } + // After you finish setting up, show the CarouselView + show() } - - // listitem onClick adapter.setOnItemClickListener(object : CustomAdapter.OnItemClickListener { override fun onItemClick(position: Int) { @@ -132,26 +112,6 @@ class GeneralFragment : Fragment() { } }) - //TOPHEADLINES LIST ITEM ONCLICK -// topAdapter.setOnItemClickListener(object : -// CustomAdapterForTopHeadlines.OnItemClickListener { -// override fun onItemClick(position: Int) { -// val pos = position % TOP_HEADLINES_COUNT -// -// val intent = Intent(context, ReadNewsActivity::class.java).apply { -// putExtra(NEWS_URL, newsDataForTopHeadlines[pos].url) -// putExtra(NEWS_TITLE, newsDataForTopHeadlines[pos].headLine) -// putExtra(NEWS_IMAGE_URL, newsDataForTopHeadlines[pos].image) -// putExtra(NEWS_DESCRIPTION, newsDataForTopHeadlines[pos].description) -// putExtra(NEWS_SOURCE, newsDataForTopHeadlines[pos].source) -// putExtra(NEWS_PUBLICATION_TIME, newsDataForTopHeadlines[pos].time) -// putExtra(NEWS_CONTENT, newsDataForTopHeadlines[pos].content) -// } -// -// startActivity(intent) -// } -// }) - // Ignore adapter.setOnItemLongClickListener(object : CustomAdapter.OnItemLongClickListener { override fun onItemLongClick(position: Int) = Unit @@ -160,20 +120,4 @@ class GeneralFragment : Fragment() { return view } - private var currentSliderPosition = INITIAL_POSITION - - // pause slider when fragment paused - override fun onPause() { - mainHandler.removeCallbacks(swiper) - currentSliderPosition = position - super.onPause() - } - - // resume slider when fragment resumed - override fun onResume() { - mainHandler.post(swiper) - position = currentSliderPosition - super.onResume() - } - } \ No newline at end of file diff --git a/app/src/main/res/layout/list_item_for_top_headlines.xml b/app/src/main/res/layout/list_item_for_top_headlines.xml index 884bcbe..00d5a89 100644 --- a/app/src/main/res/layout/list_item_for_top_headlines.xml +++ b/app/src/main/res/layout/list_item_for_top_headlines.xml @@ -30,21 +30,6 @@ android:scaleType="centerCrop" android:src="@drawable/samplenews" /> - - - @@ -52,9 +37,10 @@ android:id="@+id/headline" android:layout_width="match_parent" android:layout_height="wrap_content" - android:maxLines="4" + android:minHeight="70dp" + android:maxLines="3" android:padding="5dp" - android:text="Hello , this is sample news....." + android:text="Hello , ypesetter in the book. It usually begins with" android:textStyle="bold" />