使用BaseAdapter制作图2的ListView
- 效果图
MainActivity.java
package com.example.listview;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private String[] titles = {"毡帽系列","蜗牛系列","小蜜蜂系列",
"毡帽系列","蜗牛系列","小蜜蜂系列"};
private String[] descs = {"此系列服装有点cute,像不像小车夫。",
"宝宝变成了蜗牛,爬啊爬啊","小蜜蜂,啪哨,飞到西,飞到东",
"此系列服装有点cute,像不像小车夫。",
"宝宝变成了蜗牛,爬啊爬啊","小蜜蜂,啪哨,飞到西,飞到东"};
private int[] ivs = {R.drawable.a,R.drawable.b,R.drawable.c,
R.drawable.a,R.drawable.b,R.drawable.c};
private ListView mlistView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mlistView = (ListView)findViewById(R.id.mlistView);
MyBaseAdapter myBaseAdapter = new MyBaseAdapter();
mlistView.setAdapter(myBaseAdapter);
}
class MyBaseAdapter extends BaseAdapter{
@Override
public int getCount() {
return titles.length;
}
@Override
public Object getItem(int i) {
return titles[i];
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View convertview, ViewGroup viewGroup) {
View view = View.inflate(MainActivity.this,R.layout.list_item,null);
TextView title = (TextView) view.findViewById(R.id.title);
TextView desc = (TextView) view.findViewById(R.id.desc);
ImageView iv = (ImageView) view.findViewById(R.id.iv);
title.setText(titles[i]);
desc.setText(descs[i]);
iv.setBackgroundResource(ivs[i]);
return view;
}
}
}
activity_main.xml
<androidx.constraintlayout.widget.ConstraintLayout 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">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/mlistView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#cbcbcb"
android:dividerHeight="2px">
ListView>
LinearLayout>
androidx.constraintlayout.widget.ConstraintLayout>
list_item.xml
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
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"
android:orientation="horizontal">
<ImageView
android:id="@+id/iv"
android:layout_marginLeft="10dp"
android:layout_width="120dp"
android:layout_height="match_parent"
/>
<LinearLayout
android:layout_marginRight="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:text="毡帽系列"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp" />
<TextView
android:id="@+id/desc"
android:text="此系列服装有点cute,像不像小车夫。"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp" />
<ImageView
android:layout_gravity="right"
android:id="@+id/icon"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/t"/>
LinearLayout>
LinearLayout>
androidx.constraintlayout.widget.ConstraintLayout>
实现注册界面
MainActivity.java
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
private EditText name;
private EditText pwd;
private EditText pwd2;
private RadioGroup rdg;
private String gender = "男";
private String hobbys;
private CheckBox like_swimming;
private CheckBox like_music;
private CheckBox like_book;
Spinner sp;
private String[] starArray = {"小学", "初中", "中专/高中", "专科", "本科", "硕士", "博士"};
private String education;
public void click(View view) {
name = (EditText) findViewById(R.id.name);
pwd = (EditText) findViewById(R.id.pwd);
pwd2 = (EditText) findViewById(R.id.pwd2);
rdg = (RadioGroup) findViewById(R.id.rdg);
hobbys = new String();
like_swimming = (CheckBox) findViewById(R.id.like_swimming);
like_music = (CheckBox) findViewById(R.id.like_music);
like_book = (CheckBox) findViewById(R.id.like_book);
like_swimming.setOnCheckedChangeListener(this);
like_music.setOnCheckedChangeListener(this);
like_book.setOnCheckedChangeListener(this);
sp = findViewById(R.id.education);
sp.setSelection(5);
sp.setOnItemSelectedListener(new MySelectedListener());
rdg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int checkId) {
if (checkId != R.id.rbtn) {
gender = "女";
} else {
gender = "男";
}
}
});
String data = "用户名: " + name.getText().toString() + " 密码:" + pwd.getText().toString()
+ " 确认密码:" + pwd2.getText().toString() + " 性别: " + gender +
" 学历 " + education + " 爱好 " + hobbys;
Toast.makeText(MainActivity.this, data, Toast.LENGTH_LONG).show();
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
String motion = buttonView.getText().toString();
if (isChecked) {
if (!hobbys.contains(motion)) {
hobbys = hobbys + motion;
}
} else {
if (hobbys.contains(motion)) {
hobbys = hobbys.replace(motion, "");
}
}
}
class MySelectedListener implements AdapterView.OnItemSelectedListener {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
education = starArray[i];
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
}
}
activity_main.xml
<androidx.constraintlayout.widget.ConstraintLayout 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">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="70dp"
android:text="注册"
android:textSize="40dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp">
<EditText
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:textSize="30dp"
android:hint="用户名" />
LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/pwd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:textSize="30dp"
android:hint="密码" />
LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/pwd2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:textSize="30dp"
android:hint="确认密码" />
LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="10dp"
android:text="性别"
android:textSize="30dp" />
<RadioGroup
android:id="@+id/rdg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:textSize="30dp"
android:text="男" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30dp"
android:text="女" />
RadioGroup>
LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="6dp"
android:text="学历"
android:textSize="30dp" />
<Spinner
android:id="@+id/education"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="6dp"
android:entries="@array/education">
Spinner>
LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="6dp"
android:text="爱好"
android:textSize="30dp" />
<CheckBox
android:id="@+id/like_swimming"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="6dp"
android:textSize="30dp"
android:text="游泳" />
<CheckBox
android:id="@+id/like_music"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="6dp"
android:textSize="30dp"
android:text="音乐" />
<CheckBox
android:id="@+id/like_book"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="6dp"
android:textSize="30dp"
android:text="读书" />
LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<Button
android:background="#708090"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30dp"
android:text="注册"
android:onClick="click"/>
LinearLayout>
LinearLayout>
androidx.constraintlayout.widget.ConstraintLayout>