跳转功能完善,可以返回上个页面

This commit is contained in:
zhangsan 2024-04-30 09:41:24 +08:00
parent 985582f8de
commit 9210235f45
15 changed files with 124 additions and 36 deletions

View File

@ -29,12 +29,14 @@ class BookInformationActivity : AppCompatActivity() {
private lateinit var authorName: String private lateinit var authorName: String
private lateinit var userId:String private lateinit var userId:String
private lateinit var picurl:String private lateinit var picurl:String
private var sourceActivityClass: Class<*>? = null
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(R.layout.activity_book_info) setContentView(R.layout.activity_book_info)
Bmob.initialize(this, "8f9f1d1ea520b0ce4f84a6fa83a5f754") Bmob.initialize(this, "8f9f1d1ea520b0ce4f84a6fa83a5f754")
bookName = intent.getStringExtra("Book_name") ?: "" bookName = intent.getStringExtra("Book_name") ?: ""
userId = getSharedPreferences("userinf", Context.MODE_PRIVATE).getString("user_id", "").toString() userId = getSharedPreferences("userinf", Context.MODE_PRIVATE).getString("user_id", "").toString()
sourceActivityClass = intent.getSerializableExtra("Source_Activity") as? Class<*>
loadPerceptions() loadPerceptions()
setupRecyclerView() setupRecyclerView()
loadBookInfo() loadBookInfo()
@ -102,7 +104,7 @@ class BookInformationActivity : AppCompatActivity() {
queryBookshelf.findObjects(object : FindListener<BookShelf>() { queryBookshelf.findObjects(object : FindListener<BookShelf>() {
override fun done(bookshelfItems: MutableList<BookShelf>?, e: BmobException?) { override fun done(bookshelfItems: MutableList<BookShelf>?, e: BmobException?) {
if (e == null && bookshelfItems != null && bookshelfItems.isNotEmpty()) { if (e == null && bookshelfItems != null && bookshelfItems.isNotEmpty()) {
bookinfo_addBook.setImageResource(R.drawable.bookshelf2) bookinfo_addBook.setImageResource(R.drawable.alreadyin)
} }
} }
}) })
@ -117,8 +119,15 @@ class BookInformationActivity : AppCompatActivity() {
} }
//TODO:不一定回到MainActivity::class后面还要在跳转前保存上一个页面 //TODO:不一定回到MainActivity::class后面还要在跳转前保存上一个页面
bookinfo_return.setOnClickListener { bookinfo_return.setOnClickListener {
if (sourceActivityClass != null) {
// 返回到上一个页面
val intent = Intent(this, sourceActivityClass!!)
startActivity(intent)
} else {
// 如果没有来源页面信息,则返回到 MainActivity
val intent = Intent(this, MainActivity::class.java) val intent = Intent(this, MainActivity::class.java)
startActivity(intent) startActivity(intent)
}
finish() finish()
} }
@ -131,10 +140,14 @@ class BookInformationActivity : AppCompatActivity() {
intent.putExtra("Book_name", bookName) intent.putExtra("Book_name", bookName)
startActivity(intent) startActivity(intent)
} }
toBookshelf.setOnClickListener {
val intent = Intent(this, BookShelfActivity::class.java)
startActivity(intent)
}
setCommentClickListeners() setCommentClickListeners()
} }
//TODO:以后可以考虑再次点击这个“已加入的按钮”后,弹出一个对话框:是否将该书移除,然后将该书从书架中移除。而不是弹:该书已在您的书架中
private fun addBookToBookshelf() { private fun addBookToBookshelf() {
val queryBookshelf = BmobQuery<BookShelf>() val queryBookshelf = BmobQuery<BookShelf>()
queryBookshelf.addWhereEqualTo("userid", userId) queryBookshelf.addWhereEqualTo("userid", userId)
@ -149,7 +162,7 @@ class BookInformationActivity : AppCompatActivity() {
bookshelfRecord.save(object : SaveListener<String>() { bookshelfRecord.save(object : SaveListener<String>() {
override fun done(objectId: String?, saveException: BmobException?) { override fun done(objectId: String?, saveException: BmobException?) {
if (saveException == null) { if (saveException == null) {
bookinfo_addBook.setImageResource(R.drawable.bookshelf2) bookinfo_addBook.setImageResource(R.drawable.alreadyin)
Toast.makeText( Toast.makeText(
this@BookInformationActivity, this@BookInformationActivity,
"添加成功", "添加成功",
@ -164,7 +177,7 @@ class BookInformationActivity : AppCompatActivity() {
} }
}) })
} else { } else {
Toast.makeText(this@BookInformationActivity, "已在您的书架中!", Toast.LENGTH_SHORT) Toast.makeText(this@BookInformationActivity, "书已在您的书架中!", Toast.LENGTH_SHORT)
.show() .show()
} }
} }

View File

@ -1,5 +1,6 @@
package com.zjgsu.jianshu package com.zjgsu.jianshu
import android.content.Context
import android.content.Intent import android.content.Intent
import android.os.Bundle import android.os.Bundle
import android.widget.Toast import android.widget.Toast
@ -24,7 +25,8 @@ class GoodBookActivity : AppCompatActivity() {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(R.layout.activity_bookrank) setContentView(R.layout.activity_bookrank)
Bmob.initialize(this, "8f9f1d1ea520b0ce4f84a6fa83a5f754")//连接bmob Bmob.initialize(this, "8f9f1d1ea520b0ce4f84a6fa83a5f754")//连接bmob
adapter= BookRankAdapter(goodbookList,false) val userId = getSharedPreferences("userinf", Context.MODE_PRIVATE).getString("user_id", "").toString() //全局范围内userid登录的时候存进来
adapter= BookRankAdapter(this,goodbookList,false,userId)
bookrank_recyclerView.layoutManager= bookrank_recyclerView.layoutManager=
StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL) StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL)
bookrank_recyclerView.adapter=adapter bookrank_recyclerView.adapter=adapter

View File

@ -1,4 +1,5 @@
package com.zjgsu.jianshu package com.zjgsu.jianshu
import android.content.Context
import android.content.Intent import android.content.Intent
import android.os.Bundle import android.os.Bundle
import android.text.TextUtils import android.text.TextUtils
@ -27,7 +28,8 @@ class HotBookActivity: AppCompatActivity() {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(R.layout.activity_bookrank) setContentView(R.layout.activity_bookrank)
Bmob.initialize(this, "8f9f1d1ea520b0ce4f84a6fa83a5f754")//连接bmob Bmob.initialize(this, "8f9f1d1ea520b0ce4f84a6fa83a5f754")//连接bmob
adapter= BookRankAdapter(hotbookList,true) val userId = getSharedPreferences("userinf", Context.MODE_PRIVATE).getString("user_id", "").toString()
adapter= BookRankAdapter(this,hotbookList,true,userId)
bookrank_recyclerView.layoutManager=StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL) bookrank_recyclerView.layoutManager=StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL)
bookrank_recyclerView.adapter=adapter bookrank_recyclerView.adapter=adapter
bookrank_swipeRefresh.setOnRefreshListener { bookrank_swipeRefresh.setOnRefreshListener {

View File

@ -30,7 +30,7 @@ class LiteratureActivity : AppCompatActivity() {
Bmob.initialize(this, "8f9f1d1ea520b0ce4f84a6fa83a5f754") Bmob.initialize(this, "8f9f1d1ea520b0ce4f84a6fa83a5f754")
val titleTextView = findViewById<TextView>(R.id.categoryPage_title) val titleTextView = findViewById<TextView>(R.id.categoryPage_title)
titleTextView.text = "文学" titleTextView.text = "文学"
adapter = BookAdapter2(bookList) adapter = BookAdapter2(bookList,1)
categoryPage_recyclerView.layoutManager = StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL) categoryPage_recyclerView.layoutManager = StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL)
categoryPage_recyclerView.adapter = adapter categoryPage_recyclerView.adapter = adapter
categoryPage_swipeRefresh.setOnRefreshListener { categoryPage_swipeRefresh.setOnRefreshListener {

View File

@ -1,5 +1,6 @@
package com.zjgsu.jianshu package com.zjgsu.jianshu
import android.content.Context
import android.content.Intent import android.content.Intent
import android.os.Bundle import android.os.Bundle
import android.text.TextUtils import android.text.TextUtils

View File

@ -29,7 +29,7 @@ class ManagementActivity:AppCompatActivity() {
Bmob.initialize(this, "8f9f1d1ea520b0ce4f84a6fa83a5f754") Bmob.initialize(this, "8f9f1d1ea520b0ce4f84a6fa83a5f754")
val titleTextView = findViewById<TextView>(R.id.categoryPage_title) val titleTextView = findViewById<TextView>(R.id.categoryPage_title)
titleTextView.text = "管理学" titleTextView.text = "管理学"
adapter = BookAdapter2(bookList) adapter = BookAdapter2(bookList,2)
categoryPage_recyclerView.layoutManager = StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL) categoryPage_recyclerView.layoutManager = StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL)
categoryPage_recyclerView.adapter = adapter categoryPage_recyclerView.adapter = adapter
categoryPage_swipeRefresh.setOnRefreshListener { categoryPage_swipeRefresh.setOnRefreshListener {

View File

@ -30,7 +30,7 @@ class PhilosophyActivity:AppCompatActivity() {
Bmob.initialize(this, "8f9f1d1ea520b0ce4f84a6fa83a5f754") Bmob.initialize(this, "8f9f1d1ea520b0ce4f84a6fa83a5f754")
val titleTextView = findViewById<TextView>(R.id.categoryPage_title) val titleTextView = findViewById<TextView>(R.id.categoryPage_title)
titleTextView.text = "哲学" titleTextView.text = "哲学"
adapter = BookAdapter2(bookList) adapter = BookAdapter2(bookList,5)
categoryPage_recyclerView.layoutManager = StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL) categoryPage_recyclerView.layoutManager = StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL)
categoryPage_recyclerView.adapter = adapter categoryPage_recyclerView.adapter = adapter
categoryPage_swipeRefresh.setOnRefreshListener { categoryPage_swipeRefresh.setOnRefreshListener {

View File

@ -29,7 +29,7 @@ class PsychologyActivity:AppCompatActivity() {
Bmob.initialize(this, "8f9f1d1ea520b0ce4f84a6fa83a5f754") Bmob.initialize(this, "8f9f1d1ea520b0ce4f84a6fa83a5f754")
val titleTextView = findViewById<TextView>(R.id.categoryPage_title) val titleTextView = findViewById<TextView>(R.id.categoryPage_title)
titleTextView.text = "心理学" titleTextView.text = "心理学"
adapter = BookAdapter2(bookList) adapter = BookAdapter2(bookList,3)
categoryPage_recyclerView.layoutManager = StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL) categoryPage_recyclerView.layoutManager = StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL)
categoryPage_recyclerView.adapter = adapter categoryPage_recyclerView.adapter = adapter
categoryPage_swipeRefresh.setOnRefreshListener { categoryPage_swipeRefresh.setOnRefreshListener {

View File

@ -30,7 +30,7 @@ class ScienceActivity:AppCompatActivity() {
Bmob.initialize(this, "8f9f1d1ea520b0ce4f84a6fa83a5f754") Bmob.initialize(this, "8f9f1d1ea520b0ce4f84a6fa83a5f754")
val titleTextView = findViewById<TextView>(R.id.categoryPage_title) val titleTextView = findViewById<TextView>(R.id.categoryPage_title)
titleTextView.text = "科学" titleTextView.text = "科学"
adapter = BookAdapter2(bookList) adapter = BookAdapter2(bookList,4)
categoryPage_recyclerView.layoutManager = StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL) categoryPage_recyclerView.layoutManager = StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL)
categoryPage_recyclerView.adapter = adapter categoryPage_recyclerView.adapter = adapter
categoryPage_swipeRefresh.setOnRefreshListener { categoryPage_swipeRefresh.setOnRefreshListener {

View File

@ -63,6 +63,7 @@ inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
holder.outerLinearLayout.setOnClickListener{ holder.outerLinearLayout.setOnClickListener{
val intent = Intent(holder.itemView.context, BookInformationActivity::class.java) val intent = Intent(holder.itemView.context, BookInformationActivity::class.java)
intent.putExtra("Book_name", book.name) intent.putExtra("Book_name", book.name)
intent.putExtra("Source_Activity", MainActivity::class.java)
ContextCompat.startActivity(holder.itemView.context, intent, null) ContextCompat.startActivity(holder.itemView.context, intent, null)
} }
} }

View File

@ -25,7 +25,7 @@ import java.io.InputStream
import java.net.HttpURLConnection import java.net.HttpURLConnection
import java.net.URL import java.net.URL
class BookAdapter2(val bookList: List<Book>) : RecyclerView.Adapter<BookAdapter2.ViewHolder>() { class BookAdapter2(val bookList: List<Book>,val categoryId:Int) : RecyclerView.Adapter<BookAdapter2.ViewHolder>() {
inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) { inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val bookList_Image: ImageView = view.findViewById(R.id.book_Image) val bookList_Image: ImageView = view.findViewById(R.id.book_Image)

View File

@ -1,19 +1,29 @@
package com.zjgsu.jianshu.Adapter package com.zjgsu.jianshu.Adapter
import android.content.Context
import android.content.Intent import android.content.Intent
import android.media.Image import android.media.Image
import android.util.Log
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.ImageView import android.widget.ImageView
import android.widget.LinearLayout import android.widget.LinearLayout
import android.widget.TextView import android.widget.TextView
import android.widget.Toast
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import cn.bmob.v3.BmobQuery
import cn.bmob.v3.exception.BmobException
import cn.bmob.v3.listener.FindListener
import cn.bmob.v3.listener.SaveListener
import com.bumptech.glide.Glide import com.bumptech.glide.Glide
import com.zjgsu.jianshu.* import com.zjgsu.jianshu.*
import com.zjgsu.jianshu.Bmob.BookShelf
import kotlinx.android.synthetic.main.bookinfo_title.*
class BookRankAdapter(var bookList: List<Book_rank>,val isHotList:Boolean): RecyclerView.Adapter<BookRankAdapter.ViewHolder>() { class BookRankAdapter(private val context: Context,var bookList: List<Book_rank>, val isHotList: Boolean, val userId: String) :
RecyclerView.Adapter<BookRankAdapter.ViewHolder>() {
inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) { inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val bookrank_pic: ImageView = view.findViewById(R.id.bookrank_pic) val bookrank_pic: ImageView = view.findViewById(R.id.bookrank_pic)
val bookrank_intro: TextView = view.findViewById(R.id.bookrank_intro) val bookrank_intro: TextView = view.findViewById(R.id.bookrank_intro)
@ -23,10 +33,13 @@ class BookRankAdapter(var bookList: List<Book_rank>,val isHotList:Boolean): Recy
val outerLinearLayout: LinearLayout = view.findViewById(R.id.bookrank_outerLinearLayout) val outerLinearLayout: LinearLayout = view.findViewById(R.id.bookrank_outerLinearLayout)
val bookrank_author: TextView = view.findViewById(R.id.bookrank_author) val bookrank_author: TextView = view.findViewById(R.id.bookrank_author)
val bookrank_icon: ImageView = view.findViewById(R.id.bookrank_icon) val bookrank_icon: ImageView = view.findViewById(R.id.bookrank_icon)
val bookrank_addbook: ImageView = view.findViewById(R.id.bookrank_addbook)
val bookrank_add:TextView=view.findViewById(R.id.bookrank_add)
} }
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.book_rank2_item, parent, false) val view =
LayoutInflater.from(parent.context).inflate(R.layout.book_rank2_item, parent, false)
return ViewHolder(view) return ViewHolder(view)
} }
@ -36,7 +49,8 @@ class BookRankAdapter(var bookList: List<Book_rank>,val isHotList:Boolean): Recy
holder.bookrank_intro.text = book.intro holder.bookrank_intro.text = book.intro
val backgrounds = arrayOf( val backgrounds = arrayOf(
R.drawable.color_lightpurple, R.drawable.color_lightblue, R.drawable.color_lightpurple, R.drawable.color_lightblue,
R.drawable.color_lightgreen, R.drawable.color_flesh) R.drawable.color_lightgreen, R.drawable.color_flesh
)
holder.innerLinearLayout.setBackgroundResource(backgrounds[position % backgrounds.size]) holder.innerLinearLayout.setBackgroundResource(backgrounds[position % backgrounds.size])
if (isHotList) if (isHotList)
holder.bookrank_desc.text = "在读人数:" + book.courtOfReaders holder.bookrank_desc.text = "在读人数:" + book.courtOfReaders
@ -69,8 +83,62 @@ class BookRankAdapter(var bookList: List<Book_rank>,val isHotList:Boolean): Recy
holder.outerLinearLayout.setOnClickListener { holder.outerLinearLayout.setOnClickListener {
val intent = Intent(holder.itemView.context, BookInformationActivity::class.java) val intent = Intent(holder.itemView.context, BookInformationActivity::class.java)
intent.putExtra("Book_name", book.name) intent.putExtra("Book_name", book.name)
if(isHotList)
intent.putExtra("Source_Activity", HotBookActivity::class.java)
else
intent.putExtra("Source_Activity", GoodBookActivity::class.java)
ContextCompat.startActivity(holder.itemView.context, intent, null) ContextCompat.startActivity(holder.itemView.context, intent, null)
} }
holder.bookrank_addbook.setOnClickListener {
val queryBookshelf = BmobQuery<BookShelf>()
queryBookshelf.addWhereEqualTo("userid", userId)
queryBookshelf.addWhereEqualTo("b_name", book.name)
queryBookshelf.findObjects(object : FindListener<BookShelf>() {
override fun done(bookshelfItems: MutableList<BookShelf>?, e: BmobException?) {
if (e == null && (bookshelfItems == null || bookshelfItems.isEmpty())) {
val bookshelfRecord = BookShelf()
bookshelfRecord.setb_name(book.name)
bookshelfRecord.setuserid(userId)
bookshelfRecord.setpicurl(book.picUrl)
bookshelfRecord.save(object : SaveListener<String>() {
override fun done(objectId: String?, saveException: BmobException?) {
if (saveException == null) {
holder.bookrank_addbook.setImageResource(R.drawable.bookshelf2)
Toast.makeText(
context,
"添加成功",
Toast.LENGTH_SHORT
).show()
} else {
Log.e(
"BookInfo",
"Error adding book to bookshelf: ${saveException.message}"
)
} }
}
})
} else {
Toast.makeText(context, "该书已在您的书架中!", Toast.LENGTH_SHORT)
.show()
}
}
})
}
fun judgeInBookshelf() {
val queryBookshelf = BmobQuery<BookShelf>()
queryBookshelf.addWhereEqualTo("userid", userId)
queryBookshelf.addWhereEqualTo("b_name", book.name)
queryBookshelf.findObjects(object : FindListener<BookShelf>() {
override fun done(bookshelfItems: MutableList<BookShelf>?, e: BmobException?) {
if (e == null && bookshelfItems != null && bookshelfItems.isNotEmpty()) {
holder.bookrank_addbook.setImageResource(R.drawable.alreadyin)
holder.bookrank_add.text="已加入"
}
}
})
}
judgeInBookshelf()
}
override fun getItemCount() = bookList.size override fun getItemCount() = bookList.size
} }

View File

@ -61,6 +61,7 @@ class BookshelfAdapter(val context: Context, val bookshelf: ArrayList<Book_Shelf
} else { } else {
val intent = Intent(holder.itemView.context, BookInformationActivity::class.java) val intent = Intent(holder.itemView.context, BookInformationActivity::class.java)
intent.putExtra("Book_name", book.bkname) intent.putExtra("Book_name", book.bkname)
intent.putExtra("Source_Activity", BookShelfActivity::class.java)
ContextCompat.startActivity(holder.itemView.context, intent, null) ContextCompat.startActivity(holder.itemView.context, intent, null)
} }
} }

View File

@ -8,7 +8,6 @@
app:cardElevation="4dp"> app:cardElevation="4dp">
<LinearLayout <LinearLayout
android:id="@+id/bookrank_outerLinearLayout"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical" android:orientation="vertical"
@ -16,6 +15,7 @@
<!-- 第一行:图书封面和简介 --> <!-- 第一行:图书封面和简介 -->
<LinearLayout <LinearLayout
android:id="@+id/bookrank_outerLinearLayout"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="200dp" android:layout_height="200dp"
android:orientation="horizontal"> android:orientation="horizontal">
@ -117,7 +117,7 @@
android:src="@drawable/addbook" /> android:src="@drawable/addbook" />
<TextView <TextView
android:id="@+id/tvLikeText" android:id="@+id/bookrank_add"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="加入书架" android:text="加入书架"

View File

@ -25,15 +25,15 @@
android:layout_width="34dp" android:layout_width="34dp"
android:layout_height="30dp" android:layout_height="30dp"
android:layout_gravity="center" android:layout_gravity="center"
app:srcCompat="@drawable/bookshelf1" /> app:srcCompat="@drawable/addbook" />
<ImageView <ImageView
android:id="@+id/imageView2" android:id="@+id/toBookshelf"
android:layout_width="37dp" android:layout_width="34dp"
android:layout_height="41dp" android:layout_height="30dp"
android:layout_gravity="center" android:layout_gravity="center"
android:layout_margin="5dp" android:layout_margin="5dp"
app:srcCompat="@drawable/shopcart" /> app:srcCompat="@drawable/bookshelf1" />
<ImageView <ImageView
android:id="@+id/imageView7" android:id="@+id/imageView7"