ListView与GridView

1、ListView

效果演示:
这里写图片描述

第一步 UI界面与ListView中Item设计

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.admin.vip_06.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="35dp"
        android:orientation="horizontal"
        android:background="@color/color_26c4fd"
        >

        <ImageView
            android:id="@+id/user"
            android:layout_width="35dp"
            android:layout_height="match_parent"
            android:padding="5dp"
            android:src="@drawable/dragon"
            android:background="@drawable/circle"
            />

        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="消息"
            android:textSize="19sp"
            android:gravity="center"
            android:textColor="@color/color_ecf7fe"
            />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:src="@drawable/row"
            />


    </LinearLayout>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:text="搜索"
        android:layout_margin="8dp"
        android:gravity="center"
        android:background="@color/color_f3f3f3"
        />

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </ListView>

</LinearLayout>
<?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="70dp">

    <ImageView
        android:id="@+id/photo"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_margin="10dp"
        android:src="@drawable/bear"
        />

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="123111"
        android:layout_toRightOf="@+id/photo"
        android:textSize="19sp"
        android:layout_marginTop="5dp"
        />

    <TextView
        android:id="@+id/text2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="123111"
        android:layout_below="@id/text1"
        android:layout_toRightOf="@id/photo"
        android:layout_marginTop="5dp"
        android:textSize="19sp"
        />

    <TextView
        android:id="@+id/time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="15:00"
        android:layout_marginTop="15dp"
        android:layout_marginRight="13dp"
        android:textSize="17sp"
        />

</RelativeLayout>

circle.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval"
    android:useLevel="false"
    >

    <solid android:color="@color/color_ecf7fe" />

    <padding
        android:left="2dp"
        android:top="1dp"
        android:right="2dp"
        android:bottom="1dp" />

    <stroke
        android:width="1dp"
        android:color="@android:color/white" />

    <size android:width="50dp"
        android:height="50dp" />

</shape>

第二步 新建User类,存放每个Item的数据

package com.example.admin.vip_06;

public class User {
    private String name;
    private String content;
    private String image;
    private String time;

    public User(String name, String content, String image, String time) {
        this.name = name;
        this.content = content;
        this.image = image;
        this.time = time;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }
}

第三步 设计自己的适配器(Adapter)

package com.example.admin.vip_06;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

public class MyAdapter extends BaseAdapter {
    private List<User> mUserList;
    private Context mContext;

    public MyAdapter(List<User> userList, Context context) {
        mUserList = userList;
        mContext = context;
    }

    @Override
    public int getCount() {
        return mUserList.size();
    }

    @Override
    public Object getItem(int position) {
        return mUserList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MyHodler mMyHodler;

        if(convertView == null) {
            convertView = View.inflate(mContext, R.layout.list_layout, null);
            mMyHodler = new MyHodler();
            mMyHodler.mImageView = convertView.findViewById(R.id.photo);
            mMyHodler.mTextView1 = convertView.findViewById(R.id.text1);
            mMyHodler.mTextView2 = convertView.findViewById(R.id.text2);
            mMyHodler.time = convertView.findViewById(R.id.time);
            convertView.setTag(mMyHodler);
        }
        else {
            mMyHodler = (MyHodler) convertView.getTag();
        }

        User user = mUserList.get(position);
        mMyHodler.mTextView1.setText(user.getName());
        mMyHodler.mTextView1.setText(user.getContent());
        mMyHodler.time.setText(user.getTime());

        return convertView;
    }

    class MyHodler {
        TextView mTextView1;
        TextView mTextView2;
        ImageView mImageView;
        TextView time;
    }
}

第四步 初始化控件与传入数据

package com.example.admin.vip_06;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ImageView mUser;
    private ListView mListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();

        List<User> list = initData();

        mListView.setAdapter(new MyAdapter(list, MainActivity.this));
    }

    private List<User> initData() {
        List<User> list = new ArrayList<>();
        for(int i = 0; i < 30; i++) {
            list.add(
                    new User("土豆" + i,
                            "who are you",
                            "" + i,
                            "19:47"));
        }

        return list;
    }

    private void initView() {
        mUser = findViewById(R.id.user);
        mListView = findViewById(R.id.listView);
    }


}

2、GridView

使用方法ListView差别不大,类似于GridLayout布局,不做解释。

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值