1 AdapterView Class introduction

1.1 AdapterView Components are a group of important components ,AdapterView It is a thread drawing class , The actual use is more Adapter Related subclasses ,AdapterView Has the following characteristics :

  • AdapterView Inherited ViewGroup, Its essence is the container
  • AdapterView Can include multiple list items , And many more “ List item ” In the right form
  • AdapterView The multiple list items displayed are Adapter Provide , call AdapterView Of setAdapter(Adapter) Method setting Adapter that will do

1.2 AdapterView And its subclasses

2 ListView and ListActivity

2.1 establish ListView Two ways :

  • Use it directly ListView Create
  • Give Way Activity Inherit ListActivity( It's equivalent to Activity According to the ListView)

2.2 ListView、GridView、Spinner、Gallery etc. AdapterView It's just containers , and Adapter Responsible for providing each “ List item ” Components ,AdapterView Is responsible for displaying these list items in an appropriate way

3 Adapter Interface and implementation

3.1 Adapter The reason for the interface

Adapter It's an adapter interface that connects back-end data and front-end display , It's data and UI(View) It's an important link between . In common View(ListView,GridView) And so on Adapter. As shown in the figure below Data、Adapter、View The relationship among the three :

3.2 Adapter Class view

  You can see from the picture that Android China and Adapter All interfaces related to 、 Complete hierarchy of classes . In the process of our use, we can implement interface or inherit class according to our own needs to extend . More commonly used are BaseAdapter,SimpleAdapter,ArrayAdapter,SimpleCursorAdapter etc. .

  • BaseAdapter Is an abstract class , Inheriting it requires more methods to be implemented , So there is a high degree of flexibility ;
  • ArrayAdapter Support for generic operations , The simplest , Only one line can be displayed .
  • SimpleAdapter It has the best scalability , You can customize various effects .
  • SimpleCursorAdapter It can be applied to simple text font ListView, It needs to Cursor Fields and UI Of id correspond . If you need to implement more complex UI You can also override other methods . Think of it as SimpleAdapter A simple combination of databases , It is convenient to display the contents of the database in the form of a list .

3.3 Realization Adapter The way

  • Use ArrayAdapter, The display of the list requires three elements :

    • ListVeiw Used to show the list View.
    • Adapter   Used to map data to ListView The agency on .
    • data The specific string to be mapped , picture , Or basic components .
  • Use SimpleAdapter
    • You can define all kinds of layouts , You can put ImageView( picture ), You can also put Button( Button ),CheckBox( Check box ) wait . But it can only be TextView Views and their subviews
    • It's a simple adapter , You can map static data to XML The view defined in the file . This data is generally used HashMap structure List,list Every section of the book corresponds to ListView Each line , according to Map The data in maps keywords to the specified view .

    • There are two phases to binding data to a view , First , If set SimpleAdapter.ViewBinder, that setViewValue(android.view.View, Object, String) Will be rewritten . If setViewValue The return value of true, Indicates that the binding is complete , The system default binding implementation will no longer be called . If the return value is false, Views will bind data in the following order :                             ( If View Realized Checkable( for example CheckBox), The expected binding value is a boolean type .)---->                                                                                          (TextView. The expected binding value is a string type , By calling setViewText(TextView, String) binding .)---->                                                                                  (ImageView, The expected binding value is a resource id Or a string , By calling setViewImage(ImageView, int) or setViewImage(ImageView, String) Data binding .)

    • ListItem The event response of the subview is also in SimpleAdapter.ViewBinder Of setViewValue Method
    • HashMap Each key value data of is mapped to the corresponding id Components on . There is no corresponding layout file available , You need to customize a layout vlist.xml.
    • new One SimpleAdapter The parameter one time is :Context / List<? extends Map<String, ?>> / @LayoutRes int resource  /  String[] from  / @IdRes int[] to
  • BaseAdapter
    • occasionally , Lists are not just used for display , We can also add buttons to it . To add a button, first write a button xml file , Then it's natural to think of using the above method to define an adapter , Then map the data to the layout file . But this is not the case , Because buttons can't be mapped , Even if you successfully display the button in the layout file, you can't add the response of the button , This is the time to study ListView How realistic it is , And you have to override a class inheritance BaseAdapter. The following example shows a button and an image , Two lines of words. If you click the button, the line of the button will be deleted . And tell you ListView How it works .

3.4 Example : Use SimpleAdapter To achieve ListView

activity file

import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects; public class ListActivityTest extends Activity {
private String[] names = {" The Monkey King ", " Pig eight quit ", " But the king bull ", " The sand monk "};
private String[] descs = {" Great monkey ", " The pig who doesn't quit ", " Ugly cow ", " Loyal monk "};
private int[] imageIds = {R.drawable.monkey, R.drawable.pig, R.drawable.bull, R.drawable.monk}; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_activity_main);
List<Map<String, Object>> listItems = new ArrayList<>();
for (int i = 0; i < names.length; i++) {
Map<String, Object> listItem = new HashMap<>();
listItem.put("header", imageIds[i]);
listItem.put("name", names[i]);
listItem.put("desc", descs[i]);
//listItem.
listItems.add(listItem);
}
SimpleAdapter simpleAdapter = new SimpleAdapter(this, listItems, R.layout.simple_item,
new String[]{"header", "name", "desc"},
new int[]{R.id.header, R.id.name, R.id.desc});
ListView listView = (ListView) findViewById(R.id.myList);
listView.setAdapter(simpleAdapter); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(ListActivityTest.this, names[position] + " I was killed ", Toast.LENGTH_SHORT).show();
}
});
} }

main Layout file

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_list_activity_test"
tools:context="com.example.penghuster.exampletest.ListActivityTest"> <ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/myList"/>
</RelativeLayout>

Adapter Layout file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/header"
android:paddingLeft="10dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="10pt"
android:textColor="@color/red"
android:paddingLeft="8dp"
android:id="@+id/name"/> <TextView
android:id="@+id/desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="6pt"
android:textColor="@color/green"
android:paddingLeft="10dp"/>
</LinearLayout>
</LinearLayout>

3.5 Example : Use simpleAdapter Realization item The response of the subview

activity file

import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects; public class ListActivityTest extends Activity {
private String[] names = {" The Monkey King ", " Pig eight quit ", " But the king bull ", " The sand monk "};
private String[] descs = {" Great monkey ", " The pig who doesn't quit ", " Ugly cow ", " Loyal monk "};
private int[] imageIds = {R.drawable.monkey, R.drawable.pig, R.drawable.bull, R.drawable.monk};
private String[] btnLabels = {" The Monkey King ", " Pig eight quit ", " But the king bull ", " The sand monk "}; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_activity_main);
List<Map<String, Object>> listItems = new ArrayList<>();
for (int i = 0; i < names.length; i++) {
Map<String, Object> listItem = new HashMap<>();
listItem.put("header", imageIds[i]);
listItem.put("name", names[i]);
listItem.put("desc", descs[i]);
listItem.put("button", btnLabels[i]);
listItems.add(listItem);
}
SimpleAdapter.ViewBinder binder = new SimpleAdapter.ViewBinder() { @Override
public boolean setViewValue(View view, Object data, String textRepresentation) {
if (view instanceof Button) {
final View button = view;
// button.setBackgroundDrawable(getResources().getDrawable(R.drawable.pig));
view.setOnClickListener(new View.OnClickListener() {
LinearLayout listItem = (LinearLayout) button.getParent();
TextView tv = (TextView) listItem.findViewById(R.id.name); @Override
public void onClick(View v) {
Toast.makeText(ListActivityTest.this, tv.getText(), Toast.LENGTH_SHORT).show();
}
});
return false;
}
return false;
}
};
SimpleAdapter simpleAdapter = new SimpleAdapter(this, listItems, R.layout.simple_item,
new String[]{"header", "name", "desc", "button"},
new int[]{R.id.header, R.id.name, R.id.desc, R.id.button});
simpleAdapter.setViewBinder(binder);
ListView listView = (ListView) findViewById(R.id.myList);
listView.setAdapter(simpleAdapter); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(ListActivityTest.this, names[position] + " I was killed ", Toast.LENGTH_SHORT).show();
}
});
} }

main Layout file

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_list_activity_test"
tools:context="com.example.penghuster.exampletest.ListActivityTest"> <ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/myList"/>
</RelativeLayout>

adapter Layout file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:descendantFocusability="blocksDescendants"> <ImageView
android:id="@+id/header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp" /> <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"> <TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:textColor="@color/red"
android:textSize="10pt" /> <TextView
android:id="@+id/desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:textColor="@color/green"
android:textSize="6pt" />
</LinearLayout> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button" />
</LinearLayout>

design sketch

Be careful :

  • listItem Focus relation with subview :Item The root layout of the layout plus android:descendantFocusability
  • button The response function of the subview is defined in SimpleAdapter.ViewBinder Of setViewValue Method ( So it must be overloaded SimpleAdapter.ViewBinde)

3.6 Example :BaseAdapter establish

activity Code

package com.example.penghuster.exampletest;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView; public class BaseAdapterActivity extends Activity {
ListView myList; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_activity_main);
myList = (ListView)findViewById(R.id.myList);
BaseAdapter adapter = new BaseAdapter() {
@Override
public int getCount() {
return 40;
} @Override
public Object getItem(int position) {
return null;
} @Override
public long getItemId(int position) {
return position;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout line = new LinearLayout(BaseAdapterActivity.this);
line.setOrientation(LinearLayout.HORIZONTAL);
ImageView image = new ImageView(BaseAdapterActivity.this);
image.setBackgroundResource(R.drawable.bull);
TextView textView = new TextView(BaseAdapterActivity.this);
textView.setText(" The first " + (position + 1) + " List items ");
textView.setTextSize(12);
textView.setTextColor(Color.RED);
line.addView(image);
line.addView(textView);
return line;
}
};
myList.setAdapter(adapter);
} }

4   Auto complete text box (AutoCompleteTextView) The function and usage of

With normal ArrayAdapter Just fit the data

5 Grid view (GridView) Function and usage

main Layout code

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <GridView
android:id="@+id/grid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:horizontalSpacing="1pt"
android:verticalSpacing="1pt"
android:numColumns="4"/>
<ImageView
android:id="@+id/image"
android:layout_width="240dp"
android:layout_height="240dp" /> </LinearLayout>

Adapter Layout code

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:padding="2pt"
>
<ImageView
android:id="@+id/image1"
android:layout_width="50dp"
android:layout_height="50dp"
/>
</LinearLayout>

Activity Code

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; public class GridViewTest extends Activity {
GridView grid;
ImageView imageView;
int[] imageIds = new int[]{R.drawable.bomb5, R.drawable.bomb6, R.drawable.bomb7,
R.drawable.bomb8, R.drawable.bomb9, R.drawable.bomb10, R.drawable.bomb11,
R.drawable.bomb12, R.drawable.bomb13, R.drawable.bomb14, R.drawable.bomb15, R.drawable.bomb16}; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid_layout);
List<Map<String, Object>> listItems = new ArrayList<>();
for (int i = 0; i < imageIds.length; i++) {
Map<String, Object> listItem = new HashMap<>();
listItem.put("image", imageIds[i]);
listItems.add(listItem);
} imageView = (ImageView) findViewById(R.id.image);
SimpleAdapter simpleAdapter = new SimpleAdapter(this, listItems, R.layout.cell,
new String[]{"image"}, new int[]{R.id.image1});
grid = (GridView) findViewById(R.id.grid);
grid.setAdapter(simpleAdapter); grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
imageView.setImageResource(imageIds[position]);
}
});
} }

design sketch :

6 Extensible list component ExpandableListView

  • ExpandableListView yes ListView Subclasses of , It's in the ordinary ListView On the basis of expansion , It divides the list items into groups , Each group can contain multiple list items
  • ExpandableListView The list items displayed should be ExpandableListAdapter Provide ,ExpandableListAdapter It's also an interface , This interface provides the inheritance relationship as shown in the figure below

  • And Adapter Kind of similar , Realization ExpandableListAdapter There are also three common ways :

    • Expand BaseExpandableListAdapter
    • Use SimpleExpandableListAdapter Put two list Assemble into ExpandableListAdapter
    • Use SimpleCursorTreeAdapter take Cursor The data in is packaged as SimpleCursorTreeAdapter

Example : utilize BaseExpandableListAdapter Realization Adapter

Activity Code

import android.app.Activity;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView; public class ExpandableListViewTest extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.expandable_list_view);
ExpandableListAdapter adapter = new BaseExpandableListAdapter() {
int[] logos = new int[]{R.drawable.bull, R.drawable.monk, R.drawable.monkey};
private String[] armTypes = new String[]{" Protoss arms ", " Zerg arms ", " Terran arms "};
private String[][] arms = new String[][]{
{" berserker ", " berserker ", " berserker ", " berserker "},
{" puppy ", " puppy ", " puppy ", " puppy "},
{" Machine gunner ", " Machine gunner ", " Machine gunner ", " Machine gunner "}
};
@Override
public int getGroupCount() {
return armTypes.length;
} @Override
public int getChildrenCount(int groupPosition) {
return arms[groupPosition].length;
} @Override
public Object getGroup(int groupPosition) {
return armTypes[groupPosition];
} @Override
public Object getChild(int groupPosition, int childPosition) {
return arms[groupPosition][childPosition];
} @Override
public long getGroupId(int groupPosition) {
return 0;
} @Override
public long getChildId(int groupPosition, int childPosition) {
return groupPosition;
} @Override
public boolean hasStableIds() {
return true;
} @Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
LinearLayout linearLayout = new LinearLayout(ExpandableListViewTest.this);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
ImageView logo = new ImageView(ExpandableListViewTest.this);
logo.setImageResource(logos[groupPosition]);
linearLayout.addView(logo);
TextView textView = new TextView(ExpandableListViewTest.this);
textView.setText(getGroup(groupPosition).toString());
linearLayout.addView(textView);
return linearLayout;
} @Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
LinearLayout linearLayout = new LinearLayout(ExpandableListViewTest.this);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
ImageView logo = new ImageView(ExpandableListViewTest.this);
logo.setImageResource(logos[groupPosition]);
linearLayout.addView(logo);
TextView textView = new TextView(ExpandableListViewTest.this);//getTextView();
textView.setText(getChild(groupPosition, childPosition).toString());
linearLayout.addView(textView);
return linearLayout;
} @Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
};
ExpandableListView expandableListView = (ExpandableListView)findViewById(R.id.expandList);
expandableListView.setAdapter(adapter);
} }

Main Layout file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/expandable_list_view"
tools:context="com.example.penghuster.exampletest.ExpandableListView"> <ExpandableListView
android:id="@+id/expandList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>

design sketch

7 Spinner The function and usage of

  • Spinner It's a list selection box , Click to pop up a menu for users to choose
  • if Spinner The list options for have been fully determined , It's in android:entries Property to add the array ; if Spinner List options need to be dynamically determined , You need to use Adapter Adapter adds data

Example :

Activity file

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Spinner; public class SpinnerTest extends AppCompatActivity {
Spinner spinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spinner_main);
spinner = (Spinner) findViewById(R.id.spinner);
String[] arr = {" Tang's monk ", " Pig eight quit ", " The Monkey King "};
ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
android.R.layout.simple_spinner_dropdown_item, arr);
spinner.setAdapter(adapter);
} }

main Layout file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"> <Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/books"/> <Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"/> </LinearLayout>

design sketch

8 Gallery Function and usage of

  • Spinner It shows a vertical list selection box ;Gallery It shows a horizontal list selection box
  • Spinner For users to choose ,Gallery Allows users to drag to see the previous 、 Next list

9 AdapterViewFlipper Function and usage of

AdapterViewFlipper Inherited AdapterViewAnimator, It will also show Adapter Multiple provided View Components , But only one at a time View Components , It can turn pages manually , It can also turn pages automatically

Example

Activity file

import android.app.ActionBar;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterViewFlipper;
import android.widget.BaseAdapter;
import android.widget.ImageView; public class AdapterViewFlipperTest extends Activity {
int[] imageIds = {R.drawable.monkey, R.drawable.monk, R.drawable.bull, R.drawable.bomb5,
R.drawable.bg, R.drawable.bomb10, R.drawable.bomb15};
AdapterViewFlipper flipper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.flipper_view_main);
flipper = (AdapterViewFlipper) findViewById(R.id.flipper); BaseAdapter adapter = new BaseAdapter() {
@Override
public int getCount() {
return imageIds.length;
} @Override
public Object getItem(int position) {
return position;
} @Override
public long getItemId(int position) {
return position;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(AdapterViewFlipperTest.this);
imageView.setImageResource(imageIds[position]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT ));
return imageView;
}
};
flipper.setAdapter(adapter);
} public void prev(View source){
flipper.showPrevious();
} public void next(View source){
flipper.showNext();
} public void auto(View source){
flipper.startFlipping();
} }

main Layout file

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:showIn="@layout/flipper_view_main"
tools:context="com.example.penghuster.exampletest.AdapterViewFlipper"> <AdapterViewFlipper
android:id="@+id/flipper"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:flipInterval="5000"
android:layout_alignParentTop="true"/> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:onClick="prev"
android:text=" the previous "/> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:onClick="next"
android:text=" next "/> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:onClick="auto"
android:text=" Auto play "/> </RelativeLayout>

design sketch

10 StackView Function and usage

StackView It's also inherited from AdapterViewAnimator, It is used to put Adapter The series view provided , With “ The stack ” To show . To control StackView According to the View Components ,StackView The following two control modes are provided :

  • Towed away StackView At the top of View, next View It will show , Put the previous View Drag in StackView, Make it show
  • By calling StackView Of showNext()、showPrevious The control shows the last 、 The next component

design sketch

The first 3 Group UI Components :AdapterView And its subclasses

  1. Android The user interface UI Components --AdapterView And its subclasses ( One ) ListView And various Adapter Detailed explanation

    ListView It's the list component , Usually by inheritance ListActivity Use what the system provides ListView. be-all AdapterView Components need to have a corresponding Adapter As an adapter to display the layout of the elements in the list ...

  2. Android The user interface UI Components --AdapterView And its subclasses ( 5、 ... and ) Spinner and SpinnerAdapter

    Spinner It's a drop-down box component , You can customize the drop-down layout style , have access to ArrayAdapter as well as SpinnerAdapter adapter stay Adapter To realize SpinnerAdapter, Inherit BaseAdapte ...

  3. Android The user interface UI Components --AdapterView And its subclasses ( Four ) GridView

    GridView frequently-used XML attribute : android:columnWidth   Set the width of the column . android:horizontalSpacing   The distance between the two columns .  android:numColum ...

  4. Android The user interface UI Components --AdapterView And its subclasses ( 3、 ... and ) ExpandableListView

    ExpandableListView: List Each item in can expand and contract . A retractable ListView. android:cacheColorHint="#00000000" This ...

  5. Android The user interface UI Components --AdapterView And its subclasses ( Two ) AdapterViewAnimator And its subclasses

    AdapterViewAnimator: Animation is displayed when switching between views . android:animateFirstView Definition ViewAnimation Whether to apply animation to the current view on first display . android ...

  6. Group 4 UI Components :AdapterView And subclasses

    AdapterView Components are a group of important components ,AdapterView This province is an abstract base class , Its derived subclasses are very similar in usage , It's just the difference between the display interface and the user interface , So this time we will focus on their common features , And highlight their differences . A ...

  7. The first 6 Group UI Components :ViewAnimator And its subclasses

    ViewAnimator Is a base class , It inherited FrameLayout, So it shows FrameLayout Characteristics of , Multiple View Components “ Stack ” together .ViewAnimator The extra features are just as the name implies ...

  8. The first 1 Group UI Components : Layout manager

    1 The source of layout management In order to make UI It works well on different mobile screens ---- The resolution of different cell phone screens / It's not exactly the same size , If you let the program control the size of each component manually . Location , It's going to be a lot of trouble for programming . To solve this problem .andro ...

  9. The first 2 Group UI Components :TextView And its subclasses

    1 TextView And its subclasses TextView Inherit directly from View, yes EditView And Button The parent of two classes , The following for TextView The inheritance relationship of each subclass . 2 individual UI The style map of CheckedTe ...

Random recommendation

  1. ADO.NET Access database

    Access to the database is the core technology of various database application development ,.NET In the framework ADO.NET Technology provides a consistent interface for application development , Enhance the portability and extensibility of the program . a: Use linked objects Connection Connect data ...

  2. SQL Statement performance test

    /* --Sqlserver Clear execution cache , be used for SQL Statement performance test DBCC DROPCLEANBUFFERS DBCC FREEPROCCACHE */

  3. C Language strings and numbers are converted to each other

    stay C/C++ There is no special string variable in the language , Character arrays are usually used to store strings . String is based on “\0” As an end sign .C/C++ Provides a wealth of string processing functions , Here are some of the most commonly used functions . ● String output function puts. ● ...

  4. ASP.NET utilize WINRar Achieve online decompression file

    One . The server must be installed winrar This software . Two . establish Helper class , as follows : using System; using System.Collections.Generic; using Syste ...

  5. binlog/relay_log Clean-up

    http://www.yuminstall.com/how-to-remove-mysql-relay-log.html CHANGE MASTER TO changes the parameters ...

  6. In depth understanding of the second edition of computer system problem solving CSAPP 2.8

    Give the evaluation result of Boolean place of bit trace . operation result a 0110 1001 b 0101 0101     ~a 1001 0110 ~b 1010 1010     a&b 0100 0001 a ...

  7. 201521123049 《JAVA Programming 》 The first 12 Weekly learning summary

    1. This week's learning summary 1.1 In the way you like ( Mind mapping or something ) Summarize the multi stream and file related content . 2. Written work take Student object ( attribute :int id, String name,int age,doubl ...

  8. Spark machine learning (8):LDA Topic model algorithm

    1. LDA Basic knowledge of LDA(Latent Dirichlet Allocation) It's a theme model .LDA A three-level Bayesian probability model , Include words . The three-tier structure of subject and document . LDA It's a generative model , It can be used to generate a ...

  9. Web crawler --requests Two important objects in the library

    When we use resquests.get() when , On return response The object of , It contains all the information that the server returns , It also contains the requested request Information about .  First : response The properties of an object are as follows , r.stat ...

  10. JavaScript: Detailed explanation Base64 Encoding and decoding

    Base64 It's one of the most common encodings , For example, it is used to transfer parameters in development . In modern browsers <img /> The label goes directly through Base64 String to render images and use in email, etc .Base64 Encoding in RFC2045 In the definition of , It has been ...