今天观看视频学习了ListView,学习了属性跟点击事件;
需要自己新建一个java继承BaseAdapter
package com.example.yangy.myapplication1111111.ListView; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; import com.example.yangy.myapplication1111111.R; public class MyAdapter extends BaseAdapter { private Context mcontext; private LayoutInflater mLayoutInflater; MyAdapter(Context context){ this.mcontext=context; mLayoutInflater=LayoutInflater.from(context); } @Override public int getCount() { return 10; } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } static class ViewHolder{ public ImageView imageView; public TextView tv1; public TextView tv2; } @Override public View getView(int position, View convertView, ViewGroup parent) { //列表每一行长什么样子 ViewHolder holder=null; if(convertView==null){ convertView=mLayoutInflater.inflate(R.layout.layout_list_item,null); holder=new ViewHolder(); holder.imageView= (ImageView) convertView.findViewById(R.id.iv); holder.tv1= (TextView) convertView.findViewById(R.id.tv_title); holder.tv2= (TextView) convertView.findViewById(R.id.tv_time); convertView.setTag(holder); }else { holder= (ViewHolder) convertView.getTag(); } //给控件赋值 holder.tv1.setText("这是标题"); holder.tv2.setText("这是时间2020-2-5"); //Glide.with().load().into(holder.imageView); return convertView; } }
然后在自己新建的ListView中的.xml中写布局
<ListView
android:id="@+id/lv_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:listSelector="@drawable/but4"//点击时候的样式>
然后在自己新建的Activity中的.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:orientation="horizontal" 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.yangy.myapplication1111111.ListView.MyAdapter"> <ImageView android:id="@+id/iv" android:layout_width="100dp" android:layout_height="100dp" android:background="#000" android:scaleType="centerCrop"/> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:layout_marginTop="20dp"> <TextView android:id="@+id/tv_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="标题" android:layout_marginLeft="20dp" android:textColor="#000" android:textSize="20sp"/> <TextView android:id="@+id/tv_time" android:layout_marginTop="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="时间2020-02-05" android:layout_marginLeft="20dp" android:textColor="#000" android:textSize="20sp"/> </LinearLayout> </LinearLayout>