Skip to content

Commit

Permalink
Merge pull request #3 from deysouparno/feature/Improve_card-slider/Ca…
Browse files Browse the repository at this point in the history
…rousel

Implement carousel for home page
  • Loading branch information
Raj-m01 authored Oct 9, 2022
2 parents f7443f4 + 365b92f commit 5873871
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 46 deletions.
1 change: 1 addition & 0 deletions .idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,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
buildConfigField("String", 'API_KEY', API_KEY)
buildConfigField("String", "API_KEY", "\"5a3e054de1834138a2fbc4a75ee69053\"")

}

Expand Down Expand Up @@ -80,4 +80,7 @@ dependencies {
implementation 'com.squareup.picasso:picasso:2.71828'

implementation 'com.facebook.shimmer:shimmer:0.5.0'

implementation 'com.github.jama5262:CarouselView:1.2.2'

}
4 changes: 2 additions & 2 deletions app/src/main/java/com/example/newsapp/SavedNewsActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class SavedNewsActivity : AppCompatActivity() {
override fun onItemLongClick(position: Int) {
// Delete saved news dialog
recyclerView.findViewHolderForAdapterPosition(position)?.itemView?.setBackgroundColor(
getThemeColor(R.attr.colorPrimaryVariant)
getThemeColor(com.google.android.material.R.attr.colorPrimaryVariant)
)

val alertDialog = AlertDialog.Builder(this@SavedNewsActivity).apply {
Expand All @@ -98,7 +98,7 @@ class SavedNewsActivity : AppCompatActivity() {

setNegativeButton("No") { _, _ ->
recyclerView.findViewHolderForAdapterPosition(position)?.itemView?.setBackgroundColor(
getThemeColor(R.attr.colorPrimary)
getThemeColor(com.google.android.material.R.attr.colorPrimary)
)
}
}.create()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.os.Looper
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
Expand All @@ -26,14 +27,20 @@ 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
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 topAdapter: CustomAdapterForTopHeadlines
// 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<NewsModel>
private lateinit var newsDataForDown: List<NewsModel>
Expand All @@ -45,49 +52,68 @@ class GeneralFragment : Fragment() {
): View? {
val view = inflater.inflate(R.layout.fragment_general, container, false)
val layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
val layoutManagerTop = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)

recyclerView = view.findViewById(R.id.recyclerView)
recyclerViewTop = view.findViewById(R.id.recyclerView_top)
recyclerView.layoutManager = layoutManager
recyclerViewTop.layoutManager = layoutManagerTop

// Setting recyclerViews adapter
newsDataForTopHeadlines = MainActivity.generalNews.slice(0 until TOP_HEADLINES_COUNT)
newsDataForDown = MainActivity.generalNews.slice(TOP_HEADLINES_COUNT until MainActivity.generalNews.size - TOP_HEADLINES_COUNT)
topAdapter = CustomAdapterForTopHeadlines(newsDataForTopHeadlines)
adapter = CustomAdapter(newsDataForDown)
recyclerViewTop.adapter = topAdapter
recyclerView.adapter = adapter


carouselView = view.findViewById<CarouselView>(R.id.home_carousel)

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<ImageView>(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)
// recyclerViewTop.smoothScrollToPosition(position)
position++
mainHandler.postDelayed(this, DEFAULT_SWIPER_DELAY)
}
}

var stateChanged = false

recyclerViewTop.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
super.onScrollStateChanged(recyclerView, newState)

carouselView.carouselScrollListener = object : CarouselScrollListener {
override fun onScrollStateChanged(
recyclerView: RecyclerView,
newState: Int,
position: Int
) {
if (newState == RecyclerView.SCROLL_STATE_DRAGGING) {
mainHandler.removeCallbacks(swiper)
stateChanged = true
}
}

override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {

if (stateChanged && newState == RecyclerView.SCROLL_STATE_IDLE) {
position = layoutManagerTop.findFirstVisibleItemPosition() + 1
mainHandler.post(swiper)
stateChanged = false
}
}
})
}



// listitem onClick
adapter.setOnItemClickListener(object : CustomAdapter.OnItemClickListener {
Expand All @@ -107,24 +133,24 @@ 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)
}
})
// 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 {
Expand Down
17 changes: 14 additions & 3 deletions app/src/main/res/layout/fragment_general.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".fragmentClasses.GeneralFragment">

<LinearLayout
Expand All @@ -20,11 +21,21 @@
android:textSize="22sp"
android:textStyle="bold" />

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView_top"
<com.jama.carouselview.CarouselView
android:id="@+id/home_carousel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="280dp" />
app:enableSnapping="true"
app:scaleOnScroll="false"
app:setAutoPlay="true"
app:setAutoPlayDelay="3000"
app:carouselOffset="center"
app:indicatorAnimationType="drop"
app:indicatorRadius="5"
app:indicatorPadding="5"
app:size="10"
app:spacing="10"
app:resource="@layout/list_item_for_top_headlines"/>

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/res/layout/list_item_for_top_headlines.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/card1"
android:layout_width="match_parent"
android:layout_height="280dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:padding="5dp"
app:cardCornerRadius="10dp"
Expand Down Expand Up @@ -35,13 +35,15 @@
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|start"
android:layout_margin="10dp"
android:visibility="gone"
android:src="@drawable/ic_baseline_arrow_back_ios_24" />

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|end"
android:layout_margin="10dp"
android:visibility="gone"
android:src="@drawable/ic_baseline_arrow_forward_ios_24" />

</com.google.android.material.card.MaterialCardView>
Expand Down
3 changes: 2 additions & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ dependencyResolutionManagement {
repositories {
google()
mavenCentral()
jcenter() // Warning: this repository is going to shut down soon
jcenter() //
maven { url 'https://jitpack.io' }// Warning: this repository is going to shut down soon
}
}
rootProject.name = "NewsApp"
Expand Down

0 comments on commit 5873871

Please sign in to comment.