ListView Simple Example - news page demo

Is a list ListView control is one kind of Android system provides us with the display.
Use it to display a list of our common form of inherited from the abstract class AdapterView.

Common Adapter Introduction

  • ArrayAdapter: Simple, easy to use Adapter, an array of data used to bind the item to the list as a data source. Support for generics operations
  • SimpleAdapter: Compared ArrayAdapter, the more powerful features, every one of them can bind the data source to the item in the view.
  • CursorAdapter: cursor for binding (data extracted from a database) as the data source list items, and related databases, not commonly used.
  • BaseAdapter: This is our frequently used in the actual development, we need to inherit from our own definitions adapter BaseAdapter

News app page demo

The figure is the effect we want to achieve

Here Insert Picture Description

1. written layout

A master file layout listview
added ListView control in the layout, and a specified id match_parent arranged to occupy the entire space ListView

<?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="vertical"
    >
    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/lv_demo"/>

</LinearLayout>

Single item layout file

<?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="wrap_content"
    android:padding="5dp">

    <ImageView
        android:background="@mipmap/ic_launcher_round"
        android:id="@+id/iv_photo"
        android:layout_width="70dp"
        android:layout_height="70dp"/>
    <TextView
        android:id="@+id/tv_title"
        android:textSize="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/iv_photo"
        android:layout_marginLeft="5dp"
        android:text="title"
        android:textColor="#000"/>
    <TextView
        android:id="@+id/tv_type"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="type"
        android:textSize="15dp"
        android:textColor="#6B1F1F"
        android:layout_alignParentRight="true"
        android:layout_alignBottom="@id/iv_photo"/>
</RelativeLayout>

2. Define your own adapter MyAdapter

The definition of an adapter inherited from BaseAdapter
override the constructors and methods getView

public class MyAdapter extends BaseAdapter {
    private Context context;
    private String[] title;
    private String[] type;
    private int[] photo;
    public MyAdapter(Context context, String[] title, String[] type, int[] photo) {
        this.context = context;
        this.title = title;
        this.type = type;
        this.photo = photo;
    }
    @Override
    public int getCount() {
        return title.length;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView ==null){
            convertView=View.inflate(this.context,R.layout.item_demo,null);
             holder = new ViewHolder();
            holder.title=(TextView) convertView.findViewById(R.id.tv_title);
            holder.type=(TextView) convertView.findViewById(R.id.tv_type);
            holder.photo=(ImageView) convertView.findViewById(R.id.iv_photo);
            convertView.setTag(holder);//*将holder和convertview绑定*//*
        }else {
            holder=(ViewHolder) convertView.getTag();

        }
        holder.title.setText(title[position]);
        holder.type.setText(type[position]);
        holder.photo.setImageResource(photo[position]);
        return convertView;
    }
    public static class ViewHolder{//*ViewHolder类是MyAdapter的静态成员*//
        TextView title;
        TextView type;
        ImageView photo;
    }
}

inflate method described herein has three parameters:
The first parameter is a context, is the current Activity, the second parameter is filled root view, each of the data is displayed in the top view; whether or not the third parameter is loaded binding view to the root view.

3. write data

In Mainactivity written three kinds of data, title, type, photo
course, this data should take networking, here a few handwritten data it!

public class MainActivity extends AppCompatActivity {

    private ListView lv_demo;
    private String[] title={"2019年国庆大阅兵","大众汽车最新报价","十九大在北京隆重召开"};
    private String[] type={"军事","经济","政治"};
    private int[] photo={R.mipmap.p1,R.mipmap.p2,R.mipmap.p3};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv_demo = (ListView) findViewById(R.id.lv_demo);
        MyAdapter myAdapter = new MyAdapter(this,title,type,photo);
        lv_demo.setAdapter(myAdapter);/*显示*/
    }
}

run! ! ! ! ! !
Here Insert Picture Description

Published 13 original articles · won praise 50 · views 8125

Guess you like

Origin blog.csdn.net/m0_46350041/article/details/105168511