0826Android基础Gallery+GridView+Spinner+AutoCompleteTextView+ExpandableListView

进行横向图片的滚动,代码部分除了布局文件和昨天的不一样,也使用适配器,用法和ListView基本一样。几个属性方法:spacing左右离开一段距离,可赋负值
unselectAlpha设置未选中的透明度,传入float值
//布局文件
<Gallery
        android:id="@+id/gallery"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:spacing="20dp"
        android:unselectedAlpha="0.5"></Gallery>

//插入的view
<?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:gravity="center"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/img_fruit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/apple"
        />

    <TextView
        android:id="@+id/tv_fruit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

效果
这里写图片描述

GridView

  使用适配器,用法和ListView相同。他和ListView的区别在于他可以多行显示,而ListView只能单行显示。android:numColumns=”4”,定义4行.
  代码中插入的View和上面一样

//布局文件中
<GridView
    android:id="@+id/gv_fruits"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:numColumns="4"></GridView>

这里写图片描述
  然后捏,昨儿做了个作业,用GridView和ImageView来仿照qq的添加照片多选界面来做个Demo。
   昨晚想了一晚上没想明白,今儿老师给讲了讲。
   在上面例子的前提上做的,难点在于如何点击图片然后让相应的item整个变色。自己想来想去打算用蒙版和设置透明度来做来着,可是点击事件那儿一点思路都没有,无法将点击事件与变化整个item的颜色联系起来。老师的方法让我豁然开朗,并不是只有点击事件才能触发改变,最简单的if_else语句也可以。方法的重点在于用mManagerCheckBox[]数组用作判断是否应该变色,然后通过ImageView的setVisiblity来设置是否可见。
  布局时注意要设置checkbox中:android:focusable=”false”;还有ImageView中android:visibility=”invisible”

  具体布局和代码如下:

//布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
   >

    <CheckBox
        android:id="@+id/checkbox_fruit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:focusable="false"
        android:layout_alignParentRight="true"/>

    <ImageView
        android:id="@+id/img_fruit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/apple"
        android:layout_centerInParent="true"
        android:layout_margin="10dp"/>

    <TextView
        android:id="@+id/tv_fruit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="苹果"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        android:layout_below="@id/img_fruit"
        android:gravity="center"
        android:padding="15dp"
        />
    <ImageView
        android:id="@+id/img_tint"
        android:layout_width="70dp"
        android:layout_height="100dp"
        android:layout_alignLeft="@id/img_fruit"
        android:layout_alignTop="@id/img_fruit"
        android:background="@color/pink"
        android:visibility="invisible"/>

</RelativeLayout>


//适配器
public class FruitsAdapter extends BaseAdapter {
    private List<Fruits> mFruits;
    private LayoutInflater mInflater;
    private boolean[] mManagerCheckBox;

    public FruitsAdapter(List<Fruits> mFruits, LayoutInflater mInflater) {
        this.mFruits = mFruits;
        this.mInflater = mInflater;
        mManagerCheckBox=new boolean[mFruits.size()];
    }

    public void expandClickArea(int position){
        mManagerCheckBox[position]=!mManagerCheckBox[position];
        notifyDataSetChanged();
    }

    @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(final int position, View convertView, ViewGroup parent) {
        ViewHolder vh=null;
        if (convertView==null){
            vh=new ViewHolder();
            convertView=mInflater.inflate(R.layout.fruits_item_gridview,null);
            vh.checkbox_fruit= (CheckBox) convertView.findViewById(R.id.checkbox_fruit);
            vh.img_fruit= (ImageView) convertView.findViewById(R.id.img_fruit);
            vh.tv_fruit= (TextView) convertView.findViewById(R.id.tv_fruit);
            vh.img_tint= (ImageView) convertView.findViewById(R.id.img_tint);
            convertView.setTag(vh);
        }else{
            vh= (ViewHolder) convertView.getTag();
        }
        Fruits fruits=mFruits.get(position);
        vh.tv_fruit.setText(fruits.getName());
        vh.img_fruit.setImageResource(fruits.getImg());
        vh.checkbox_fruit.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mManagerCheckBox[position]=isChecked;
                notifyDataSetChanged();
            }
        });
        vh.checkbox_fruit.setChecked(mManagerCheckBox[position]);
        if(mManagerCheckBox[position]){
            vh.img_tint.setVisibility(View.VISIBLE);
        }else{
            vh.img_tint.setVisibility(View.INVISIBLE);
        }
        return convertView;
    }
    class ViewHolder{
        ImageView img_fruit;
        TextView tv_fruit;
        CheckBox checkbox_fruit;
        ImageView img_tint;
    }
}

//活动
public class MainActivity extends Activity {
    private GridView mGfruits;
    private List<Fruits> mFruits;
    private LayoutInflater mInflater;
    private FruitsAdapter mAdapter;
    private ImageView mImage;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mGfruits = (GridView) findViewById(R.id.gv_fruits);
        mImage= (ImageView) findViewById(R.id.img_fruit);
        mInflater = getLayoutInflater();
        initmFruit();
        mAdapter = new FruitsAdapter(mFruits, mInflater);
//      系统调用适配器中的方法
        mGfruits.setAdapter(mAdapter);
        mGfruits.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mAdapter.expandClickArea(position);
                Toast.makeText(getApplicationContext(),position+1+"",Toast.LENGTH_SHORT).show();

            }
        });
    }

    private void initmFruit() {
        mFruits = new ArrayList<>();
        for (int i=0;i<10;i++) {
            Fruits apple = new Fruits(R.mipmap.apple, "苹果");
            Fruits orange = new Fruits(R.mipmap.orange, "橘子");
            Fruits lemon = new Fruits(R.mipmap.lemon, "柠檬");
            mFruits.add(apple);
            mFruits.add(orange);
            mFruits.add(lemon);
        }
    }
}

这里写图片描述

Spinner+AutoCompleteTextView

  下拉选择+搜索。这次没有用自定义adapter写,用了比较简单的ArrayAdapter来举例的。

//布局
<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="com.example.laowang.android0826.SecondActivity">

   <AutoCompleteTextView
       android:id="@+id/auto_textview"
       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"
        android:layout_centerVertical="true"></Spinner>

</RelativeLayout>

//活动
public class SecondActivity extends Activity {
    private AutoCompleteTextView mAuto_textview;
    private Spinner mSpinner;
//    private ArrayAdapter<String> mAdapter;
    private String[] mData={"layout_linear","layout_relative","layout_frame","view_text","view_img","view_edit","include"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        mAuto_textview= (AutoCompleteTextView) findViewById(R.id.auto_textview);
        mSpinner= (Spinner) findViewById(R.id.spinner);
        ArrayAdapter<String> mAdapter1=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,mData);
        mAuto_textview.setAdapter(mAdapter1);
        ArrayAdapter<String> mAdapter2=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,mData);
        mSpinner.setAdapter(mAdapter2);
    }
}

这里写图片描述

ExpandableListView

  这家伙说白了就是ListView的加强版,简直就是一个ListView中又加了一个ListView。今天写的Demo的效果是,班级view作为外部,学生view作为内部,点开班级后可以看到学生view

//布局文件
<LinearLayout 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/expandablelistview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    ></ExpandableListView>
</LinearLayout>

注:箭头可以点开,类似于qq中的分组。
这里写图片描述

//插入的View
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center|left"
    android:background="@color/blue">
    <TextView
        android:id="@+id/textview_clazz_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="40dp"
        />

    <TextView
        android:id="@+id/textview_clazz_num"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"/>

    <TextView
        android:id="@+id/textview_clazz_students"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"/>
</LinearLayout>


<?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:gravity="center_vertical"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/textview_student_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        />

    <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"
        android:layout_margin="5dp"
        />
</LinearLayout>

//班级类,外侧
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;
    }

    public String getClazzName() {
        return clazzName;
    }

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

    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;
    }
}


//学生类,内部
public class Student {
    private String studentName;
    private String studentAge;
    private String studentSex;

    public Student( String studentName, String studentAge,String studentSex) {
        this.studentSex = studentSex;
        this.studentName = studentName;
        this.studentAge = studentAge;
    }

    public String getStudentName() {
        return studentName;
    }

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

    public String getStudentSex() {
        return studentSex;
    }

    public void setStudentSex(String studentSex) {
        this.studentSex = studentSex;
    }

    public String getStudentAge() {
        return studentAge;
    }

    public void setStudentAge(String studentAge) {
        this.studentAge = studentAge;
    }

}

  这家伙难写就在于他的adapter和数据的初始化上面。因为adapter要写group的和child的,然后child要通过group得到。数据初始化是要记得将内部的数据设置在外部的数据中(外部set[内部])。adapter继承BaseExpandableListAdapter。

//重头戏自定义adapter
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) {
        ViewHolderClazz vhClazz=null;
        if(convertView==null){
            vhClazz=new ViewHolderClazz();
            convertView=mInflater.inflate(R.layout.item_clazz,null);
            vhClazz.textViewClazzName= (TextView) convertView.findViewById(R.id.textview_clazz_name);
            vhClazz.textViewClazzNum= (TextView) convertView.findViewById(R.id.textview_clazz_num);
            vhClazz.textViewClazzStudents= (TextView) convertView.findViewById(R.id.textview_clazz_students);
            convertView.setTag(vhClazz);
        }else{
            vhClazz= (ViewHolderClazz) convertView.getTag();
        }
        Clazz clazz=mClazzs.get(groupPosition);

        vhClazz.textViewClazzName.setText(clazz.getClazzName());
        vhClazz.textViewClazzNum.setText(clazz.getClazzNum());
//       为啥子要+个“”?clazz.getStudents().size()是int类型的,setText中加的是String类型的
        vhClazz.textViewClazzStudents.setText(""+clazz.getStudents().size());
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ViewHolderStudent vhStudent=null;
        if(convertView==null){
            vhStudent=new ViewHolderStudent();
            convertView=mInflater.inflate(R.layout.item_student,null);
            vhStudent.textViewStudentName= (TextView) convertView.findViewById(R.id.textview_student_name);
            vhStudent.textViewStudentAge= (TextView) convertView.findViewById(R.id.textview_student_age);
            vhStudent.textViewStudentSex= (TextView) convertView.findViewById(R.id.textview_student_sex);
            convertView.setTag(vhStudent);
        }else{
            vhStudent= (ViewHolderStudent) convertView.getTag();
        }
//        有点乱,好好看
        Clazz clazz=mClazzs.get(groupPosition);
        List<Student> students=clazz.getStudents();
        Student student=students.get(childPosition);

        vhStudent.textViewStudentName.setText(student.getStudentName());
        vhStudent.textViewStudentAge.setText(student.getStudentAge());
        vhStudent.textViewStudentSex.setText(student.getStudentSex());
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }
    class ViewHolderClazz{
        TextView textViewClazzName;
        TextView textViewClazzNum;
        TextView textViewClazzStudents;

    }
    class ViewHolderStudent{
        TextView textViewStudentName;
        TextView textViewStudentAge;
        TextView textViewStudentSex;
    }
}

//活动
public class MainActivity extends Activity {
    private ExpandableListView mEListView;
    private List<Clazz> mClazzs;
    private MyExpandableAdapter mAdapter;
    private LayoutInflater mInflater;

    public MainActivity() {
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mEListView= (ExpandableListView) findViewById(R.id.expandablelistview);
        initdata();
        mInflater=getLayoutInflater();
        mAdapter=new MyExpandableAdapter(mClazzs,mInflater);
        mEListView.setAdapter(mAdapter);
    }

    private void initdata() {
        mClazzs =new ArrayList<>();

//        creatStudent();
        Clazz clazz1=new Clazz("一班","201501");
        List<Student> students1=new ArrayList<>();
        students1.add(new Student("张一","18","男"));
        students1.add(new Student("张二","19","女"));
        students1.add(new Student("张三","20","男"));
        students1.add(new Student("张四","21","女"));
//      一定要记得把学生添加到班级里
        clazz1.setStudents(students1);
        Clazz clazz2=new Clazz("二班","201502");
        List<Student> students2=new ArrayList<>();
        students2.add(new Student("王一","18","男"));
        students2.add(new Student("王二","19","女"));
        students2.add(new Student("王三","20","男"));
        students2.add(new Student("王四","21","女"));
        clazz2.setStudents(students2);
        Clazz clazz3=new Clazz("三班","201503");
        List<Student> students3=new ArrayList<>();
        students3.add(new Student("李一","18","男"));
        students3.add(new Student("李二","19","女"));
        students3.add(new Student("李三","20","男"));
        students3.add(new Student("李四","21","女"));
        clazz3.setStudents(students3);
        Clazz clazz4=new Clazz("四班","201504");
        List<Student> students4=new ArrayList<>();
        students4.add(new Student("刘一","18","男"));
        students4.add(new Student("刘二","19","女"));
        students4.add(new Student("刘三","20","男"));
        students4.add(new Student("刘四","21","女"));
        students4.add(new Student("李一","18","男"));
        students4.add(new Student("李二","19","女"));
        students4.add(new Student("李三","20","男"));
        students4.add(new Student("李四","21","女"));
        clazz4.setStudents(students4);
        mClazzs.add(clazz1);
        mClazzs.add(clazz2);
        mClazzs.add(clazz3);
        mClazzs.add(clazz4);
    }

    private void creatStudent() {

    }

}

  最后做出来的效果,用老师的一句话说就是:透着一股浓浓的山寨味儿╮(╯▽╰)╭
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值