您的位置:首页 > 其它

Grid、Gallery、AutoCompleteTextView及Spinner(8.26)

2015-08-26 11:59 183 查看
Grid为分列形式的里面多了一个onItemSelected事件和Gallery为左右滑动的

Gallery示例图
fruit布局文件

只含有一个关键布局的布局grid或者gallery

Fruit类

FruitAdapter类

主类

AutoCompleteTextView和Spinner同时含有自动填充和下拉菜单

ExpandableListView类似qq列表的形式
ExpandableListView布局文件activity_mainxml

一级菜单布局item_clazzxml

二级菜单布局item_studentxml

clazz类一级菜单的类

Studnet类二级菜单的类

MyExpandableAdapter类

MainActivity主类

展示

Grid为分列形式的(里面多了一个onItemSelected事件)和Gallery为左右滑动的

实现和ListView大体相同,只是把原来放有ListView的布局中改放Gallery和Grid

Gallery示例图



fruit布局文件

<?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">
    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>


只含有一个关键布局的布局(grid或者gallery)

<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">
    <!--换成Grid,就成了Grally-->
    <Gallery
        android:id="@+id/gridView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:unselectedAlpha="0.4"
        android:spacing="20dp"
       ></Gallery>

</RelativeLayout>


Fruit类

package com.test.dukang.gridview;

/**
 * Created by Administrator on 2015/8/26.
 */
public class Fruit {
    private int image;
    private String name;

    public int getImage() {
        return image;
    }

    public void setImage(int image) {
        this.image = image;
    }

    public String getName() {
        return name;
    }

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

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


FruitAdapter类

package com.test.dukang.gridview.Adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.test.dukang.gridview.Fruit;
import com.test.dukang.gridview.R;

import java.util.List;

/**
 * Created by Administrator on 2015/8/26.
 */
public class FruitAdapter extends BaseAdapter{
    private List<Fruit> mList;
    private LayoutInflater mInfalter;

    public FruitAdapter(List<Fruit> mList, LayoutInflater mInfalter) {
        this.mList = mList;
        this.mInfalter = mInfalter;
    }

    @Override
    public int getCount() {
        return mList.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=mInfalter.inflate(R.layout.fruit_layout,null);
            viewHolder=new ViewHolder();
            viewHolder.imageView= (ImageView) convertView.findViewById(R.id.image);
            viewHolder.textView= (TextView) convertView.findViewById(R.id.textView);
            convertView.setTag(viewHolder);
        }else {
            viewHolder= (ViewHolder) convertView.getTag();
        }
        Fruit fruit=mList.get(position);
        viewHolder.imageView.setImageResource(fruit.getImage());
        viewHolder.textView.setText(fruit.getName());
        return convertView;
    }
    class ViewHolder{
        TextView textView;
        ImageView imageView;
    }
}


主类

package com.test.dukang.gridview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Adapter;
import android.widget.Gallery;
import android.widget.GridView;

import com.test.dukang.gridview.Adapter.FruitAdapter;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private List<Fruit> mList;
    private FruitAdapter mAdapter;
    private LayoutInflater mInflater;
    private GridView mGridView;
    private Gallery mGallery;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mList=new ArrayList<>();
        for(int i=0;i<30;i++){
            Fruit apple=new Fruit(R.mipmap.m,"苹果");
            Fruit banana=new Fruit(R.mipmap.m,"香蕉");
            Fruit orange=new Fruit(R.mipmap.m,"橘子");
            mList.add(apple);
            mList.add(banana);
            mList.add(orange);
        }
        mInflater=getLayoutInflater();
      //  mGridView= (GridView) findViewById(R.id.gridView);
        mGallery= (Gallery) findViewById(R.id.gridView);
        mAdapter=new FruitAdapter(mList,mInflater);
        mGallery.setAdapter(mAdapter);

    }

}


AutoCompleteTextView和Spinner(同时含有自动填充和下拉菜单)

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


public class MainActivity extends Activity {
    private AutoCompleteTextView mAutoText;
    private ArrayAdapter<String> mAdapter;
    private ArrayAdapter<String> mAdapter_Spinner;
    private Spinner mSpinner;
    private String[] mData={"list_view","list_a","list_b"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mSpinner = (Spinner) findViewById(R.id.spinner);
        mAutoText=(AutoCompleteTextView) findViewById(R.id.auto_text);
        mAdapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,mData);
        mAdapter_Spinner=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,mData);
        mSpinner.setAdapter(mAdapter_Spinner);
        mAutoText.setAdapter(mAdapter);
    }

}


ExpandableListView(类似qq列表的形式)

1.ExpandableListView布局文件(activity_main.xml)

android:childIndicator =“@null”可以取消一级菜单的小箭头显示,具体可参考api

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

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

</RelativeLayout>


2.一级菜单布局(item_clazz.xml)

就是显示红色背景的单条显示布局,必须给每个控件定义id,而且id名必须见名知意

<?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:background="#55ff00ff"
    android:orientation="horizontal" >

    <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_student"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>


3.二级菜单布局(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_age"
        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" />

</LinearLayout>


4.clazz类(一级菜单的类)

package com.example.model;

import java.util.List;

public class Clazz {
    private String clazzName;
    private String clazzNum;
    "*****注意的地方*****"
    //在这个类中定义了一个二级菜单包含类的集合,必须谨记
    private List<Student> students;
    /**
     *设置构造方法
     */
    public Clazz(String clazzName, String clazzNum) {

        this.clazzName = clazzName;
        this.clazzNum = clazzNum;
    }
    /**
     *设置get()和set()方法
     */
    public String getClazzName() {
        return clazzName;
    }
    public void setClazzName(String clazzName) {
        this.clazzName = clazzName;
    }
    public String getClazzNum() {
        return clazzNum;
    }
    public void setClazzNum(String clazzNum) {
        this.clazzNum = clazzNum;
    }
    public List<Student> getStudents() {
        return students;
    }
    public void setStudents(List<Student> students) {
        this.students = students;
    }

}


5.Studnet类(二级菜单的类)

package com.example.model;

public class Student {
    private String studentName;
    private String age;
    private String sex;
    private String lable;
    /**
     *设置构造方法
     */
    public Student(String studentName, String age, String sex, String lable) {

        this.studentName = studentName;
        this.age = age;
        this.sex = sex;
        this.lable = lable;
    }
    /**
     *设置get()和set()方法
     */
    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;
    }

}


6.MyExpandableAdapter类

package com.example.expandlistview;

import java.util.List;

import com.example.model.Clazz;
import com.example.model.Student;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
    /**
     *构建自己的适配器,继承BaseExpandableListAdapter
     */
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;
    }
    /**
     *返回一级类的集合的数目,mClazzs为一级菜单类的集合
     */
    @Override
    public int getGroupCount() {
        // TODO Auto-generated method stub
        return mClazzs.size();
    }
    /**
     *返回二级级类的集合的数目,
      *Clazzs.get(groupPosition).getStudents()为了得到二级菜单类的集合
     */
    @Override
    public int getChildrenCount(int groupPosition) {
        // TODO Auto-generated method stub
        return mClazzs.get(groupPosition).getStudents().size();
    }
    //返回分组的索引
    @Override
    public Object getGroup(int groupPosition) {
        // TODO Auto-generated method stub
        return groupPosition;
    }
    /**
     *返回childPosition的索引,child为二级菜单
     */
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return childPosition;
    }
    /**
     *返回groupPosition的索引,一级菜单
     */
    @Override
    public long getGroupId(int groupPosition) {
        // TODO Auto-generated method stub
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return childPosition;
    }
    /**
     *暂时未默认状态
     */
    @Override
    public boolean hasStableIds() {
        // TODO Auto-generated method stub
        return false;
    }
    "******精髓的部分*****"
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        //得到以菜单的view视图,传入xml文件
        convertView=mInflater.inflate(R.layout.item_clazz, null);
        //另外注意本例中未使用ViewHolder来增加性能,所以直接在布局中得到想要的值
        TextView textViewClazzName=(TextView) convertView.findViewById(R.id.textview_clazz_name);
        TextView textViewClazzNum=(TextView) convertView.findViewById(R.id.textview_clazz_num);
        TextView textViewClazzStudent=(TextView) convertView.findViewById(R.id.textview_clazz_student);
        //创建一级菜单类,然后用position得到单个对象
        Clazz clazz=mClazzs.get(groupPosition);
        //然后分别用每条给View添加需要的值
        textViewClazzName.setText(clazz.getClazzName());
        textViewClazzNum.setText(clazz.getClazzNum());
        textViewClazzStudent.setText(""+clazz.getStudents().size());
        //最后别忘记返回
        return convertView;
    }
    /**
     *二级菜单的convertView
     */
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
            ViewGroup parent) { 
            //传入二级菜单的布局
                                convertView=mInflater.inflate(R.layout.item_student, null);
        //另外注意本例中未使用ViewHolder来增加性能,所以直接在布局中得到想要的值
        TextView textViewStudntName=(TextView) convertView.findViewById(R.id.textView_student_name);
        TextView textViewStudntAge=(TextView) convertView.findViewById(R.id.textView_student_age);
        TextView textViewStudntSex=(TextView) convertView.findViewById(R.id.textView_student_sex);
        //创建二级菜单类,然后用position得到单个对象,先得到一级菜单中的集合,然后它的position得到二级菜单对象
        Student student=mClazzs.get(groupPosition).getStudents().get(childPosition);
        //然后为赋值
        textViewStudntName.setText(student.getStudentName());
        textViewStudntAge.setText(student.getAge());
        textViewStudntSex.setText(student.getSex());
        //不要忘了最后的返回值
        return convertView;
    }
    //判断当前的二级菜单是否被选中
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return false;
    }

}


7.MainActivity主类

package com.example.expandlistview;

import java.util.ArrayList;
import java.util.List;

import com.example.model.Clazz;
import com.example.model.Student;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;

public class MainActivity extends Activity {
    //得到自己的适配器对象
    private MyExpandableAdapter mExpandable;
    //得到ExpandableListView(可张开的视图对象)
    private ExpandableListView mExpandListView;
    //定义一级菜单的集合
    private List<Clazz> mClazzs;
    //得到内容填充器,设置为全局变量,为了方便使用
    private LayoutInflater mInflater;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //传入的布局为含有ExpandableListView的布局
        setContentView(R.layout.activity_main);
        //从布局中得到ExpandableListView的id
        mExpandListView=(ExpandableListView) findViewById(R.id.expandableView);
        //初始化数据
        initData();
        //得到内容填充器
        mInflater=getLayoutInflater();
        //获取自己定义的适配器对象,并通过构造器传入参数
        mExpandable=new MyExpandableAdapter(mClazzs, mInflater);
        //添加适配器
        mExpandListView.setAdapter(mExpandable);
    }

    private void initData(){
        //给一级菜单的集合初始化
        mClazzs=new ArrayList<Clazz>();
        //创建一级菜单类的对象
        Clazz clazz1=new Clazz("1班","201501");
        //给二级菜单的集合初始化
        List<Student> student1=new ArrayList<Student>();
        //传入值
        Student xiaoming=new Student("小明","18","男","我是小明");
        Student xiaohong=new Student("晓红","17","女","我是小红");
        Student xiaoli=new Student("小李","19","男","我是小李");
        Student xiaowang=new Student("小明","24","女","我是小王");
        //添加对象
        student1.add(xiaoming);
        student1.add(xiaohong);
        student1.add(xiaoli);
        student1.add(xiaowang);
        //将二级菜单的集合传入一级菜单
        clazz1.setStudents(student1);
        //给一级菜单的集合添加对象
        mClazzs.add(clazz1);

        Clazz clazz2=new Clazz("2班","201502");
        List<Student> student2=new ArrayList<Student>();
        Student zhangsan=new Student("张三","18","男","我是张三");
        Student lisi=new Student("李四","17","女","我是小李");
        Student wangwu=new Student("王五","19","男","我是小王");
        Student zhaoliu=new Student("赵六 ","24","女","我是小赵");
        student2.add(zhangsan);
        student2.add(lisi);
        student2.add(wangwu);
        student2.add(zhaoliu);
        clazz2.setStudents(student2);
        mClazzs.add(clazz2);

        Clazz clazz3=new Clazz("3班","201503");
        List<Student> student3=new ArrayList<Student>();
        Student ligang=new Student("李刚","18","男","我是李刚");
        Student wanggang=new Student("王刚","17","女","我是王刚");
        Student zhaogang=new Student("赵刚","19","男","我是赵刚");
        Student zhangliu=new Student("张柳 ","24","女","我是张柳");
        student3.add(ligang);
        student3.add(wanggang);
        student3.add(zhaogang);
        student3.add(zhangliu);
        clazz3.setStudents(student3);
        mClazzs.add(clazz3);

        Clazz clazz4=new Clazz("4班","201504");
        List<Student> student4=new ArrayList<Student>();
        Student liqiang=new Student("李强","18","男","我是李强");
        Student wangqiang=new Student("王强","17","女","我是王强");
        Student zhaoqiang=new Student("赵强","19","男","我是赵强");
        Student zhangqiang=new Student("张强 ","24","女","我是张强");
        student4.add(liqiang);
        student4.add(wangqiang);
        student4.add(zhaoqiang);
        student4.add(zhangqiang);
        clazz4.setStudents(student4);
        mClazzs.add(clazz4);

    }

}


8.展示

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: