こんばんは。
layoutについて、以下に変更した結果取得ができなくなったのですが、おわかりになりますでしょうか。
原因はlayoutだと思うのですが、、
変更点は※をつけております。
ご教示いただければ幸いです!
以下が取得できなくなったと思うのですが、エラーが出てなく...
recycler.setHasFixedSize(true)
recycler.layoutManager = LinearLayoutManager(this)
recycler.addItemDecoration(DividerItemDecoration(this, DividerItemDecoration.VERTICAL))
adapter = ItemRecyclerViewAdapter(ArrayList(), ArrayList(), this)
recycler.adapter = adapter
xml
1<android.support.design.widget.CoordinatorLayout 2 3 <android.support.design.widget.FloatingActionButton 4 5</androidx.coordinatorlayout.widget.CoordinatorLayout> 6 7---------------------------------------????------------------------ 8<?xml version="1.0" encoding="utf-8"?> 9※<androidx.coordinatorlayout.widget.CoordinatorLayout 10 xmlns:android="http://schemas.android.com/apk/res/android" 11 xmlns:tools="http://schemas.android.com/tools" 12 xmlns:app="http://schemas.android.com/apk/res-auto" 13 android:layout_width="match_parent" 14 android:layout_height="match_parent" 15 tools:context=".MainActivity" android:id="@+id/context_view"> 16 17 ※<com.google.android.material.floatingactionbutton.FloatingActionButton 18 android:layout_width="wrap_content" 19 android:layout_height="wrap_content" 20 android:clickable="true" app:srcCompat="@drawable/ic_add_black_24dp" 21 android:id="@+id/fab" android:layout_gravity="bottom|end" android:layout_marginEnd="16dp" 22 android:layout_marginBottom="16dp"/> 23 <androidx.recyclerview.widget.RecyclerView 24 android:layout_width="match_parent" 25 android:layout_height="match_parent" android:layout_marginTop="8dp" 26 android:layout_marginStart="8dp" android:layout_marginEnd="8dp" 27 android:layout_marginBottom="8dp" 28 android:id="@+id/recycler"/> 29 30※</androidx.coordinatorlayout.widget.CoordinatorLayout>
kt
1 2class MainActivity : AppCompatActivity() { 3 4 private val createNewTodoKey: Int = 1 5 private lateinit var realm: Realm 6 private lateinit var adapter: ItemRecyclerViewAdapter 7 8 override fun onCreate(savedInstanceState: Bundle?) { 9 super.onCreate(savedInstanceState) 10 setContentView(R.layout.activity_main) 11 12 realm = Realm.getDefaultInstance() 13 14 // RecyclerViewの初期化 15 initRecyclerView() 16 17 // ドラッグ&ドロップとスワイプの処理 18 val itemTouchHelper = ItemTouchHelper(object : ItemTouchHelper.SimpleCallback(ItemTouchHelper.UP or ItemTouchHelper.DOWN 19 , ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT) { 20 21 override fun onMove(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, target: RecyclerView.ViewHolder): Boolean { 22 val fromPosition = viewHolder.adapterPosition 23 val toPosition = target.adapterPosition 24 25 adapter.insertItem(fromPosition, toPosition) 26 recycler.adapter?.notifyItemMoved(fromPosition, toPosition) 27 28 moveItemInRealm(fromPosition + 1, toPosition + 1) 29 30 return true 31 } 32 33 override fun onChildDraw( 34 c: Canvas, 35 recyclerView: RecyclerView, 36 viewHolder: RecyclerView.ViewHolder, 37 dX: Float, 38 dY: Float, 39 actionState: Int, 40 isCurrentlyActive: Boolean 41 ) { 42 43 44 val background = ColorDrawable() 45 val itemView = viewHolder.itemView 46 val isLeftDirection = dX < 0 47 val deleteIcon = getDrawable(R.drawable.ic_delete_white_24dp) 48 49 background.color = Color.parseColor("#f44336") 50 51 if (isLeftDirection) { 52 background.setBounds(itemView.right + dX.toInt(), itemView.top, itemView.right, itemView.bottom) 53 } else { 54 background.setBounds(itemView.left, itemView.top, itemView.left + dX.toInt(), itemView.bottom) 55 } 56 background.draw(c) 57 58 59 if (deleteIcon != null) { 60 val itemHeight = itemView.bottom - itemView.top 61 val iconWidth = deleteIcon.intrinsicWidth 62 val iconHeight = deleteIcon.intrinsicHeight 63 64 val iconTop = itemView.top + (itemHeight - iconHeight) / 2 65 val iconBottom = iconTop + iconHeight 66 if (isLeftDirection) { 67 val iconRight = itemView.right - (itemHeight - iconHeight) / 2 68 val iconLeft = iconRight - iconWidth 69 deleteIcon.setBounds(iconLeft, iconTop, iconRight, iconBottom) 70 deleteIcon.draw(c) 71 72 } else { 73 val iconLeft = itemView.left + (itemHeight - iconHeight) / 2 74 val iconRight = iconLeft + iconWidth 75 deleteIcon.setBounds(iconLeft, iconTop, iconRight, iconBottom) 76 deleteIcon.draw(c) 77 } 78 } 79 80 super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive) 81 82 } 83 84 override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) { 85 val position = viewHolder.adapterPosition 86 87 // 一時保存 88 val tmpItem = adapter.getItem(position) 89 val tmpIsChecked = adapter.getIsChecked(position) 90 91 adapter.removeItem(position) 92 recycler.adapter?.notifyItemRemoved(position) 93 94 95 var isCancelled = false 96 Snackbar 97 .make(context_view, R.string.item_removed_message, Snackbar.LENGTH_LONG) 98 .addCallback(object : BaseTransientBottomBar.BaseCallback<Snackbar>() { 99 override fun onDismissed(transientBottomBar: Snackbar?, event: Int) { 100 super.onDismissed(transientBottomBar, event) 101 if (!isCancelled) { 102 removeItemFromRealm(position + 1) 103 } 104 } 105 }) 106 .setAction(R.string.item_undo_message, View.OnClickListener { 107 adapter.insertNewItem(tmpItem, tmpIsChecked, position) 108 isCancelled = true 109 }) 110 .show() 111 } 112 113 }) 114 itemTouchHelper.attachToRecyclerView(recycler) 115 116 fab.setOnClickListener { 117 val intent = Intent(this, EditToDO::class.java) 118 startActivityForResult(intent, createNewTodoKey) 119 } 120 121 } 122 123 override fun onResume() { 124 super.onResume() 125 val todoLists = realm.where(ToDoRealm::class.java).sort("id", Sort.ASCENDING).findAll() 126 adapter.clearAllItems() 127 todoLists.forEach { 128 adapter.addItem(it.task, it.isChecked) 129 } 130 } 131 132 override fun onDestroy() { 133 super.onDestroy() 134 realm.close() 135 } 136 137 override fun onActivityResult(requestCode: Int, resultCode: Int, intent: Intent?) { 138 super.onActivityResult(requestCode, resultCode, intent) 139 140 if (resultCode == Activity.RESULT_OK && 141 requestCode == createNewTodoKey && 142 intent != null) { 143 144 val todoBody = intent.extras?.getString("key1").toString() 145 if (todoBody == "") return 146 147 // RecyclerViewへ追加 148 adapter.addItem(todoBody, false) 149 150 // realmへ保存 151 addNewItemToRealm(todoBody) 152 } 153 } 154 155 private fun addNewItemToRealm(body: String) { 156 realm.executeTransaction { 157 val todoObj = realm.createObject(ToDoRealm::class.java) 158 todoObj.task = body 159 todoObj.id = createNewId() 160 } 161 } 162 163 private fun removeItemFromRealm(id: Int) { 164 val itemList = realm.where(ToDoRealm::class.java).sort("id", Sort.DESCENDING) 165 val maxId = itemList.findFirst()!!.id 166 167 // データの削除 168 val item = itemList.equalTo("id", id).findAll() 169 realm.executeTransaction { 170 item.deleteFromRealm(0) 171 } 172 173 val updateItemList = realm.where(ToDoRealm::class.java).sort("id", Sort.ASCENDING).findAll() 174 175 if (maxId == id) {return} 176 else { 177 realm.executeTransaction { 178 for (index in (id + 1)..maxId) { 179 updateItemList[index - 2]!!.id -= 1 180 } 181 } 182 } 183 } 184 185 private fun moveItemInRealm(fromId: Int, toId: Int) { 186 val itemList = realm.where(ToDoRealm::class.java).sort("id", Sort.ASCENDING).findAll() 187 188 realm.executeTransaction { 189 val tmpTask = itemList[toId - 1]!!.task 190 itemList[toId - 1]!!.task = itemList[fromId - 1]!!.task 191 itemList[fromId - 1]!!.task = tmpTask 192 } 193 } 194 195 private fun createNewId(): Int { 196 val id: Int = realm.where(ToDoRealm::class.java).sort("id", Sort.DESCENDING).findFirst()?.id ?: 0 197 return id + 1 198 } 199 200 private fun initRecyclerView() { 201 recycler.setHasFixedSize(true) 202 recycler.layoutManager = LinearLayoutManager(this) 203 recycler.addItemDecoration(DividerItemDecoration(this, DividerItemDecoration.VERTICAL)) 204 adapter = ItemRecyclerViewAdapter(ArrayList(), ArrayList(), this) 205 recycler.adapter = adapter 206 } 207 208}
あなたの回答
tips
プレビュー