跟着教程学习中,记下笔记
这次实现一个简单类似电话簿的东西,不过使用了内部类,和一些比较快捷的结构操作
具体参数可自主调整
实现图片:
代码实现:
package com.example.hp.test_listview;
import android.graphics.drawable.Drawable;
import android.support.v7.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;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ArrayList<student> ListDate=new ArrayList();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListDate.add(new student("小张",true));
ListDate.add(new student("小李",true));
ListDate.add(new student("小江",false));
ListDate.add(new student("小香",true));
myAdapter ListAdapter =new myAdapter();
ListView listView =(ListView)findViewById(R.id.id_listview);
listView.setAdapter(ListAdapter);
}
private class student
{
public String name;
public boolean sex;
public student(String name,boolean sex)
{
this.name=name;
this.sex=sex;
}
}
private class myAdapter extends BaseAdapter
{
Drawable[] icons=new Drawable[2];
public myAdapter()
{
icons[0]=getDrawable(R.drawable.ic_nv);
icons[1]=getDrawable(R.drawable.ic_nan);
}
@Override
public int getCount() {
return ListDate.size();
}
@Override
public Object getItem(int position) {
return ListDate.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView,
ViewGroup parent)
{
if(convertView==null)
{
convertView=getLayoutInflater().
inflate(R.layout.list_item_student,parent,false);
}
student date=(student)getItem(position);//得到对象
TextView textView=(TextView)convertView.
findViewById(R.id.id_textview);
textView.setText(date.name);
ImageView imageView=(ImageView)convertView.
findViewById(R.id.id_imageview);
if(date.sex)
imageView.setImageDrawable(icons[1]);
else
imageView.setImageDrawable(icons[0]);
return convertView;
}
}
}
有两个布局文件:
一个总的,一个只是做出摸板,代码:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="com.example.hp.test_listview.MainActivity">
<ListView
android:id="@+id/id_listview"
android:layout_width="368dp"
android:layout_height="495dp"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp" />
</android.support.constraint.ConstraintLayout>
和:
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:id="@+id/id_imageview"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:srcCompat="@mipmap/ic_launcher" />
<TextView
android:id="@+id/id_textview"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="TextView" />
</LinearLayout>