Realize the effect of displaying the product list on the interface after Taobao search

Table of contents

activity_main.xml

list_view_header.xml

mainactivity.xml


It is used for homework examples of Android studio courses in colleges and universities. If it is not necessary, please do not plagiarize

Realized the effect of product list, including six products, please research by yourself if you need to add products

activity_main.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="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:text="水果商城"
        android:textSize="18sp"
        android:textColor="#FFFFFF"
        android:background="#FA3C78"
        android:gravity="center" />
    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/lv"/>
</LinearLayout>

list_view_header.xml

<?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="match_parent"
    android:padding="16dp">
    <ImageView
        android:layout_width="120dp"
        android:layout_height="90dp"
        android:id="@+id/iv"
        android:layout_centerVertical="true" />
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_toRightOf="@+id/iv"
        android:layout_centerVertical="true">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/title"
            android:text="桌子"
            android:textSize="20sp"
            android:textColor="#000000" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tv_price"
            android:text="价格:"
            android:textSize="20sp"
            android:layout_marginTop="10dp"
            android:layout_below="@id/title"
            android:textColor="#FF8F03" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/price"
            android:text="1000"
            android:textSize="20sp"
            android:layout_marginTop="10dp"
            android:layout_below="@id/title"
            android:layout_toRightOf="@id/tv_price"
            android:textColor="#FF8F03" />
    </RelativeLayout>
</RelativeLayout>

mainactivity.xml

package com.example.myapplication;

import android.app.Activity;
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;


public class MainActivity extends Activity {
    private  String[] titles = {"苹果","香蕉","梨子","火龙果","猕猴桃","桃子"};
    private  String[] prices = {"10元/kg","8元/kg","7元/kg","15元/kg","10元/kg","11元/kg"};

    private int[] icons = {R.drawable.pingguo,R.drawable.xiangjiao,R.drawable.lizi,R.drawable.huolongguo,R.drawable.mihoutoa,R.drawable.taozi};

    protected  void onCreate(Bundle saveInstanceState){
        super.onCreate(saveInstanceState);
        setContentView(R.layout.activity_main);

        ListView mListView = (ListView) findViewById(R.id.lv);

        MyBaseAdaPter mAdapter = new MyBaseAdaPter();

        mListView.setAdapter(mAdapter);
    }
class MyBaseAdaPter extends  BaseAdapter{
        @Override
        public int getCount( ){
            return  titles.length;
        }
        @Override
        public Object getItem(int position){
            return titles[position];
        }
        @Override
        public long getItemId(int position){
            return  position;
        }
        @Override
        public View getView(int position,View convertView,ViewGroup parent){
            ViewHolder holder;
            if(convertView == null){
                convertView = View.inflate( MainActivity.this,R.layout.list_item,null);
                holder= new ViewHolder();
                holder.title=(TextView) convertView.findViewById(R.id.title);
                holder.price=(TextView) convertView.findViewById(R.id.price);
                holder.iv=(ImageView) convertView.findViewById(R.id.iv);
                convertView.setTag(holder);
            }
            else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.title.setText(titles[position]);
            holder.price.setText(prices[position]);
            holder.iv.setBackgroundResource(icons[position]);
            return  convertView;
        }
    }
    static class ViewHolder{
        TextView title;
        TextView price;
        ImageView iv;
    }
}

After modification, run the interface

Classmates, you must study hard after paying the tuition fees, copying and copying will end up hurting yourself

Add attention and like, thank you! ! !

Note: When copying the code directly, please add your own background image and modify the file name, please leave a message for other errors

If you need to customize WeChat applet, poster design, logo design, please add little brother VX: Doi000101, and note your intention

Guess you like

Origin blog.csdn.net/weixin_46165323/article/details/127181390