Android Search results display highlighting (with data sliding bottom auto refresh)

Source: Internet
Author: User

First of all

Search results (Here I just simulate the data, real and when the server goes, the data returned should contain the keywords)



Simulation of an interface without a search result

Specific implementation

In this in a sentence, is to do one thing, split into a number of summary, not in the development of the time to touch the brain and can also control the progress of development.

The idea is actually very simple, we listen to the changes in the input box, and then after the text changes to request the server, and then take the results we need to display the data.

    • The first step: the monitoring of the search box
Et_search.addtextchangedlistener (NewTextwatcher () {@Override             Public void beforetextchanged(Charsequence S,intStartintCountintAfter) {}/** * Callback This method when the text in the search box changes * @param charsequence The text of the input box * @param  start starts * @param before * @param count words */            @Override             Public void ontextchanged(Charsequence charsequence,intStartintBefore,intCount) {//Make a logical request here}@Override             Public void aftertextchanged(Editable s) {            }        });
    • Step two: Make related logical requests
 if(! Textutils.isempty (charsequence) && charsequence.length () >3) {//Here are 3 only to simulate the requestMkey = Charsequence.tostring ();                    InitData (Charsequence.tostring ());                Changestates (state); }Else{state = no_tthing;                Changestates (state); }/** * Get data for the first time * * @param Key Highlight */    Private void InitData(String key) {//Here is the actual network request is to go to the network to obtain dataString result = Jsonutils.getjson ( This,"Search.json"); Gson Gson =NewGson (); Searchbean Searchbean = Gson.fromjson (result, searchbean.class);if(Searchbean! =NULL) {Mdatabeen = Searchbean.getdata ();if(Mdatabeen! =NULL&& mdatabeen.size () >0) {state = Show_data;            Msearchadapter.loaddata (Mdatabeen, key); }Else{state = no_tthing; }        }Else{state = no_tthing; }    }/** * Change Search Status * * @param State Search Key value */    Private void changestates(intState) {Switch(state) { CaseNO_TTHING:mNoLayout.setVisibility (view.visible); Recycler_view.setvisibility (view.invisible); Break; CaseSHOW_DATA:mNoLayout.setVisibility (View.gone); Recycler_view.setvisibility (view.visible); Break; }    }
    • Step three: Make discoloration
/** * @param Context context * @param wholestr All text * @param highlightstr Change the color of the text * @param color * /     Public Stringformatutil(context context, string wholestr, String highlightstr,intColor) { This. Mcontext = Context; This. wholestr = Wholestr; This. highlightstr = Highlightstr; This. color = color; }/** * Fill Color * * @return stringformatutil * *     PublicStringformatutilFillColor() {if(! Textutils.isempty (WHOLESTR) &&! Textutils.isempty (HIGHLIGHTSTR)) {Spbuilder =NewSpannablestringbuilder (WHOLESTR);//matching rulesPattern p = pattern.compile (HIGHLIGHTSTR);//Match fieldMatcher m = P.matcher (Spbuilder);//Coloringcolor = Mcontext.getresources (). GetColor (color);//Start a loop to find if there is a keyword in it so that multiple keywords appear in a sentence will be highlighted             while(M.find ()) {intStart = M.start ();intEnd = M.end (); Spbuilder.setspan (NewForegroundcolorspan (color), start, end, spanned.span_exclusive_exclusive); }return  This; }return NULL; }/** * Gets the results that have been changed (this time has been highlighted, do not get this result when the ToString () or the color will be removed) * * @return result */     PublicSpannablestringbuilderGetResult() {if(Spbuilder! =NULL) {returnSpbuilder; }return NULL; }//To use the tool class, that is, when assigning a value to the title    //This is the use of adapter inside the rulesMformatutil =NewStringformatutil (Holder.itemView.getContext (), Databean.gettitle (), Mlightstr, r.color.coloraccent). FillColor (); Holder.tv_title.setText (Mformatutil.getresult ());
Demo description

The local JSON here is my own definition, and in the search for the time to add their own logic, if the actual project needs to be in accordance with their own needs to make changes. Related display do not need to care, here is just to give you a search and keyword highlighting a way of thinking.

Demo Code Portal
    • Https://github.com/wuyinlei/SearchViewDemo
Reference articles
    • Android TextView to achieve keyword highlighting

Android Search results display highlighting (with data sliding bottom auto refresh)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.