Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#2 Update the Listview UI #8

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .idea/codeStyles/codeStyleConfig.xml

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

32 changes: 16 additions & 16 deletions app/src/main/java/com/example/android/notekeeper/ListActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,36 @@ package com.example.android.notekeeper
import android.content.Intent
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.widget.ArrayAdapter
import android.support.v7.widget.DividerItemDecoration
import android.support.v7.widget.LinearLayoutManager
import com.example.android.notekeeper.adp.NoteAdp
import kotlinx.android.synthetic.main.activity_list.*
import kotlinx.android.synthetic.main.content_list.*

class ListActivity : AppCompatActivity() {
class ListActivity : AppCompatActivity(), NoteAdp.Listener {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_list)
setSupportActionBar(toolbar)

fab.setOnClickListener { view ->
val intent = Intent(this, MainActivity::class.java)
fab.setOnClickListener { openNewActivity(null) }

startActivity(intent)
listOfNotes.apply {
layoutManager = LinearLayoutManager(context)
addItemDecoration(DividerItemDecoration(context, DividerItemDecoration.HORIZONTAL))
}

listOfNotes.adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, DataManager.notes)

listOfNotes.setOnItemClickListener { parent, view, position, id ->
val intent = Intent(this, MainActivity::class.java)

intent.putExtra(NOTE_POSITION, position)
listOfNotes.adapter = NoteAdp(DataManager.notes, this, this)
}

startActivity(intent)
}
override fun onItemClicked(adapterPosition: Int) {
openNewActivity(adapterPosition)
}

override fun onResume() {
super.onResume()
(listOfNotes.adapter as ArrayAdapter<NoteInfo>).notifyDataSetChanged()
private fun openNewActivity(adpPosition: Int?) {
val intent = Intent(this, MainActivity::class.java)
adpPosition?.run { intent.putExtra(NOTE_POSITION, adpPosition) }
startActivity(intent)
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package com.example.android.notekeeper

data class CourseInfo (val courseId: String, val title: String) {
override fun toString(): String {
return title
}
}
data class CourseInfo(val courseId: String, val title: String)

data class NoteInfo (var course: CourseInfo? = null, var title: String? = null, var text: String? = null)
48 changes: 48 additions & 0 deletions app/src/main/java/com/example/android/notekeeper/adp/NoteAdp.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.example.android.notekeeper.adp

import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.example.android.notekeeper.NoteInfo
import com.example.android.notekeeper.R
import kotlinx.android.synthetic.main.item_note.view.*

class NoteAdp(private val noteInfoList: List<NoteInfo>, private val context: Context, private val listener: Listener) :
victorvicari marked this conversation as resolved.
Show resolved Hide resolved
RecyclerView.Adapter<NoteAdp.ViewHolder>() {

interface Listener {
fun onItemClicked(adapterPosition: Int)
}

override fun onCreateViewHolder(parent: ViewGroup, p1: Int): ViewHolder {
val inflater = LayoutInflater.from(parent.context)
val layout = inflater.inflate(R.layout.item_note, parent, false)

return ViewHolder(layout, listener)
}

override fun onBindViewHolder(holder: ViewHolder, positionItem: Int) {
holder.bindView(noteInfoList[positionItem], context)
}

override fun getItemCount(): Int = noteInfoList.size


class ViewHolder(private val view: View, listener: Listener) : RecyclerView.ViewHolder(view) {

init {
view.setOnClickListener { listener.onItemClicked(adapterPosition) }
}

fun bindView(noteInfo: NoteInfo, context: Context) {
view.item_note_info.text = context.getText(R.string.note)
view.item_note_course.text = noteInfo.course?.title
view.item_note_title.text = noteInfo.title
view.item_note_text.text = noteInfo.text
}

}

}
35 changes: 18 additions & 17 deletions app/src/main/res/layout/content_list.xml
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_list"
tools:context=".ListActivity">
<android.support.constraint.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".ListActivity"
tools:showIn="@layout/activity_list">

<android.support.v7.widget.RecyclerView
android:id="@+id/listOfNotes"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="56dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<ListView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent" android:layout_marginTop="56dp"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp" app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="8dp"
android:id="@+id/listOfNotes"/>
</android.support.constraint.ConstraintLayout>
73 changes: 73 additions & 0 deletions app/src/main/res/layout/item_note.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.card.MaterialCardView 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:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:cardCornerRadius="8dp"
app:strokeWidth="1dp">

<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/item_note_info"
style="@style/AppTheme.Text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:textSize="17sp"
app:layout_constraintBottom_toTopOf="@+id/item_note_course"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="Note Info" />

<TextView
android:id="@+id/item_note_course"
style="@style/AppTheme.Text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="8dp"
app:layout_constraintBottom_toTopOf="@+id/item_note_title"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/item_note_info"
tools:text="Physics" />

<TextView
android:id="@+id/item_note_title"
style="@style/AppTheme.Text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="8dp"
app:layout_constraintBottom_toTopOf="@+id/item_note_text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/item_note_course"
tools:text="Test 1" />

<TextView
android:id="@+id/item_note_text"
style="@style/AppTheme.Text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/item_note_title"
tools:text="Test 1" />

</android.support.constraint.ConstraintLayout>
victorvicari marked this conversation as resolved.
Show resolved Hide resolved

</android.support.design.card.MaterialCardView>
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
<string name="app_name">Edit note</string>
<string name="action_settings">Settings</string>
<string name="title_activity_list">Note Keeper</string>

<string name="note">Note Info:</string>
</resources>
6 changes: 6 additions & 0 deletions app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,10 @@
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar"/>
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light"/>

<style name="AppTheme.Text" parent="TextAppearance.AppCompat">
<item name="android:textColor">@android:color/black</item>
<item name="android:textSize">16sp</item>
<item name="android:padding">5dp</item>
</style>

</resources>