• Android开发-ListView的使用
  • 林丹红 发表于 2016/3/23 18:07:00 | 分类标签: Android开发 ListView
  •  
    • ListView在android程序中比较常用,在此做一下简单的总结
    • 内容:自定义ListView, 自定义Adapter,ListView滚动事件的应用!

     

    项目结构如下图

     

    ListView的使用:

    • 建立一个listView的布局文件,命名为:item_layout.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"
        android:paddingLeft="16dp"
        android:paddingTop="16dp">
        <LinearLayout
            android:gravity="center_vertical"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="60dp">
            <ImageView
                android:id="@+id/imageView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@mipmap/ic_launcher" />
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="16dp"
                android:orientation="vertical">
                <TextView
                    android:id="@+id/textView1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Hello World" />
                <TextView
                    android:id="@+id/textView2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:text="Hello World" />
            </LinearLayout>
            <Button
                android:id="@+id/button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:text="Button" />
        </LinearLayout>
    </LinearLayout>

    大致视图如下

     

    • 创建一个自定义ListView的布局文件:activity_main.xml
    <?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"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.example.linux.listviewscrolltest.MainActivity">
    
        <com.example.linux.listviewscrolltest.linux.CustomListView
            android:id="@+id/listView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        </com.example.linux.listviewscrolltest.linux.CustomListView>
    </LinearLayout>
    • 建立一个Entity,命名为:PersonEntiry.java,用于显示
    package com.example.linux.listviewscrolltest.linux;
    
    /**
     * Created by Linux on 2016/3/13.
     */
    public class PersonEntiry {
        private int imageId;
        private String username;
        private String password;
        private String button;
    
        public int getImageId() {
            return imageId;
        }
    
        public void setImageId(int imageId) {
            this.imageId = imageId;
        }
    
        public String getUsername() {
            return username;
        }
    
        public void setUsername(String username) {
            this.username = username;
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    
        public String getButton() {
            return button;
        }
    
        public void setButton(String button) {
            this.button = button;
        }
    }
    • 建立一个Holder,命名为:PersonViewHolder.java,对应于布局文件中的四个组件
    package com.example.linux.listviewscrolltest.linux;
    
    import android.widget.Button;
    import android.widget.ImageView;
    import android.widget.TextView;
    
    /**
     * Created by Linux on 2016/3/13.
     */
    public class PersonViewHolder {
        ImageView imageView;
    
        TextView textView1;
    
        TextView textView2;
    
        Button button;
    }
    • 创建一个ListView的适配器,命名为PersonAdapter.java, 重写父类的四个方法:
    package com.example.linux.listviewscrolltest.linux;
    
    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.Button;
    import android.widget.ImageView;
    import android.widget.TextView;
    
    import com.example.linux.listviewscrolltest.R;
    
    import java.util.List;
    
    /**
     * Created by 胡红翔 on 2016/3/13.
     * function: 自定义ListView的适配器
     *
     */
    public class PersonAdapter extends BaseAdapter {
        List<PersonEntiry> personEntiries;
        LayoutInflater inflater;
    
        public PersonAdapter(Context context, List<PersonEntiry> personEntiries) {
            this.personEntiries = personEntiries;
            this.inflater = LayoutInflater.from(context);
        }
    
        @Override
        public int getCount() {
            return personEntiries.size();
        }
    
        @Override
        public Object getItem(int position) {
            return personEntiries.get(position);
        }
    
        @Override
        public long getItemId(int position) {
            return position;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            PersonEntiry entity = personEntiries.get(position);
            PersonViewHolder holder;
            if (convertView == null) {
                holder = new PersonViewHolder();
                convertView = inflater.inflate(R.layout.item_layout, null);
                holder.imageView = (ImageView) convertView.findViewById(R.id.imageView);
                holder.textView1 = (TextView) convertView.findViewById(R.id.textView1);
                holder.textView2 = (TextView) convertView.findViewById(R.id.textView2);
                holder.button = (Button) convertView.findViewById(R.id.button);
                convertView.setTag(holder);
            } else {
                holder = (PersonViewHolder) convertView.getTag();
            }
            holder.imageView.setImageResource(entity.getImageId());
            holder.textView1.setText(entity.getUsername());
            holder.textView2.setText(entity.getPassword());
            holder.button.setText(entity.getButton());
            return convertView;
        }
    }
    •  创建一个自定义的ListView:CustomListView,添加滚动事件:
    package com.example.linux.listviewscrolltest.linux;
    
    import android.content.Context;
    import android.util.AttributeSet;
    import android.util.Log;
    import android.widget.AbsListView;
    import android.widget.ListView;
    
    /**
     * Created by Linux on 2016/3/13.
     * function: 自定义的ListView
     */
    public class CustomListView extends ListView implements AbsListView.OnScrollListener {
        private static final String TAG = "MainActivity";
    
        public CustomListView(Context context) {
            super(context);
            setOnScrollListener(this);
        }
    
        public CustomListView(Context context, AttributeSet attrs) {
            super(context, attrs);
            setOnScrollListener(this);
        }
    
        public CustomListView(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            setOnScrollListener(this);
        }
    
        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            // view应该就是可见的View的集合
            Log.i(TAG, "state change: " + scrollState + ", view size: " + view.getChildCount());
        }
    
        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            Log.i(TAG, "on scroll: " + firstVisibleItem + ", visible: " + visibleItemCount + " , " + totalItemCount);
        }
    }
    •  最后在MainActivity中使用自定义的ListView:
    package com.example.linux.listviewscrolltest;
    
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    
    import com.example.linux.listviewscrolltest.linux.CustomListView;
    import com.example.linux.listviewscrolltest.linux.PersonAdapter;
    import com.example.linux.listviewscrolltest.linux.PersonEntiry;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class MainActivity extends AppCompatActivity {
    
        private static final String TAG = "MainActivity";
        private CustomListView listView;
        private PersonAdapter personAdapter;
        private List<PersonEntiry> personEntiries = new ArrayList<>();
        private PersonEntiry entity;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            listView = (CustomListView) findViewById(R.id.listView);
    
            setData();
            initListView();
        }
    
        // 增加数据
        private void setData() {
            for (int i = 0; i < 10; i++) {
                entity = new PersonEntiry();
                entity.setImageId(R.mipmap.ic_launcher);
                entity.setUsername("linux: " + i);
                entity.setPassword("huhx: " + i);
                entity.setButton("刘力: " + i);
                personEntiries.add(entity);
            }
        }
    
        // 生成ListView
        private void initListView() {
            personAdapter = new PersonAdapter(this, personEntiries);
            listView.setAdapter(personAdapter);
        }
    }
    •  为测试方便,附上AndroidManifest.xml
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.linux.listviewscrolltest">
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            <activity android:name=".MainActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    
    </manifest>

     

    实现过程

    ListView中比较重要的几个要素:

    • 数据:
    • 适配器adapter:
    • listview:

    然后,根据适配器的种类,我们可以把listview分成三种,有ArrayAdapter,SimpleAdapter和SimpleCursorAdapter,而最后一种是专门与数据库连接用的。

  • 请您注意

    ·自觉遵守:爱国、守法、自律、真实、文明的原则

    ·尊重网上道德,遵守《全国人大常委会关于维护互联网安全的决定》及中华人民共和国其他各项有关法律法规

    ·严禁发表危害国家安全,破坏民族团结、国家宗教政策和社会稳定,含侮辱、诽谤、教唆、淫秽等内容的作品

    ·承担一切因您的行为而直接或间接导致的民事或刑事法律责任

    ·您在编程中国社区新闻评论发表的作品,本网站有权在网站内保留、转载、引用或者删除

    ·参与本评论即表明您已经阅读并接受上述条款

  • 感谢本文作者
  • 作者头像
  • 昵称:林丹红
  • 加入时间:2014/6/1 14:41:00
  • TA的签名
  • 这家伙很懒,虾米都没写
  • +进入TA的空间
  • 以下内容也很赞哦
分享按钮