片段中的yalantis搜索过滤器进入后台

时间:2017-06-20 01:28:02

标签: android-fragments android-recyclerview searchfiltercollection

我正在使用 Yalantis过滤器来安装android。我有一个 Fragment 而不是活动:  `class IncentiveActivity:Fragment(),FilterListener {

private var incentiveList = ArrayList<Incentive>()
var mAdapter: IncentiveListRecyclerAdapter? = null
val handler: Handler  by lazy {
    Handler()
}
private var mColors: IntArray? = null
private var mTitles: Array<String>? = null
private var mAllQuestions: List<Incentive>? = null
private var mFilter: Filter<FilterTag>? = null
val that :Context? = null
var fragment : View? =null


override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    fragment = inflater!!.inflate(R.layout.activity_incentive_detail, container, false)
    return fragment
}

override fun onStart() {
    super.onStart()
    setIncentiveAdapter()

    mColors = getResources().getIntArray(R.array.colorslist);
    mTitles = getResources().getStringArray(R.array.job_titles);

    mFilter =  this.filter as Filter<FilterTag>

    mFilter!!.adapter =Adapter(getTags())
    mFilter!!.listener = this
    //the text to show when there's no selected items
    mFilter!!.noSelectedItemText = getString(R.string.str_all_selected)
    mFilter!!.build()

}

private fun getTags():List<FilterTag> {
    val tags = ArrayList<FilterTag>()
    for (i in 0..mTitles!!.size - 1) {
        tags.add(FilterTag(mTitles!![i], mColors!![i]))
    }
    return tags
}

fun setIncentiveAdapter(){
    //todo for salafi API call here and set adapter
    val in1  = Incentive("Get 100p by completing 10","Description","D","Cash","100","ACTIVE",10,"id-vcxsdt","ACTIVE",8,6,9)
    val in2  = Incentive("Get 100p by completing 10","Description","W","Cash","100","OPTED",10,"id-vcxsdt","OPTED",8,6,9)
    val in3  = Incentive("Get 100p by completing 10","Description","Y","Cash","100","EXPIRED",10,"id-vcxsdt","EXPIRED",8,6,9)
    val in4  = Incentive("Get 100p by completing 10","Description","Y","Cash","100","ACHIEVED",10,"id-vcxsdt","EXPIRED",8,6,9)
    val in5  = Incentive("Get 100p by completing 10","Description","Y","Cash","100","NOT_OPTED",10,"id-vcxsdt","NOT_OPTED",8,6,9)

    incentiveList.add(in1)
    incentiveList.add(in2)
    incentiveList.add(in3)
    incentiveList.add(in4)
    incentiveList.add(in5)

    mAdapter = IncentiveListRecyclerAdapter(context, incentiveList)
    incentiveListView.adapter = mAdapter
    incentiveListView.layoutManager = LinearLayoutManager(context)
}


private fun calculateDiff(oldList: List<Incentive>, newList: List<Incentive >) {
    DiffUtil.calculateDiff(object : DiffUtil.Callback() {
        override fun getOldListSize(): Int {
            return oldList.size
        }

        override fun getNewListSize(): Int {
            return newList.size
        }

        override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
            return oldList[oldItemPosition].equals(newList[newItemPosition])
        }

        override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
            return oldList[oldItemPosition].equals(newList[newItemPosition])
        }
    }).dispatchUpdatesTo(mAdapter)
}

override fun onFilterDeselected(item: FilterTag) {

}

override fun onFilterSelected(item: FilterTag) {
    Log.e("Item",item.getText())
    if (item.getText().equals(mTitles!![0])) {
        Log.e("mtitle",mTitles!![0])
        mFilter!!.deselectAll();
        mFilter!!.collapse();

    }
}

/*private fun findByTags(tags: List<FilterTag>): List<Incentive> {
    val questions = ArrayList<Incentive>()

    for (question in mAllQuestions!!) {
        for (tag in tags) {
            if (question.hasTag(tag.getText()) && !questions.contains(question)) {
                questions.add(question)
            }
        }
    }

    return questions
}*/

override fun onFiltersSelected(filters: java.util.ArrayList<FilterTag>) {
    Log.e("filter",filters.size.toString())
    /*val newQuestions = findByTags(filters)
    val oldQuestions = mAdapter.getQuestions()
    mAdapter.setQuestions(newQuestions)
    calculateDiff(oldQuestions, newQuestions)*/
}

override fun onNothingSelected() {
    if (this.incentiveListView != null) {
        mFilter!!.adapter =Adapter(getTags())
        incentiveListView.adapter = mAdapter
    }
} 

inner class Adapter(@NotNull items: List<FilterTag>) : FilterAdapter<FilterTag>(items) {
    override fun createView(position: Int, item: FilterTag): FilterItem {
        val filterItem = FilterItem(this@IncentiveActivity.activity)
        filterItem.strokeColor = mColors!![0]
        filterItem.textColor = mColors!![0]
        filterItem.cornerRadius = 14f
        filterItem.checkedTextColor = ContextCompat.getColor(this@IncentiveActivity.activity, android.R.color.white)
        filterItem.color = ContextCompat.getColor(this@IncentiveActivity.activity, R.color.m_color2)
        filterItem.checkedColor = mColors!![position]
        filterItem.text = item.getText()
        filterItem.deselect()


        return filterItem
    }


}

}` 当我展开过滤器时,我遇到了问题,它出现在我的列表背景中,保持我的列表可见。

this is my listView when i first open the fragment this is the listview after i select the filter

我很抱歉,如果我没有提供所有细节,我的问题没有遵循SOF条件,谢谢

0 个答案:

没有答案