问题编辑改造还差搜索提示框顶部蓝线
This commit is contained in:
@ -6,7 +6,6 @@ import android.arch.lifecycle.ViewModelProviders
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import android.os.Message
|
||||
import android.provider.MediaStore
|
||||
import android.support.v7.widget.GridLayoutManager
|
||||
import android.support.v7.widget.RecyclerView
|
||||
@ -14,17 +13,14 @@ import android.text.Editable
|
||||
import android.text.TextUtils
|
||||
import android.text.TextWatcher
|
||||
import android.view.MenuItem
|
||||
import android.widget.AbsListView
|
||||
import android.widget.AbsListView.OnScrollListener.SCROLL_STATE_IDLE
|
||||
import android.widget.AutoCompleteTextView
|
||||
import android.widget.EditText
|
||||
import android.widget.ListPopupWindow
|
||||
import com.gh.base.fragment.BaseDialogWrapperFragment
|
||||
import com.gh.base.fragment.WaitingDialogFragment
|
||||
import com.gh.common.util.DisplayUtils
|
||||
import com.gh.common.util.DialogUtils
|
||||
import com.gh.common.util.EntranceUtils
|
||||
import com.gh.gamecenter.SuggestionActivity.MEDIA_STORE_REQUEST
|
||||
import com.gh.gamecenter.ask.entity.QuestionsDetailEntity
|
||||
import com.gh.gamecenter.ask.entity.QuestionsIndexEntity
|
||||
import com.gh.gamecenter.ask.questionsedit.QuestionsEditViewModel
|
||||
import com.gh.gamecenter.ask.questionsedit.TagsSelectFragment
|
||||
import com.gh.gamecenter.ask.questionsedit.pic.QuestionsEditPicAdapter
|
||||
@ -43,7 +39,7 @@ class QuestionsEditActivity : NormalActivity() {
|
||||
|
||||
private var mViewModel: QuestionsEditViewModel? = null
|
||||
|
||||
val mTitle by bindView<EditText>(R.id.questionsedit_title)
|
||||
val mTitle by bindView<AutoCompleteTextView>(R.id.questionsedit_title)
|
||||
val mContent by bindView<EditText>(R.id.questionsedit_content)
|
||||
val mPicRv by bindView<RecyclerView>(R.id.suggest_pic_rv)
|
||||
|
||||
@ -56,9 +52,6 @@ class QuestionsEditActivity : NormalActivity() {
|
||||
|
||||
private var mSearchKey: String? = null
|
||||
|
||||
private var listPopupWindow: ListPopupWindow? = null
|
||||
private var mTitleTipAdapter: QuestionTitleTipAdapter? = null
|
||||
|
||||
companion object {
|
||||
// searchKey 补充到标题(新增问题)
|
||||
fun getIntent(context: Context, searchKey: String?): Intent {
|
||||
@ -81,16 +74,6 @@ class QuestionsEditActivity : NormalActivity() {
|
||||
|
||||
}
|
||||
|
||||
override fun handleMessage(msg: Message?) {
|
||||
if (msg?.what == 1) {
|
||||
val s = msg.obj as String?
|
||||
if (!s.isNullOrEmpty()) {
|
||||
mViewModel?.getTitleTipData(s!!)
|
||||
} else if (listPopupWindow?.isShowing!!) {
|
||||
listPopupWindow?.dismiss()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun getLayoutId(): Int {
|
||||
return R.layout.activity_questions_edit
|
||||
@ -116,7 +99,7 @@ class QuestionsEditActivity : NormalActivity() {
|
||||
if (file.length() > 8 * 1024 * 1024) {
|
||||
toast(R.string.suggestion_pic_hint)
|
||||
} else {
|
||||
var picList = mViewModel?.mQuestionsPic?.value
|
||||
val picList = mViewModel?.mQuestionsPic?.value
|
||||
if (picList != null && picList.size >= 3) {
|
||||
toast(R.string.questions_edit_maxpic_hint)
|
||||
} else {
|
||||
@ -157,16 +140,6 @@ class QuestionsEditActivity : NormalActivity() {
|
||||
if (it != null) mPicAdapter?.notifyPicList(it)
|
||||
})
|
||||
|
||||
mViewModel?.mTitleTipData?.observe(this, Observer { it ->
|
||||
if (it != null && it.size > 0) {
|
||||
mTitleTipAdapter?.setListData(it)
|
||||
listPopupWindow?.show()
|
||||
setPopuListScroll()
|
||||
} else {
|
||||
listPopupWindow?.dismiss()
|
||||
}
|
||||
})
|
||||
|
||||
initView()
|
||||
}
|
||||
|
||||
@ -186,7 +159,6 @@ class QuestionsEditActivity : NormalActivity() {
|
||||
if ("?" != endString && "?" != endString)
|
||||
title += "?"
|
||||
}
|
||||
|
||||
mViewModel?.mQuestionTitle = title.trim()
|
||||
mViewModel?.mQuestionContent = mContent.text.toString()
|
||||
mViewModel?.mSelectTags?.clear()
|
||||
@ -217,42 +189,17 @@ class QuestionsEditActivity : NormalActivity() {
|
||||
}
|
||||
}
|
||||
|
||||
val titleTipAdapter = QuestionTitleTipAdapter(this, mTitle)
|
||||
mTitle.setAdapter(titleTipAdapter)
|
||||
mTitle.addTextChangedListener(LimitTextWatcher(mTitle))
|
||||
mContent.addTextChangedListener(LimitTextWatcher(mContent))
|
||||
mPicAdapter = QuestionsEditPicAdapter(this, mViewModel!!)
|
||||
mPicRv.layoutManager = object : GridLayoutManager(this, 5) {
|
||||
override fun canScrollVertically(): Boolean {
|
||||
return false
|
||||
}
|
||||
}
|
||||
mPicAdapter = QuestionsEditPicAdapter(this, mViewModel!!)
|
||||
mPicRv.adapter = mPicAdapter
|
||||
mTitleTipAdapter = QuestionTitleTipAdapter(applicationContext)
|
||||
listPopupWindow = ListPopupWindow(this)
|
||||
listPopupWindow?.anchorView = mTitle
|
||||
listPopupWindow?.setAdapter(mTitleTipAdapter)
|
||||
listPopupWindow?.height = DisplayUtils.dip2px(this, 200F)
|
||||
listPopupWindow?.setOnItemClickListener({ _, _, position, _ ->
|
||||
val entity = mTitleTipAdapter?.getItem(position) as QuestionsIndexEntity
|
||||
val tracers = mEntrance + "+(标题自动搜索)"
|
||||
startActivity(QuestionsDetailActivity.getIntent(this, entity.id, tracers))
|
||||
})
|
||||
}
|
||||
|
||||
private fun setPopuListScroll() {
|
||||
var totalItemCount = 0
|
||||
var lastVisibleItem = 0
|
||||
listPopupWindow?.listView!!.setOnScrollListener(object : AbsListView.OnScrollListener {
|
||||
override fun onScrollStateChanged(view: AbsListView, scrollState: Int) {
|
||||
if (totalItemCount == lastVisibleItem && scrollState == SCROLL_STATE_IDLE) {
|
||||
mViewModel?.getTitleTipData(mTitle.text.toString())
|
||||
}
|
||||
}
|
||||
|
||||
override fun onScroll(view: AbsListView, firstVisibleItem: Int, visibleItemCount: Int, totalCount: Int) {
|
||||
lastVisibleItem = firstVisibleItem + visibleItemCount
|
||||
totalItemCount = totalCount
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
private inner class LimitTextWatcher(private val mEditText: EditText) : TextWatcher {
|
||||
@ -272,12 +219,6 @@ class QuestionsEditActivity : NormalActivity() {
|
||||
mTitle.setSelection(mTitle.text!!.length)
|
||||
toast("标题最多50个字")
|
||||
}
|
||||
|
||||
var message = Message()
|
||||
message.obj = mTitle.text.toString()
|
||||
message.what = 1
|
||||
mBaseHandler.removeMessages(1)
|
||||
mBaseHandler.sendMessageDelayed(message, 500)
|
||||
} else if (mEditText === mContent) {
|
||||
if (s.length > QUESTION_CONTENT_MAX_LENGTH) {
|
||||
mContent.setText("")
|
||||
@ -293,6 +234,15 @@ class QuestionsEditActivity : NormalActivity() {
|
||||
}
|
||||
|
||||
override fun onBackPressed() {
|
||||
val imgList = mViewModel?.mQuestionsPic?.value
|
||||
val title = mTitle.text.toString().trim { it <= ' ' }
|
||||
val content = mContent.text.toString().trim { it <= ' ' }
|
||||
if (imgList != null && imgList.size > 0 || title.isNotEmpty() || content.isNotEmpty()) {
|
||||
DialogUtils.showCancelAlertDialog(this, "提示"
|
||||
, if (mViewModel?.mQuestionEntity == null) "确定放弃提问吗?" else "确定放弃编辑吗?"
|
||||
, "再想想", " 放弃", null) { finish() }
|
||||
return
|
||||
}
|
||||
finish()
|
||||
}
|
||||
}
|
||||
@ -5,15 +5,12 @@ import android.arch.lifecycle.AndroidViewModel
|
||||
import android.arch.lifecycle.LiveData
|
||||
import android.arch.lifecycle.MediatorLiveData
|
||||
import android.arch.lifecycle.Transformations
|
||||
import anet.channel.util.Utils.context
|
||||
import com.gh.base.fragment.WaitingDialogFragment
|
||||
import com.gh.common.util.GsonUtils
|
||||
import com.gh.common.util.ImageUtils
|
||||
import com.gh.common.util.UrlFilterUtils
|
||||
import com.gh.gamecenter.R
|
||||
import com.gh.gamecenter.ask.entity.QuestionsDetailEntity
|
||||
import com.gh.gamecenter.ask.entity.QuestionsIndexEntity
|
||||
import com.gh.gamecenter.manager.UserManager
|
||||
import com.gh.gamecenter.mvvm.NetworkBoundResource
|
||||
import com.gh.gamecenter.mvvm.Resource
|
||||
import com.gh.gamecenter.mvvm.Status
|
||||
@ -25,7 +22,6 @@ import okhttp3.MediaType
|
||||
import okhttp3.RequestBody
|
||||
import okhttp3.ResponseBody
|
||||
import org.json.JSONObject
|
||||
import retrofit2.HttpException
|
||||
import rx.android.schedulers.AndroidSchedulers
|
||||
import rx.schedulers.Schedulers
|
||||
import java.net.HttpURLConnection
|
||||
@ -44,7 +40,6 @@ class QuestionsEditViewModel : AndroidViewModel {
|
||||
private var mCommunityId: String? = null
|
||||
|
||||
val mProcessDialog = MediatorLiveData<WaitingDialogFragment.WaitingDialogData>()
|
||||
val mTitleTipData = MediatorLiveData<MutableList<QuestionsIndexEntity>>()
|
||||
|
||||
val mTitleTagsLD: LiveData<Resource<List<String>>>
|
||||
|
||||
@ -73,41 +68,6 @@ class QuestionsEditViewModel : AndroidViewModel {
|
||||
})
|
||||
}
|
||||
|
||||
var mLastSearchKey: String? = null
|
||||
var mListPage: Int = 1
|
||||
var isLoading: Boolean = false
|
||||
|
||||
fun getTitleTipData(key: String) {
|
||||
if (isLoading) return
|
||||
isLoading = true
|
||||
var list: MutableList<QuestionsIndexEntity>?
|
||||
if (!mLastSearchKey.isNullOrEmpty() && mLastSearchKey == key) {
|
||||
list = if (mTitleTipData.value != null) mTitleTipData.value else ArrayList()
|
||||
} else {
|
||||
mListPage = 1
|
||||
list = ArrayList()
|
||||
}
|
||||
RetrofitManager
|
||||
.getInstance(context).api
|
||||
.getAskSearchByTitle(UserManager.getInstance().getCommunityId(context),
|
||||
UrlFilterUtils.getFilterQuery("keyword", key), mListPage)
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(object : Response<List<QuestionsIndexEntity>>() {
|
||||
override fun onResponse(response: List<QuestionsIndexEntity>?) {
|
||||
list?.addAll(response!!)
|
||||
mTitleTipData.postValue(list)
|
||||
mLastSearchKey = key
|
||||
mListPage++
|
||||
isLoading = false
|
||||
}
|
||||
|
||||
override fun onFailure(e: HttpException?) {
|
||||
isLoading = false
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fun getDefaultTag(communityId: String?) {
|
||||
mCommunityId = if (mQuestionEntity != null) mQuestionEntity?.communityId else communityId
|
||||
mApiService
|
||||
|
||||
@ -1,20 +1,29 @@
|
||||
package com.gh.gamecenter.ask.questionsedit.tip
|
||||
|
||||
import android.content.Context
|
||||
import android.text.Html
|
||||
import android.text.TextUtils
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.BaseAdapter
|
||||
import android.widget.TextView
|
||||
import android.widget.*
|
||||
import com.gh.common.util.DisplayUtils
|
||||
import com.gh.common.util.UrlFilterUtils
|
||||
import com.gh.gamecenter.QuestionsDetailActivity
|
||||
import com.gh.gamecenter.R
|
||||
import com.gh.gamecenter.ask.entity.QuestionsIndexEntity
|
||||
import com.gh.gamecenter.manager.UserManager
|
||||
import com.gh.gamecenter.retrofit.Response
|
||||
import com.gh.gamecenter.retrofit.RetrofitManager
|
||||
|
||||
/**
|
||||
* Created by khy on 3/05/18.
|
||||
*/
|
||||
class QuestionTitleTipAdapter(private val context: Context) : BaseAdapter() {
|
||||
class QuestionTitleTipAdapter(private val context: Context, title: AutoCompleteTextView) : BaseAdapter(), Filterable {
|
||||
|
||||
var questionList: MutableList<QuestionsIndexEntity> = arrayListOf()
|
||||
var questionList: MutableList<QuestionsIndexEntity> = ArrayList()
|
||||
var mTitleView = title
|
||||
var mSearchKey: String? = null
|
||||
|
||||
override fun getView(pos: Int, convertView: View?, parent: ViewGroup?): View {
|
||||
val view: View
|
||||
@ -25,12 +34,24 @@ class QuestionTitleTipAdapter(private val context: Context) : BaseAdapter() {
|
||||
view = convertView.tag as View
|
||||
}
|
||||
val entity = questionList[pos]
|
||||
view.findViewById<TextView>(R.id.questions_edit_index_title).text = entity.title
|
||||
view.findViewById<TextView>(R.id.questions_edit_index_title).text = Html.fromHtml(replayKeyword(entity.title))
|
||||
view.findViewById<TextView>(R.id.questions_edit_index_answer_count).text = context.getString(R.string.ask_answer_count, entity.answerCount)
|
||||
view.setOnClickListener({
|
||||
val tracers = "(问题编辑)+(标题自动搜索)"
|
||||
context.startActivity(QuestionsDetailActivity.getIntent(context, entity.id, tracers))
|
||||
})
|
||||
return view
|
||||
|
||||
}
|
||||
|
||||
private fun replayKeyword(title: String): String {
|
||||
return if (TextUtils.isEmpty(mSearchKey)) title else title.replace(mSearchKey?.toRegex()!!, getHtmlKeyword())
|
||||
}
|
||||
|
||||
private fun getHtmlKeyword(): String {
|
||||
return "<font color=\"#ff4147\">$mSearchKey</font>"
|
||||
}
|
||||
|
||||
override fun getItem(pos: Int): Any {
|
||||
return questionList[pos]
|
||||
}
|
||||
@ -43,8 +64,41 @@ class QuestionTitleTipAdapter(private val context: Context) : BaseAdapter() {
|
||||
return questionList.size
|
||||
}
|
||||
|
||||
fun setListData(list: MutableList<QuestionsIndexEntity>) {
|
||||
questionList = list
|
||||
notifyDataSetChanged()
|
||||
override fun getFilter(): Filter {
|
||||
return QuestionTitleFilter()
|
||||
}
|
||||
|
||||
inner class QuestionTitleFilter : Filter() {
|
||||
|
||||
override fun performFiltering(prefix: CharSequence?): FilterResults {
|
||||
val results = FilterResults()
|
||||
if (prefix.isNullOrBlank()) return results
|
||||
RetrofitManager
|
||||
.getInstance(context).api
|
||||
.getAskSearchByTitle(UserManager.getInstance().getCommunityId(context), UrlFilterUtils.getFilterQuery("keyword", prefix.toString()), 1)
|
||||
.subscribe(object : Response<List<QuestionsIndexEntity>>() {
|
||||
override fun onResponse(response: List<QuestionsIndexEntity>?) {
|
||||
results.values = response
|
||||
results.count = response!!.size
|
||||
mSearchKey = prefix.toString()
|
||||
}
|
||||
})
|
||||
return results
|
||||
}
|
||||
|
||||
override fun publishResults(constant: CharSequence?, results: FilterResults?) {
|
||||
if (results != null && results.count > 0) {
|
||||
questionList = results.values as ArrayList<QuestionsIndexEntity>
|
||||
} else {
|
||||
questionList.clear()
|
||||
}
|
||||
if (questionList.size > 2) {
|
||||
mTitleView.dropDownHeight = DisplayUtils.dip2px(context, 200F)
|
||||
} else {
|
||||
mTitleView.dropDownHeight = ViewGroup.LayoutParams.WRAP_CONTENT;
|
||||
}
|
||||
notifyDataSetChanged()
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,44 +0,0 @@
|
||||
package com.gh.gamecenter.ask.questionsedit.tip
|
||||
|
||||
import android.content.Context
|
||||
import android.graphics.Rect
|
||||
import android.support.v7.widget.AppCompatAutoCompleteTextView
|
||||
import android.util.AttributeSet
|
||||
import android.view.KeyEvent
|
||||
import android.view.inputmethod.CompletionInfo
|
||||
|
||||
|
||||
/**
|
||||
* Created by khy on 3/05/18.
|
||||
*/
|
||||
class QuestionTitleTipEditView : AppCompatAutoCompleteTextView {
|
||||
|
||||
constructor(context: Context) : super(context) {}
|
||||
|
||||
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {}
|
||||
|
||||
override fun onCommitCompletion(completion: CompletionInfo) {
|
||||
super.onCommitCompletion(completion)
|
||||
}
|
||||
|
||||
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
|
||||
}
|
||||
|
||||
override fun onKeyPreIme(keyCode: Int, event: KeyEvent): Boolean {
|
||||
if (KeyEvent.KEYCODE_BACK === keyCode) super.dismissDropDown()
|
||||
return super.onKeyPreIme(keyCode, event)
|
||||
}
|
||||
|
||||
override fun replaceText(text: CharSequence) {
|
||||
}
|
||||
|
||||
protected override fun onFocusChanged(focused: Boolean, direction: Int, previouslyFocusedRect: Rect?) {
|
||||
super.onFocusChanged(focused, direction, previouslyFocusedRect)
|
||||
super.dismissDropDown()
|
||||
}
|
||||
|
||||
override fun dismissDropDown() {
|
||||
if (text.toString().length == 0) super.dismissDropDown()
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,25 +0,0 @@
|
||||
package com.gh.gamecenter.ask.questionsedit.tip;
|
||||
|
||||
import android.content.Context;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.util.AttributeSet;
|
||||
import android.widget.ListPopupWindow;
|
||||
|
||||
import com.gh.common.util.DisplayUtils;
|
||||
|
||||
/**
|
||||
* Created by khy on 5/05/18.
|
||||
*/
|
||||
|
||||
public class QuestionTitleTipPopuListView extends ListPopupWindow {
|
||||
|
||||
public QuestionTitleTipPopuListView(@NonNull Context context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
public QuestionTitleTipPopuListView(@NonNull Context context, @Nullable AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
setHeight(DisplayUtils.dip2px(context, 200));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user