build.gradle(app)
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.todolistapp"
minSdkVersion 16
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
// Room
def room_version = "2.0.0"
implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"
// ViewModel and LiveData
def lifecycle_version = "2.0.0"
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
kapt "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"
// For Floating Action Button
implementation 'com.google.android.material:material:1.0.0'
}
as you can see from my dependencies, I did not import the recycled view Androidx library.
androidx.recyclerview:recyclerview:1.0.0
but as shown below, I can comfortably use it in my layout (activity_main.xml) and MainActivity code.
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerViewTasks"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingBottom="80dp"/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
android:tint="@android:color/white"
app:srcCompat="@android:drawable/ic_input_add"/>
</FrameLayout>
MainActivity.kt
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity(), TaskAdapter.TaskViewCliskListener {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
recyclerViewTasks.layoutManager = LinearLayoutManager(this)
}
What is responsible for this behavior?
RecyclerView does not allocate an item view for every item in your data source. Instead, it allocates only the number of item views that fit on the screen(Viewport) and it reuses those item layouts as the user scrolls.
setAdapter(themeAdapter); recyclerView. setHasFixedSize(true); recyclerView. setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager. HORIZONTAL, false));
you should use recyclerview because it offers more control than listview. It is litte bit complex but you get there then your life will be super easy whenever you are dealing with list kind of thing.
As per the dependencies of the com.google.android.material:material:1.0.0
:
androidx.recyclerview:recyclerview:1.0.0
This means that the Material library takes a transitive dependency on RecyclerView already and you don't need to manually include it yourself.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With