diff --git a/app/src/main/java/com/gh/gamecenter/QuestionsEditActivity.kt b/app/src/main/java/com/gh/gamecenter/QuestionsEditActivity.kt index 96f2dbfe4b..59c9459e73 100644 --- a/app/src/main/java/com/gh/gamecenter/QuestionsEditActivity.kt +++ b/app/src/main/java/com/gh/gamecenter/QuestionsEditActivity.kt @@ -6,6 +6,7 @@ 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 @@ -13,17 +14,21 @@ 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.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.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 import com.gh.gamecenter.ask.questionsedit.tip.QuestionTitleTipAdapter -import com.gh.gamecenter.ask.questionsedit.tip.QuestionTitleTipEditView import com.gh.gamecenter.manager.UserManager import com.gh.gamecenter.mvvm.Resource import com.gh.gamecenter.mvvm.Status @@ -38,7 +43,7 @@ class QuestionsEditActivity : NormalActivity() { private var mViewModel: QuestionsEditViewModel? = null - val mTitle by bindView(R.id.questionsedit_title) + val mTitle by bindView(R.id.questionsedit_title) val mContent by bindView(R.id.questionsedit_content) val mPicRv by bindView(R.id.suggest_pic_rv) @@ -51,6 +56,9 @@ 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 { @@ -73,6 +81,17 @@ 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 } @@ -138,6 +157,16 @@ 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() } @@ -188,8 +217,6 @@ class QuestionsEditActivity : NormalActivity() { } } - val questionTitleAutoCompleteAdapter = QuestionTitleTipAdapter(applicationContext) - mTitle.setAdapter(questionTitleAutoCompleteAdapter) mTitle.addTextChangedListener(LimitTextWatcher(mTitle)) mContent.addTextChangedListener(LimitTextWatcher(mContent)) mPicRv.layoutManager = object : GridLayoutManager(this, 5) { @@ -199,6 +226,33 @@ class QuestionsEditActivity : NormalActivity() { } 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 { @@ -218,6 +272,12 @@ 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("") @@ -228,6 +288,7 @@ class QuestionsEditActivity : NormalActivity() { } } } + override fun afterTextChanged(s: Editable) {} } diff --git a/app/src/main/java/com/gh/gamecenter/ask/questionsedit/QuestionsEditViewModel.kt b/app/src/main/java/com/gh/gamecenter/ask/questionsedit/QuestionsEditViewModel.kt index a5d01ee987..f09350df11 100644 --- a/app/src/main/java/com/gh/gamecenter/ask/questionsedit/QuestionsEditViewModel.kt +++ b/app/src/main/java/com/gh/gamecenter/ask/questionsedit/QuestionsEditViewModel.kt @@ -5,12 +5,15 @@ 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 @@ -22,6 +25,7 @@ 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 @@ -40,6 +44,7 @@ class QuestionsEditViewModel : AndroidViewModel { private var mCommunityId: String? = null val mProcessDialog = MediatorLiveData() + val mTitleTipData = MediatorLiveData>() val mTitleTagsLD: LiveData>> @@ -68,6 +73,41 @@ 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? + 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>() { + override fun onResponse(response: List?) { + 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 diff --git a/app/src/main/java/com/gh/gamecenter/ask/questionsedit/TagsSelectFragment.kt b/app/src/main/java/com/gh/gamecenter/ask/questionsedit/TagsSelectFragment.kt index 5c0aa59e11..c2c81a8f86 100644 --- a/app/src/main/java/com/gh/gamecenter/ask/questionsedit/TagsSelectFragment.kt +++ b/app/src/main/java/com/gh/gamecenter/ask/questionsedit/TagsSelectFragment.kt @@ -15,6 +15,7 @@ import android.widget.EditText import android.widget.TextView import com.gh.base.fragment.BaseDialogWrapperFragment import com.gh.base.fragment.BaseFragment +import com.gh.base.fragment.WaitingDialogFragment import com.gh.common.util.AskErrorResponseUtils import com.gh.common.util.DisplayUtils import com.gh.common.util.LoginUtils @@ -78,6 +79,7 @@ class TagsSelectFragment : BaseFragment() { mViewModel?.postQuestion()?.observe(this, Observer { it -> when (it?.status) { Status.SUCCESS -> { + mViewModel?.mProcessDialog?.postValue(WaitingDialogFragment.WaitingDialogData("提交中...", false)) toast("提交成功") if (mViewModel?.mQuestionEntity != null) { // 修改问题 val data = Intent() @@ -90,11 +92,15 @@ class TagsSelectFragment : BaseFragment() { activity?.finish() } Status.ERROR -> { + mViewModel?.mProcessDialog?.postValue(WaitingDialogFragment.WaitingDialogData("提交中...", false)) if (!LoginUtils.userPostErrorToast(it.exception, context, false)) { toast("提交失败") AskErrorResponseUtils.errorResponseControl(context, it.exception) } } + Status.LOADING -> { + mViewModel?.mProcessDialog?.postValue(WaitingDialogFragment.WaitingDialogData("提交中...", true)) + } } }) } diff --git a/app/src/main/java/com/gh/gamecenter/ask/questionsedit/tip/QuestionTitleTipAdapter.kt b/app/src/main/java/com/gh/gamecenter/ask/questionsedit/tip/QuestionTitleTipAdapter.kt index f82f043f13..9e7766e0c6 100644 --- a/app/src/main/java/com/gh/gamecenter/ask/questionsedit/tip/QuestionTitleTipAdapter.kt +++ b/app/src/main/java/com/gh/gamecenter/ask/questionsedit/tip/QuestionTitleTipAdapter.kt @@ -1,40 +1,30 @@ package com.gh.gamecenter.ask.questionsedit.tip import android.content.Context -import android.content.Intent import android.view.LayoutInflater import android.view.View import android.view.ViewGroup -import android.widget.* -import com.gh.common.util.UrlFilterUtils -import com.gh.gamecenter.QuestionsDetailActivity +import android.widget.BaseAdapter +import android.widget.TextView 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 -import com.google.gson.Gson /** * Created by khy on 3/05/18. */ -class QuestionTitleTipAdapter(private val context: Context) : BaseAdapter(), Filterable { +class QuestionTitleTipAdapter(private val context: Context) : BaseAdapter() { var questionList: MutableList = arrayListOf() - val onItemClickListener = OnItemClickListener() override fun getView(pos: Int, convertView: View?, parent: ViewGroup?): View { - val view: View - if (convertView == null) { view = LayoutInflater.from(context).inflate(R.layout.question_edit_title_search_item, convertView, false) view.tag = view } else { - view = convertView + view = convertView.tag as View } - - var entity = questionList[pos] + val entity = questionList[pos] view.findViewById(R.id.questions_edit_index_title).text = entity.title view.findViewById(R.id.questions_edit_index_answer_count).text = context.getString(R.string.ask_answer_count, entity.answerCount) return view @@ -53,52 +43,8 @@ class QuestionTitleTipAdapter(private val context: Context) : BaseAdapter(), Fil return questionList.size } - override fun getFilter(): Filter { - return QuestionTitleFilter() + fun setListData(list: MutableList) { + questionList = list + notifyDataSetChanged() } - - inner class QuestionTitleFilter : Filter() { - - override fun performFiltering(prefix: CharSequence?): FilterResults { - -// Log.i("!!!", prefix.toString()) - - 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>() { - override fun onResponse(response: List?) { - results.values = response - results.count = response!!.size - } - }) - return results - - } - - override fun publishResults(constant: CharSequence?, results: FilterResults?) { - - if (results != null && results.count > 0) { - questionList = results.values as ArrayList -// Log.i("!!!", "r:" + questionList.count()) - notifyDataSetChanged() - } - - } - - } - - inner class OnItemClickListener : AdapterView.OnItemClickListener { - - override fun onItemClick(p0: AdapterView<*>?, p1: View?, pos: Int, p3: Long) { - val intent = Intent(context, QuestionsDetailActivity::class.java) - intent.putExtra("question", Gson().toJson(questionList[pos])) - context.startActivity(intent) - } - - } - } diff --git a/app/src/main/java/com/gh/gamecenter/ask/questionsedit/tip/QuestionTitleTipPopuListView.java b/app/src/main/java/com/gh/gamecenter/ask/questionsedit/tip/QuestionTitleTipPopuListView.java new file mode 100644 index 0000000000..ca34d32926 --- /dev/null +++ b/app/src/main/java/com/gh/gamecenter/ask/questionsedit/tip/QuestionTitleTipPopuListView.java @@ -0,0 +1,25 @@ +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)); + } +}