The progress bar (ProgressBar)

In Android, the ProgressBar is used to indicate the percentage of a time-consuming operation that has been completed

Lateral style

Circular style

XML configuration: “?” Represents an uncertain option

<ProgressBar style="? android:attr/progressBarStyleHorizontal" android:layout_width="fill_parent" android:layout_height="50dp" android:visibility="gone"/>Copy the code

Commonly used attributes

Android: Max Sets the maximum value of the progress bar. Android :progress Specifies the completed progress value of the progress bar. Android :progressDrawable sets the drawing mode of the progress barCopy the code

Style property

? Android: attr/progressBarStyleHorizontal thin horizontal strip progress bar? Android: attr/progressBarStyleLarge large circular progress bar? Android: attr/progressBarStyleSmall small circular progress bar @ android: style/Widget. The ProgressBar. Large a big leap, Rotating images of the progress bar @ android: style/Widget. The ProgressBar. Small Small jump, rotating the picture of the progress bar @ android: style/Widget. The ProgressBar. Horizontal thick Horizontal strip progress barCopy the code

Public synchronized void setProgress(int progress) ProgressBar (ProgressBar, ProgressBar, ProgressBar) Others, such as the Request Network, use a circular progress bar without knowing the operation time. The ProgressBar prompts the user in the foreground, while the task is usually performed in the background. However, Android does not allow background threads to directly manipulate the UI, so we usually need to notify the main thread when we need to process the UI. In an Activity, we can use runOnUiThread to handle the main thread

Child threads manipulate the UI

runOnUiThread(new Runnable(){ @Override public void run(){ progressBar.setProgress(index); }});Copy the code

The first Thread + Handler

If you need to update the UI in a child thread, you can also use a Handler to send a message to the main thread in the child thread and let the main thread refresh it

new Thread(){ @Override public void run(){ super.run(); Message m = new Message(); m.whit=1; m.obj = progress; } }.start(); // The main thread receives the message, Private Handler mHandler = new Handler() {@override public void handleMessage(Message MSG) { super.handleMessage(msg); If (msg.what==1){int progress = (Integer)msg.obj; mProgressBar.setProgress(progress); }}};Copy the code

Article drag the SeekBar

The drag bar is similar to the progress bar, except that the drag bar allows the user to change the value by dragging the slider. It is usually used to adjust a value, such as the transparency or volume of a picture

Set in XML:

<SeekBar
	android:id="@+id/seekBar1"
	android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:max="100"
    android:padding="10px"
    android:progress="50" />
Copy the code

Common attributes:

Android: Max ="100" maximum size of the progress bar Android :padding="10px" Margin of the progress bar Android :progress="50" initial value of the progress barCopy the code

Listening event:

/ / listen to drag the values change event seekBar. SetOnSeekBarChangeListener (new OnSeekBarChangeListener () {@ Override public void @override public void onStarTrachingTouch(SeekBar SeekBar){ } @override public void onProgressChanged(SeekBar SeekBar, int progress, Boolean fromUser){// When progress changes //progress indicates the changed progress value // and fromUser indicates whether the user has changed the progress value. }});Copy the code

RatingBar

Star rating bar drag bar similar, both allow users to drag to change progress,

The difference is that the star rating bar shows progress through the stars. Usually.

The star rating bar is used to indicate the support degree of a certain affair or the satisfaction degree of a certain service, such as Taobao

To the seller’s favorable rating

The XML configuration:

<RatingBar android:id="@+id/myRatingBar" Android :layout_width="match_parent" Android :numStars="5" Android :rating="2.5" Android: isIndicator = "true" android: stepSize = "0.5" android: layout_height = "wrap_content" / >Copy the code

Commonly used attributes

Android :numStars="5" android:isIndicator="true" Android :isIndicator="true" Android :isIndicator="true" android:isIndicator="true" android:isIndicator="true Android :stepSize="0.5" // Specify how many stars to change each time. Default is 0.5Copy the code

Listen for an event

/ / article listening score values change event batingBar setOnRatingBarChangeListener (new OnRatingBarChangeListener () {@ Override public void OnRatingChanged (RatingBar RatingBar, float rating, Boolean fromUser) {ProgressBar}});Copy the code

ScrollView

The scroll view is used to add scroll bars to other components. By default, when there is too much content on the form and one screen cannot be displayed, the excess part cannot be seen by the user, because the Android layout manager does not provide the ability to scroll the screen. ScrollView is a subclass of FrameLayout, so in a ScrollView, you can add any component you want, but you can only put one component in a ScrollView, and if you want to put more than one, you can put a layout manager first, and then you can put the components you want into that layout manager, In scroll view, the linear layout manager is used a lot

The XML configuration:

<ScrollView
	android:id="@+id/scrollView"
	android:layout_width="match_parent"
	android:layout_height="wrap_content">	
<ScrollView/>	
Copy the code

Horizontal scroll view

The HorizontalScrollView is similar to the ScrollView, but it scrolls horizontally

The XML configuration:

<HorizontalScrollView
	android:layout_width="match_parent"
	android:lauout_height=wrap_content">
	
<HorizontalScrollView/>
Copy the code

AutoCompleteTextView

Enter the information we want to enter in the input box and the relevant information pops up. This effect is implemented in Android using the AutoCompleteTextView

AutoCompleteTextView inherits EditText and has all the properties and methods of EditText

The XML configuration:

<AutoCompleteTextView
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:text=""
	android:completionThreshold="2"/>	
Copy the code

Commonly used attributes

Android: completionHint used in the pop-up menu to specify tip title android: completionThreshold is used to specify the user input is at least a few characters will show tip android: dropDownHeight Is used to specify the height of the drop-down menu android: dropDownHorizontalOffset is used to specify the drop-down offset between the waist and text level. Down her waist and text left-aligned by default android: dropDownVerticalOffset is used to specify the drop-down vertical led between the waist and text. Dropdown menus follow text boxes by default. Android :dropDownWidth Is used to specify the width of the dropdown menu. Android :popupBackground is used to set the background for the dropdown menuCopy the code

The AutoCompleteTextView component has to set the data, typically an array of strings, and we need to assemble the view and the data, which is where the adapter comes in

The adapter is passed in using the setAdapter(adapter) method

Adapter Adapter class

Adapter is an Adapter interface that connects back-end data and front-end display. It is an important link between data and UI(View). In common View(ListView,GridView) and other places need to use Adapter Adapter

The class diagram is structured as follows

The figure shows the hierarchy of all interfaces and classes associated with Android and Adapter. Can according to their own needs in the process of we use implementing an interface or a derived class, expand the adapter which are frequently used are: BaseAdapter, SimpleAdapter, ArrayAdapter, SimpleCursorAdapter, etc

BaseAdapter is an abstract class, inheriting it requires more methods, so it also has high flexibility. ArrayAdapter supports generic operations, the simplest, can only extend a line of words. SimpleAdapter has the best extensibility and can customize various effects. The SimpleCursorAdapter can be used with a simple text-only ListView that requires Cursor fields to correspond to UI ids. You can override other methods if you need to implement a more complex UI. You can think of SimpleAdapter as a simple combination of a database that makes it easy to display the contents of a database as a list.

ArrayAdapter class

An ArrayAdapter is used to wrap multiple values of an array or List into multiple List items. An ArrayAdapter has six constructors

Public ArrayAdapter(Context Context,int resource,int textViewResourceId, List<T> objects) Usually, you pass in the current Activity //resource which represents the layout resource, and you pass in the layout file that you need, Commonly used directly / / android. R.l ayout. Simple_list_item_1, the layout contains a TextView / / textViewResourceId represent resourece TextView Id number in the layout, Android.r.i.ext1 // where T stands for generics, arrays and collections are our dataCopy the code

Automatic text box (MultiAutoCompleteTextView)

An editable text view inherited from the AutoCompleteTextView that effectively expands prompts for text typed by the user without requiring the user to enter the entire content. It differs from the AutoCompleteTextView in that it can handle multiple input fields, such as the contact list in the SMS interface, edit multiple contacts at the same time, and prompt.

The XML configuration:

<MultiAutoCompleteTextView
	android:layout_width="match_parent"
	android:layout_height="wrap_content">
	
<MultiAutoCompleteTextView/>
Copy the code

The user must provide a MultiAutoCompleteTextView. Tokenizer used to distinguish different substrings

The separator:

multiAutoCompleteTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer()); //CommaTokenzier uses', 'to distinguishCopy the code

List selection box

A Spinner is a drop-down list that is typically used to select a list of selectable items and can be used by adapters or to set array resources directly

The XML configuration:

<Spinner android:layout_width="match_parent" android:layout_height="wrap_content" android:entries="@array/ctype"/> // Set the array sourceCopy the code

Select the default:

spinner.setSelection(2,true); Default selected valueCopy the code

Listening event:

spinner.setOnitemSelectedListener(new OnItemSelectedListener(){ @Override public void onItemSelected(AdapterView<? >parent,View View,int position,long ID){Override public void onNothingSelected(AdapterView<? > parent) {// When there is no choice}});Copy the code

It can also set up an adapter as the data source, which is required if there is more than one TextView in the drop-down list to display text, but typically only one text is required for drop-down lists. spinner.setAdapter(adapter); If multiple component values need to change, the SimpleAdapter class is generally used

SimpleAdapter class

SimpleAdapter is the most extensible adapter that can define any layout you want

Construction method:

SimpleAdapter(Context context, List<? extends Map<String, ? >> data, int resource, String[] from, int[] to) parameter context: context, such as this. Data: Map list of data to display. This part needs to be implemented by itself. The type must be the same as above, and each item must be the same as the entry specified in from. ListView Specifies the Id of a single layout file. This layout is your custom layout. The layout you want to display is in this layout. The layout must include the control ID parameter defined in TO: FROM: a list of column names associated with each item added to the Map. To: is an int array whose ids are the ids of each control in the custom layout, and must correspond to the from parameter aboveCopy the code

ListView

ListView is one of the most important views in Android. It is used by almost every software. It is presented as a vertical list, and only Adapter can map the data in the list to the ListView

The list display requires three elements

  • ListView The View used to display the list
  • The adapter is used to map data to the mediation on the ListView
  • Data specific strings, images, or basic components to be mapped

The XML configuration:

<ListView
	android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:entries="@array/ctype">

<ListView/>
    
Copy the code

Common attributes:

Android: Divider Divider sets the visual divider for the list, which can be separated by color or a Drawable resource. Android :dividerHeight Divider sets the height of the divider android: Entries The data source of the list Android: headerDividersEnabled is used to set whether drawing after headerView splitter, the default value is true, when set to false, says it will not draw, Using this property requires setting the headerView to the ListView through the addHeaderView() method provided by the LiswView componentCopy the code

Listening event:

listView.setOnItemClickListener(new OnItemClickListener(){ @Override public void onItemClick(AdapterView<? > parent, View View, int position, long id) {// where position is the current list position}});Copy the code

Built-in system layout:

android.R.layout.simple_list_item_multiple_choice: Each list item has the checkbox text Android.r.layout.simple_list_ITEM_1. Each list item is a plain text Android.r.layout.simple_list_ITEM_2 Each list item is plain text (slightly larger) Android.r.layout. simple_list_item_CHECKED Each list item has a checked list item Android.r.layout. simple_list_ITEM_singLE_CHOICE Each list item is text with a radio buttonCopy the code

If there is nothing too complex in the List, we can draw the List directly using an ArrayAdapter. But if the List is too complex, you need to use a custom layout to implement the List. Use the SimpleAdapter above with the ListView and display the contents.

The most common adapter for ListView is BaseAdapter, which itself is an abstract class that implements ListAdapter and SpinnerAdapter, and is a parent of ArrayAdapter and SimpleAdapter. It allows us to customize our views more freely.

Share with you

I want to work my way up

Fly forward on the blades at the highest point

Let the wind blow dry tears and sweat

I want to work my way up

Waiting for the sun to watch its face

Little days have big dreams

I have my day

Let the wind blow dry tears and sweat

One day I will have my day