소개
게시글
질문&답변
북마크 탭에서 삭제기능을 추가하려고합니다
선생님 ... 올려주신거를 참고로 인터페이스 추가해서 만들어봤습니다근데하지만 북마크 클릭시 강제종료가되는 현상이발생합니다 .. ㅠㅠ마지막으로 저가 맞게 했는지만 봐주시면 안될까요? 정말죄송합니다..코드 자세히 보실려면 깃허브 저번꺼에 업로드 했어요.. rvAdapter.setOnItemClickListener(object : BookmarkRVAdapter.OnItemClickListener{ override fun onItemClick(v: View, data: ContentModel, pos : Int) { Intent(context,BookmarkFragment::class.java).apply { Log.d("clicktest:", "클릭")// var ft: FragmentTransaction = fragmentManager!!.beginTransaction()// ft.detach(Fragment()).attach(Fragment()).commit() addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) }.run { startActivity(this) } } }) 이부분에서 계속 오류가 뜨는거같은데.. ㅠㅠㅠㅠ :(BookmarkRVAdapter.ktpackage com.example.communityapp.contentlistimport android.content.Contextimport android.content.Intentimport android.util.Logimport android.view.LayoutInflaterimport android.view.Viewimport android.view.ViewGroupimport android.widget.ImageViewimport android.widget.TextViewimport android.widget.Toastimport androidx.recyclerview.widget.RecyclerViewimport com.bumptech.glide.Glideimport com.example.communityapp.Rimport com.example.communityapp.databinding.FragmentBookmarkBindingimport com.example.communityapp.fragments.BookmarkFragmentimport com.example.communityapp.utills.FBRefimport com.example.communityapp.utills.FBauthclass BookmarkRVAdapter(val context: Context,val items: ArrayList,val keyList:ArrayList,val bookmarkIdList:MutableList) : RecyclerView.Adapter() {var datas = mutableListOf()override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BookmarkRVAdapter.ViewHolder {val v = LayoutInflater.from(parent.context).inflate(R.layout.content_rv_item, parent, false)return ViewHolder(v)}override fun onBindViewHolder(holder: BookmarkRVAdapter.ViewHolder, position: Int) {holder.bindItem(items[position],keyList[position])}override fun getItemCount(): Int {return items.size }// Click 이벤트 적용하기 interface OnItemClickListener{fun onItemClick(v:View, data: ContentModel, pos: Int)}private var listener : OnItemClickListener? = null fun setOnItemClickListener(listener : OnItemClickListener) {this.listener = listener}inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {fun bindItem(item: ContentModel,key:String) {itemView.setOnClickListener { Toast.makeText(context, item.title, Toast.LENGTH_SHORT).show()val intent = Intent(context, ContentshowActivity::class.java)intent.putExtra("url", item.webUrl)itemView.context.startActivity(intent)} val contentTitle = itemView.findViewById(R.id.textArea)val imageViewArea = itemView.findViewById(R.id.imageArea)val bookmarkArea=itemView.findViewById(R.id.bookmarkArea)bookmarkArea.setImageResource(R.drawable.bookmark_color)bookmarkArea.setOnClickListener{ Toast.makeText(context, key, Toast.LENGTH_SHORT)Log.d("BookmarkRVAdapter", FBauth.getUid())Log.d("Bookmarkrv:", bookmarkIdList.toString())FBRef.bookmarkRef .child(FBauth.getUid()).child(key).removeValue()val pos = adapterPosition if(pos!= RecyclerView.NO_POSITION){listener?.onItemClick(itemView,item,pos)}// notifyDataSetChanged()} contentTitle.text = item.title Glide.with(context).load(item.imageUrl).into(imageViewArea)}}} BookmarkFragment.kt package com.example.communityapp.fragmentsimport android.content.Intentimport android.os.Bundleimport android.util.Logimport androidx.fragment.app.Fragmentimport android.view.LayoutInflaterimport android.view.Viewimport android.view.ViewGroupimport android.widget.ImageViewimport android.widget.Toastimport androidx.databinding.DataBindingUtilimport androidx.fragment.app.FragmentManagerimport androidx.fragment.app.FragmentTransactionimport androidx.navigation.findNavControllerimport androidx.recyclerview.widget.GridLayoutManagerimport androidx.recyclerview.widget.RecyclerViewimport com.example.communityapp.Rimport com.example.communityapp.contentlist.BookmarkRVAdapterimport com.example.communityapp.contentlist.ContentModelimport com.example.communityapp.databinding.FragmentBookmarkBindingimport com.example.communityapp.utills.FBRefimport com.example.communityapp.utills.FBauthimport com.google.firebase.database.DataSnapshotimport com.google.firebase.database.DatabaseErrorimport com.google.firebase.database.ValueEventListener// TODO: Rename parameter arguments, choose names that matchclass BookmarkFragment : Fragment() {private lateinit var binding: FragmentBookmarkBindinglateinit var rvAdapter: BookmarkRVAdapterval bookmarkIdList = mutableListOf()val items = ArrayList()val itemKeyList = ArrayList()private val TAG = BookmarkFragment::class.java.simpleName override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)}override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,savedInstanceState: Bundle?): View? {binding = DataBindingUtil.inflate(inflater, R.layout.fragment_bookmark, container, false)//2 getBookmarkData()//3 rvAdapter = BookmarkRVAdapter(requireContext(), items, itemKeyList, bookmarkIdList)val rv: RecyclerView = binding.bookmarkRV rv.adapter = rvAdapter rv.layoutManager = GridLayoutManager(requireContext(), 2)binding.hometap.setOnClickListener { it.findNavController().navigate(R.id.action_bookmarkFragment_to_homeFragment)} binding.tiptap.setOnClickListener { it.findNavController().navigate(R.id.action_bookmarkFragment_to_tipFragment)} binding.storetap.setOnClickListener { it.findNavController().navigate(R.id.action_bookmarkFragment_to_storeFragment)} binding.talktap.setOnClickListener { it.findNavController().navigate(R.id.action_bookmarkFragment_to_talkFragment)} rvAdapter.setOnItemClickListener(object : BookmarkRVAdapter.OnItemClickListener{override fun onItemClick(v: View, data: ContentModel, pos : Int) {Intent(context,BookmarkFragment::class.java).apply { Log.d("clicktest:", "클릭")// var ft: FragmentTransaction = fragmentManager!!.beginTransaction()// ft.detach(Fragment()).attach(Fragment()).commit() addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)}.run { startActivity(this) } }})return binding.root }private fun getCategoryData() {val postListener = object : ValueEventListener {override fun onDataChange(dataSnapshot: DataSnapshot) {for (dataModel in dataSnapshot.children) {Log.d(TAG, dataModel.toString())val item = dataModel.getValue(ContentModel::class.java)if (bookmarkIdList.contains(dataModel.key.toString())) {items.add(item!!)itemKeyList.add(dataModel.key.toString())}}rvAdapter.notifyDataSetChanged()}override fun onCancelled(databaseError: DatabaseError) {// Getting Post failed, log a message Log.w("ContentListActivity", "loadPost:onCancelled", databaseError.toException())}}FBRef.category1.addValueEventListener(postListener)FBRef.category2.addValueEventListener(postListener)FBRef.category3.addValueEventListener(postListener)FBRef.category4.addValueEventListener(postListener)FBRef.category5.addValueEventListener(postListener)FBRef.category6.addValueEventListener(postListener)FBRef.category7.addValueEventListener(postListener)FBRef.category8.addValueEventListener(postListener)}private fun getBookmarkData() {val postListener = object : ValueEventListener {override fun onDataChange(dataSnapshot: DataSnapshot) {bookmarkIdList.clear()for (dataModel in dataSnapshot.children) {bookmarkIdList.add(dataModel.key.toString())}//1 rvAdapter.notifyDataSetChanged()getCategoryData()}override fun onCancelled(databaseError: DatabaseError) {// Getting Post failed, log a message Log.w("ContentListActivity", "loadPost:onCancelled", databaseError.toException())}}FBRef.bookmarkRef.child(FBauth.getUid()).addValueEventListener(postListener)}}
- 0
- 5
- 293
질문&답변
북마크 탭에서 삭제기능을 추가하려고합니다
이겁ㄴ다
- 0
- 5
- 293
질문&답변
북마크 탭에서 삭제기능을 추가하려고합니다
선생님 저가 그전 코드에서 부터 getBookmarkData()에서 bookmarkIdList.clear()넣으니깐 idlist 에선 잘 삭제가된걸 확인했는데...Bookmarklayout 에서 그전 idlist 값이 남아있어서? 삭제하면 그전 레이아웃 idlist 값의 레이아웃(이미지) 새롭게 드가는 idlist 값이 동시에 공존해있어 문의 드렸어용혹시나 싶어서 getCategoryData() 에서 bookmarkIdList.clear() 넣어도 그전 idlist (이미지)값 이 bookmarkFragment에 남아있더라구요...ㅠㅠ그래서 이걸 삭제할때마다 레이아웃을 새롭게 리셋 시켜야하나 싶어서 문의드렸어요
- 0
- 5
- 293