Android

关注公众号 jb51net

关闭
首页 > 软件编程 > Android > Android Spinner

Android Spinner和GridView组件的使用示例

作者:小皮猪

Spinner其实是一个列表选择框,不过Android的列表选择框并不需要显示下拉列表,而是相当于弹出一个菜单供用户选择,GridView是一个在二维可滚动的网格中展示内容的控件。网格中的内容通过使用adapter自动插入到布局中

一. 概述:

        Spinner是我们所熟悉的下拉框列表。与ListView类似,我们必须为Spinner对象指定一个Adapter。我们从Spinner的简单用法开始讲述。

二. 实现

MainActivity.java

package com.example.demo03_29;
 
import androidx.appcompat.app.AppCompatActivity;
 
import android.os.Bundle;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
 
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
    TextView choice;
    ArrayAdapter<CharSequence> adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        choice=(TextView) this.findViewById(R.id.choice);
 
        Spinner spinner=(Spinner)this.findViewById(R.id.sp1);
        adapter=ArrayAdapter.createFromResource(this, R.array.habit,
                android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(this);
    }
 
 
    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
        choice.setText(adapter.getItem(i));
    }
 
    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {
 
    }
}

在values包下的  string.xml

<resources>
    <string name="app_name">Demo03-29</string>
<string-array name="habit">
    <item>10公里夜跑</item>
    <item>爬山</item>
    <item>游泳</item>
    <item>吃饭</item>
    <item>打代码</item>
</string-array>
</resources>

activity_main.xml 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
 
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="你喜欢的运动"/>
        <Spinner
            android:id="@+id/sp1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </LinearLayout>
 
    <View
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:background="#05ADF8"/>
 
    <TextView
        android:id="@+id/choice"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        />
</LinearLayout>

sipnner_down.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">
 
    <ImageView
        android:id="@+id/p01"
        android:layout_width="64dp"
        android:layout_height="64dp"
        />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    <TextView
        android:id="@+id/t1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <TextView
        android:id="@+id/t2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    </LinearLayout>
 
</LinearLayout>

         这个布局比较简单,就是在LinearLayout中放置一个LinearLayout、一个分隔组件用的View和一个显示所选结果的TextView。在其中嵌套的LinearLayout中放置一个用于提示用的TextView和下拉框Spinner。

三. 美化

MyAdapter.java 

package com.example.demo03_29;
 
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
 
import androidx.annotation.NonNull;
 
public class MyArrayApater extends ArrayAdapter {
    private LayoutInflater minflater;
    String[] titles;
    String[] desc={
            "集成工具",
            "高速保存,随时存储",
            "优质资源,尽在迅雷",
            "集成开发工具",
            "学习神器"
    };
    int[] photos={R.drawable.p1,R.drawable.p2,R.drawable.p3,R.drawable.p4,R.drawable.p5};
 
    public MyArrayApater(@NonNull Context context, int resource, CharSequence[] objects) {
        super(context, resource, objects);
 
        minflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        titles=context.getResources().getStringArray(R.array.habit);
    }
    @Override
    public int getCount(){
        return titles.length;
    }
    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent){
        View v;
        v=minflater.inflate(R.layout.spinner_down,parent,false);
 
        ImageView iv=(ImageView)v.findViewById(R.id.p01);
        iv.setImageResource(photos[position]);
        TextView tv01=(TextView) v.findViewById(R.id.t1);
        tv01.setText(titles[position]);
        TextView tv02=(TextView) v.findViewById(R.id.t2);
        tv02.setText(desc[position]);
        return v;
    }
}

修改 MainActivity.java

四. GridView

       GridView以二维表格的方式显示数据,若数据比较多,该组件将提供垂直滚动条。我们用一个列子来说明GridView的使用方法。

activity_main.xml 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
 
    <GridView
        android:id="@+id/gr1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:numColumns="4"
        />
 
</LinearLayout>

 ImgeAdapter.java

package com.example.demo0330;
 
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
 
public class ImageAdapter extends BaseAdapter {
    private int[] images={
            R.drawable.p1, R.drawable.p2, R.drawable.p3, R.drawable.p4,
            R.drawable.p1, R.drawable.p2, R.drawable.p3, R.drawable.p4,
            R.drawable.p1, R.drawable.p2, R.drawable.p3, R.drawable.p4,
            R.drawable.p1, R.drawable.p2, R.drawable.p3, R.drawable.p4,
            R.drawable.p1, R.drawable.p2, R.drawable.p3, R.drawable.p4,
 
    };
    private Context context;
    public ImageAdapter(Context context){
        this.context=context;
    }
 
 
    @Override
    public int getCount() {
        return images.length;
    }
 
    @Override
    public Object getItem(int position) {
        return images[position];
    }
 
    @Override
    public long getItemId(int position) {
        return position;
    }
 
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView==null){
            imageView=new ImageView(context);
            int width= GridView.LayoutParams.MATCH_PARENT;
            int heght= GridView.LayoutParams.MATCH_PARENT;
            imageView.setLayoutParams(new ViewGroup.LayoutParams(width,heght));
            imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        }else{
            imageView=(ImageView) convertView;
        }
        imageView.setImageResource(images[position]);
        return imageView;
    }
}

MainActivity.java

package com.example.demo0330;
 
import androidx.appcompat.app.AppCompatActivity;
 
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;
 
public class MainActivity extends AppCompatActivity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        GridView gridView=(GridView) findViewById(R.id.gr1);
        gridView.setAdapter(new ImageAdapter(this));
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                Toast.makeText(MainActivity.this,"现在点击的是位置在"+(position+1)+"张照片",Toast.LENGTH_LONG).show();
            }
        });
    }
}

效果图:

到此这篇关于Android Spinner和GridView组件的使用示例的文章就介绍到这了,更多相关Android Spinner内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文