首页 » Java » Android组件学习笔记(ListView结构操作)

Android组件学习笔记(ListView结构操作)

原文 http://blog.csdn.net/Changing_with_hard/article/details/78343522

2017-10-25 18:20:30阅读(284)

跟着教程学习中,记下笔记

这次实现一个简单类似电话簿的东西,不过使用了内部类,和一些比较快捷的结构操作

具体参数可自主调整

实现图片:

<a href=Android组件学习笔记(ListView结构操作)" src="http://img.blog.csdn.net/20171025170313388?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvQ2hhbmdpbmdfd2l0aF9oYXJk/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

代码实现:

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>



最新发布

CentOS专题

关于本站

5ibc.net旗下博客站精品博文小部分原创、大部分从互联网收集整理。尊重作者版权、传播精品博文,让更多编程爱好者知晓!

小提示

按 Ctrl+D 键,
把本文加入收藏夹