Android studio :适配器控件

Adapter:
Layout部分:

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context=".MainActivity">
    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>

Java部分:

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String[] strs = {"项目1","项目2","项目3","项目4","项目5"};
        ArrayAdapter<String> adapter = new ArrayAdapter<String>
                (this,android.R.layout.simple_expandable_list_item_1,strs);
        ListView list_test =  findViewById(R.id.list);
        list_test.setAdapter(adapter);
    }
}

在这里插入图片描述
Spinner:
Layout部分:

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context=".MainActivity">
    <Spinner
        android:id="@+id/spacer1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <Spinner
        android:id="@+id/spacer2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:spinnerMode="dialog"/>
</LinearLayout>

Java部分:

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class MainActivity extends AppCompatActivity {
    Spinner sp1,sp2;
    String str[] = new String[]{"数学","英语","语文","音乐","美术","体育"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sp1 = findViewById(R.id.spacer1);
        sp2 = findViewById(R.id.spacer2);
        ArrayAdapter adapter = new ArrayAdapter<String>
                (this,android.R.layout.simple_expandable_list_item_1,str);
        sp1.setAdapter(adapter); 
        sp2.setAdapter(adapter);
    }
}

在这里插入图片描述
在这里插入图片描述
ListView2:
Layout部分:
activity_main.xml

<?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">
    <ImageView
        android:layout_marginLeft="10dp"
        android:id="@+id/person_image"
        android:layout_width="50dp"
        android:layout_height="50dp" />
    <TextView android:id="@+id/person_name"
        android:layout_marginLeft="10dp"
        android:textSize="20sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_toRightOf="@id/person_image"/>
    <CheckBox android:id="@+id/check_Box"
        android:layout_marginRight="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true"
        android:buttonTint="#8B8878"/>
</RelativeLayout>

item_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/selected_all"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="全选"
            android:textSize="24sp"/>
        <Button
            android:id="@+id/unselected_all"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="全不选"
            android:textSize="24sp"/>
        <Button
            android:id="@+id/reverse_select"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="反选"
            android:textSize="24sp"/>
    </LinearLayout>
    <ListView
        android:id="@+id/list_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </ListView>
</LinearLayout>

Java部分:
MainActivity.java

import android.app.Activity;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

public class MainActivity extends AppCompatActivity {
    private List<Person> mPersonList = new ArrayList<>(); 
    private ListView mListView; 
    private PersonAdapter mAdapter; 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initPerson();
        mAdapter = new PersonAdapter(MainActivity.this, mPersonList);
        mListView = (ListView) findViewById(R.id.list_view);
        mListView.setAdapter(mAdapter); 
        Button button1 = (Button) findViewById(R.id.selected_all);
        Button button2 = (Button) findViewById(R.id.unselected_all);
        Button button3 = (Button) findViewById(R.id.reverse_select);
        button1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                selectAll(v);
            }
        });
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                unSelectAll(v);
            }
        });
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                reverseSelect(v);
            }
        });
    }
    public void selectAll(View view){
        for(Person person : mPersonList){
            person.setCheck(true);
        }
        mAdapter.notifyDataSetChanged();
    }
    public void unSelectAll(View view){
        for(Person person : mPersonList){
            person.setCheck(false);
        }
        mAdapter.notifyDataSetChanged();
    }
    //反选
    public void reverseSelect(View view) {
        for(Person person : mPersonList) {
            if (person.getIsCheck() == false) {
                person.setCheck(true);
            } else {
                person.setCheck(false);
            }
        }
        mAdapter.notifyDataSetChanged();
    }
    //初始化实体类
    private void initPerson() {
        for(int i=0; i<5; i++) {
            Person tom = new Person("照相机", R.drawable.p1);
            mPersonList.add(tom);
            Person kate = new Person("时钟", R.drawable.p2);
            mPersonList.add(kate);
            Person ross = new Person("游戏", R.drawable.p3);
            mPersonList.add(ross);
        }
    }
}

Person.java

public class Person {
    private String name;//定义字符串
    private int imageId;//定义图片ID
    private boolean isCheck;//电影复选框组件对象
    //构造方法初始化
    public Person(String name, int imageId) {
        this.name = name;
        this.imageId = imageId;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getImageId() {
        return imageId;
    }
    public void setImageId(int imageId) {
        this.imageId = imageId;
    }
    public boolean getIsCheck() {
        return isCheck;
    }
    public void setCheck(boolean check) {
        isCheck = check;
    }
}

PersonAdapter.java

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
public class PersonAdapter extends BaseAdapter {
    private Context mContext;//设备上下文
    private List<Person> mList;//包含实体类的列表
    private ViewHolder mViewHolder;//视图缓冲
    //构造方法初始化
    public PersonAdapter(Context mContext, List<Person> mList) {
        this.mContext = mContext;
        this.mList = mList;
    }
    @Override
    public int getCount() {
        return mList.size();//返回列表的长度
    }
    @Override
    public Object getItem(int i) {
        return mList.get(i);//返回元素项
    }
    @Override
    public long getItemId(int i) {
        return i;//返回元素ID
    }
    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        final Person person = mList.get(i);//获取到具体项
        if(view == null) {//如果视图不为空再进行创建
            //用LayouInflater加载布局,传给布局对象view
            //用view找到三个控件,存放在viewHolder中,再把viewHolder储存到View中
            //完成了把控件展示在ListView的步骤
            view = LayoutInflater.from(mContext).inflate(R.layout.item_layout,
                    viewGroup, false);
            mViewHolder = new ViewHolder();//初始化viewHolder对象
            mViewHolder.checkBox = view.findViewById(R.id.check_Box);
            mViewHolder.personImage = view.findViewById(R.id.person_image);
            mViewHolder.personName = view.findViewById(R.id.person_name);
            view.setTag(mViewHolder);//将ViewHolder对象加入到Tag中
        }else {
            mViewHolder = (ViewHolder) view.getTag();
        }
        //复选框设置改变监听事件
        mViewHolder.checkBox.setOnCheckedChangeListener
                (new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                        person.setCheck(b);//设置状态
                    }
                });
        //设置具体项的内容
        mViewHolder.personName.setText(person.getName());
        mViewHolder.personImage.setImageResource(person.getImageId());
        mViewHolder.checkBox.setChecked(person.getIsCheck());
        return view;//将设置好的视图进行返回
    }
    //holder类
    class ViewHolder {
        ImageView personImage;
        TextView personName;
        CheckBox checkBox;
    }
}

在这里插入图片描述
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值