GridView、AutoTextView、Spinner、Gallery、ExpandableListView

这里写图片描述

GridView的案例

MainActivity

public class MainActivity extends AppCompatActivity {
private GridView mGridView;
    private LayoutInflater mInflater;
    private List<Fruit> mFruits;
    private MyGirdAdapter mAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mGridView= (GridView) findViewById(R.id.gridview);
        mInflater=getLayoutInflater();//mInflater有多种获取方式
        mFruits=new ArrayList<>();
        mAdapter=new MyGirdAdapter(mInflater,mFruits);
        mGridView.setAdapter(mAdapter);
        for(int i=0;i<10;i++) {
            Fruit apple = new Fruit("菠萝", R.mipmap.pineapple);
            Fruit peach = new Fruit("桃子", R.mipmap.peach);
            Fruit banana = new Fruit("香蕉", R.mipmap.fbanana);
            Fruit strawberry = new Fruit("草莓", R.mipmap.fstrawberry);
            mFruits.add(apple);
            mFruits.add(banana);
            mFruits.add(strawberry);
            mFruits.add(peach);
        }

    }

MyGridAdapter

public class MyGriAdapter extends BaseAdapter {
    private List<Fruit> mFruits;
    private LayoutInflater mInflater;

    public MyGirdAdapter( LayoutInflater Inflater,List<Fruit> Fruits) {
        mFruits = Fruits;
       mInflater = Inflater;
    }

    @Override
    public int getCount() {
        return mFruits.size();
    }

    @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) {
        ViewHolder viewHolder=null;
        if(convertView==null){
            convertView=mInflater.inflate(R.layout.item_gird,null);
            viewHolder=new ViewHolder();
            viewHolder.textView= (TextView) convertView.findViewById(R.id.textview);
            viewHolder.imageView= (ImageView) convertView.findViewById(R.id.imageview);
            convertView.setTag(viewHolder);
        }else{
            viewHolder= (ViewHolder) convertView.getTag();
        }
        Fruit fruit=mFruits.get(position);
        viewHolder.textView.setText(fruit.getName());
        viewHolder.imageView.setImageResource(fruit.getImg());
        return convertView;
    }
    class ViewHolder{
        TextView textView;
        ImageView imageView;
    }
}

activity_main.xml

<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" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<GridView
    android:id="@+id/gridview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:numColumns="3">

</GridView>

</RelativeLayout>

item_gird.xml

<?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">
<TextView
    android:id="@+id/textview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
    <ImageView
        android:id="@+id/imageview"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@mipmap/pineapple"/>

</LinearLayout>

运行图

>

AutoTextView案例

MainActivity

public class MainActivity extends AppCompatActivity {

    private AutoCompleteTextView mAutoTextView;
    private ArrayAdapter<String> mAdapter;
    private String [] mData={"layout_linear","layout_relative","layout_frame","view_text","view_image","view_button","editView","peach","pineapple"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mAutoTextView= (AutoCompleteTextView) findViewById(R.id.auto_textview);
        mAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,mData);
        mAutoTextView.setAdapter(mAdapter);
    }

activity_main.xml

<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" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <AutoCompleteTextView
        android:id="@+id/auto_textview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="search"/>
</RelativeLayout>

运行图

这里写图片描述

Spinner案例

MainActivity

public class MainActivity extends AppCompatActivity {
private Spinner mSpinner;
    private ArrayAdapter<String > mAdapter;
    private String [] mData={"layout_linear","layout_relative","layout_frame","view_text","view_image","view_button","editView","peach","pineapple"};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mSpinner= (Spinner) findViewById(R.id.spinner);
       mAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,mData);
       mSpinner.setAdapter(mAdapter);
    }

activity_main.xml

<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" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </Spinner>
</RelativeLayout>

运行图

这里写图片描述

Gallery案例

MainActivity

public class MainActivity extends AppCompatActivity {

    private Gallery mGallery;
    private LayoutInflater mInflater;
    private List<Fruit> mFruits;
    private MyGalleryAdapter mAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
        mGallery= (Gallery) findViewById(R.id.gallery);
        mInflater=getLayoutInflater();//mInflater有多种获取方式
        mFruits=new ArrayList<>();

        for(int i=0;i<10;i++) {
            Fruit apple = new Fruit("菠萝", R.mipmap.pineapple);
            Fruit peach = new Fruit("桃子", R.mipmap.peach);
            Fruit banana = new Fruit("香蕉", R.mipmap.fbanana);
            Fruit strawberry = new Fruit("草莓", R.mipmap.fstrawberry);
            mFruits.add(apple);
            mFruits.add(banana);
            mFruits.add(strawberry);
            mFruits.add(peach);
        }
        mAdapter=new MyGalleryAdapter(mInflater,mFruits);
        mGallery.setAdapter(mAdapter);
        mGallery.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Log.d("mygallery","onItemSelected"+mFruits.get(position).getName());//得到居中位置上的图片的名称
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

    }

MyGalleryAdapter

public class MyGalleryAdapter extends BaseAdapter {
    private List<Fruit> mFruits;
    private LayoutInflater mInflater;

    public MyGalleryAdapter(LayoutInflater mFruits, List<Fruit> mInflater) {
        this.mFruits = mFruits;
        this.mInflater = mInflater;
    }

    @Override
    public int getCount() {
        return mFruits.size();
    }

    @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) {
        ViewHolder viewHolder=null;
        if(convertView==null){
            convertView=mInflater.inflate(R.layout.item_gird,null);
            viewHolder=new ViewHolder();
            viewHolder.textView= (TextView) convertView.findViewById(R.id.textview);
            viewHolder.imageView= (ImageView) convertView.findViewById(R.id.imageview);
            convertView.setTag(viewHolder);
        }else{
            viewHolder= (ViewHolder) convertView.getTag();
        }
        Fruit fruit=mFruits.get(position);
        viewHolder.textView.setText(fruit.getName());
        viewHolder.imageView.setImageResource(fruit.getImg());
        return convertView;
    }
    class ViewHolder{
        TextView textView;
        ImageView imageView;
    }
}

Fruit类

public class Fruit {
    private String name;
    private int img;

    public Fruit(String name, int img) {
        this.name = name;
        this.img = img;
    }

    public String getName() {
        return name;

    }

    public void setName(String name) {
        this.name = name;
    }

    public int getImg() {
        return img;
    }

    public void setImg(int img) {
        this.img = img;
    }
}

item_gallery.xml

<?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">

    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        />
    <ImageView
        android:id="@+id/imageview"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@mipmap/pineapple"/>
</LinearLayout>

activity_main.xml

<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" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">


<Gallery
    android:id="@+id/gallery"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:spacing="50dp"
    android:unselectedAlpha="0.5">

</Gallery>

</RelativeLayout>

运行图

这里写图片描述

ExpandableListView案例

MainActivity

public class MainActivity extends AppCompatActivity {
private ExpandableListView mEListView;
    private List<Clazz>  mClazzs;
private MyExpandableAdapter mAdapter;
    private LayoutInflater mInflater;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mEListView= (ExpandableListView) findViewById(R.id.expandlistview);
        initData();
        mInflater=getLayoutInflater();
        mAdapter=new MyExpandableAdapter(mClazzs,mInflater);
        mEListView.setAdapter(mAdapter);
    }

    private void initData() {
        mClazzs=new ArrayList<>();
        Clazz clazz1=new Clazz("一班","201501");
      List<Student> student1=new ArrayList<>();
        student1.add(new Student("张三","18","男"));
        student1.add(new Student("李四","17","男"));
        student1.add(new Student("王五","19","女"));
        student1.add(new Student("赵六", "20", "男"));
        student1.add(new Student("李悦","23","女"));
        student1.add(new Student("曹林林","20","女"));
        student1.add(new Student("张方舟","8","女"));
        clazz1.setStudents(student1);
        Clazz clazz2=new Clazz("二班","201502");
        List<Student> student2=new ArrayList<>();
        student2.add(new Student("张三","18","男"));
        student2.add(new Student("李四","17","男"));
        student2.add(new Student("王五","19","女"));
        student2.add(new Student("赵六","20","男"));
        clazz2.setStudents(student2);

        Clazz clazz3=new Clazz("三班","201503");
        List<Student> student3=new ArrayList<>();
        student3.add(new Student("张三","18","男"));
        student3.add(new Student("李四","17","男"));
        student3.add(new Student("王五","19","女"));
        student3.add(new Student("赵六","20","男"));
        student3.add(new Student("杜蛋蛋","5","男"));
        student3.add(new Student("曹晓晓","21","女"));
        clazz3.setStudents(student3);
        mClazzs.add(clazz1);
        mClazzs.add(clazz2);
        mClazzs.add(clazz3);

    }

Clazz类

public class Clazz {
    private String clazzName;
    private String clazzNum;
    private List<Student> students;

    public String getClazzName() {
        return clazzName;
    }

    public void setClazzName(String clazzName) {
        this.clazzName = clazzName;
    }

    public Clazz(String clazzName, String clazzNum) {
        this.clazzName = clazzName;
        this.clazzNum = clazzNum;
    }

    public List<Student> getStudents() {
        return students;
    }

    public void setStudents(List<Student> students) {
        this.students = students;
    }

    public String getClazzNum() {
        return clazzNum;
    }

    public void setClazzNum(String clazzNum) {
        this.clazzNum = clazzNum;
    }
}

Student类

public class Student {

    private  String studentName;
    private  String age;
    private  String sex;
    private  String lable;

    public String getStudentName() {
        return studentName;
    }

    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getLable() {
        return lable;
    }

    public void setLable(String lable) {
        this.lable = lable;
    }

    public Student(String studentName, String age, String sex) {
        this.studentName = studentName;
        this.age = age;
        this.sex = sex;

    }
}

MyExpandableAdapter

public class MyExpandableAdapter extends BaseExpandableListAdapter {
    private List<Clazz> mClazzs;
    private LayoutInflater mInflater;

    public MyExpandableAdapter(List<Clazz> mClazzs, LayoutInflater mInflater) {
        this.mClazzs = mClazzs;
        this.mInflater = mInflater;
    }

    @Override
    public int getGroupCount() {
        return mClazzs.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return mClazzs.get(groupPosition).getStudents().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groupPosition;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        convertView = mInflater.inflate(R.layout.item_clazz, null);
        TextView textviewClazzName = (TextView) convertView.findViewById(R.id.textview_clazz_name);
        TextView textviewClazzNum = (TextView) convertView.findViewById(R.id.textview_clazz_num);
        TextView textviewClazzStudents = (TextView) convertView.findViewById(R.id.textview_clazz_students);
        Clazz clazz = mClazzs.get(groupPosition);
        textviewClazzName.setText(clazz.getClazzName());
        textviewClazzNum.setText(clazz.getClazzNum());
        textviewClazzStudents.setText("总人数:" + clazz.getStudents().size());

        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        convertView = mInflater.inflate(R.layout.item_student, null);
        TextView textviewStudentName = (TextView) convertView.findViewById(R.id.textview_student_name);
        TextView textviewStudentAge = (TextView) convertView.findViewById(R.id.textview_student_age);
        TextView textviewStudentSex = (TextView) convertView.findViewById(R.id.textview_student_sex);
        Clazz clazz = mClazzs.get(groupPosition);
        List<Student> students = clazz.getStudents();
        Student student = students.get(childPosition);
        textviewStudentName.setText(student.getStudentName());
        textviewStudentAge.setText(student.getAge());
        textviewStudentSex.setText(student.getSex());
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }
}

activity_main.xml

<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" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

   <ExpandableListView
      android:id="@+id/expandlistview"
       android:layout_width="match_parent"
       android:layout_height="match_parent">

   </ExpandableListView>

</RelativeLayout>

item_clazz.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:gravity="center_vertical"
    android:background="@drawable/back"
    >
<TextView
    android:id="@+id/textview_clazz_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/textview_clazz_num"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/textview_clazz_students"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

item_student.xml

<?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="horizontal">
<TextView
    android:id="@+id/textview_student_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/textview_student_sex"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"/>
    <TextView
        android:id="@+id/textview_student_age"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

运行图

这里写图片描述

总结

1、先建立Activity,用setContentView()加载布局。
2、在XML文件中添加GridView布局(或者是AutoTextView、Spinner、Gallery等布局)。
3、用findViewById找到GridView布局的id号。
4、GridView布局显示数据的时候,必须设置adapter

设置adapter

继承BaseAdapter
实现四个方法(有些布局不止四个方法,比如ExpandableListView)
getView()方法
写内部类ViewHolder
5、初始化数据
6初始化Adapter
7、调用GridView的setAdapter()。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值