Android?完整購(gòu)物商城界面的實(shí)現(xiàn)案例
activity_main.xml
在res/layout文件中,放置一個(gè)TextView控件用于顯示購(gòu)物商城界面的標(biāo)題,放置一個(gè)ListView控件用于顯示購(gòu)物商場(chǎng)界面的列表
<?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"
tools:context=".MainActivity"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="45dp"
android:text="購(gòu)物商城"
android:textSize="18sp"
android:textColor="#FFFFFF"
android:background="#FF8F03"
android:gravity="center"/>
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:listSelector="@color/colorAccent" />
</LinearLayout>
list_Item.xml
在res/layout文件中創(chuàng)建一個(gè)列表?xiàng)l目界面的布局文件list_item.xml,在該文件中放置一個(gè)Image View控件用于顯示商品的圖片;放置2個(gè)TextView控件分別用于顯示商品的名稱和價(jià)格!
<?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:id="@+id/iv"
android:layout_width="120dp"
android:layout_height="90dp"
android:layout_centerVertical="true" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@+id/iv">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="桌子"
android:textColor="#000000"
android:textSize="20sp" />
<TextView
android:id="@+id/tv_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/title"
android:layout_marginTop="10dp"
android:text="價(jià)格:"
android:textColor="#FF8F03"
android:textSize="20sp" />
<TextView
android:id="@+id/price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/title"
android:layout_marginTop="10dp"
android:layout_toRightOf="@+id/tv_price"
android:text="1000"
android:textColor="#FF8F03"
android:textSize="20sp" />
</RelativeLayout>
</RelativeLayout>
MainActivity.java
12-14行代碼定義了數(shù)組titles,prices,icons,分別用于存儲(chǔ)商品列表中的商品名稱,價(jià)格和圖片,并且3個(gè)數(shù)組的長(zhǎng)度一致。
19行創(chuàng)建MyBaseAdpter的實(shí)例
20行設(shè)置數(shù)據(jù)適配器List View上
23-34行代碼返回條目的長(zhǎng)度,價(jià)格,數(shù)據(jù)對(duì)象
37行找到list_item.xml中創(chuàng)建的TextView控件
創(chuàng)建一個(gè)View Holder類,將需要加載的控件變量放在該類中
最后調(diào)用id屬性,set實(shí)現(xiàn)!
package com.example.dymemory1;
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 ListView mListView;
private String[] titles = {"蘋果","蛋糕","修羅壁紙","桌子","獼猴桃","毛衣"};
private String[] prices = {"10元/kg","100元/個(gè)","50元/張","45元/把","10元/kg","110元/件"};
private int[] icons = {R.drawable.apple,R.drawable.cake,R.drawable.juyoujing,R.drawable.table,R.drawable.kiwifruit,R.drawable.wireclothes};
protected void onCreate(Bundle saveInstanceState){
super.onCreate(saveInstanceState);
setContentView(R.layout.activity_main);
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 =null;
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;
}
}
到此這篇關(guān)于Android 完整購(gòu)物商城界面的實(shí)現(xiàn)案例的文章就介紹到這了,更多相關(guān)Android 購(gòu)物商城界面內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解Android自定義控件屬性TypedArray以及attrs
這篇文章主要為大家介紹了android自定義控件屬性TypedArray以及attrs,感興趣的小伙伴們可以參考一下2016-01-01
常見(jiàn)Android編譯優(yōu)化問(wèn)題梳理總結(jié)
這篇文章主要介紹了常見(jiàn)Android編譯優(yōu)化問(wèn)題梳理總結(jié),文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-08-08
Android?studio?利用共享存儲(chǔ)進(jìn)行用戶的注冊(cè)和登錄驗(yàn)證功能
這篇文章主要介紹了Android?studio?利用共享存儲(chǔ)進(jìn)行用戶的注冊(cè)和登錄驗(yàn)證功能,包括注冊(cè)頁(yè)面布局及登錄頁(yè)面功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-12-12
Phonegap使用拍照功能時(shí)的內(nèi)存問(wèn)題
最近幾天在學(xué)習(xí)使用phonegap進(jìn)行android應(yīng)用的開(kāi)發(fā),網(wǎng)上的資料比較亂,個(gè)人參考了很多資料,也試驗(yàn)了很多次,一直在摸索,總算小有心得,這此過(guò)程中也遇到了一些問(wèn)題,這里給大家分享下解決Phonegap使用拍照功能時(shí)的內(nèi)存問(wèn)題的方法,這里簡(jiǎn)單的整理一下2015-05-05
Android registerForActivityResult動(dòng)態(tài)申請(qǐng)權(quán)限案例詳解
這篇文章主要介紹了Android registerForActivityResult動(dòng)態(tài)申請(qǐng)權(quán)限案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09
Android指紋識(shí)別認(rèn)識(shí)和基本使用詳解
這篇文章主要為大家詳細(xì)介紹了Android指紋識(shí)別認(rèn)識(shí)和基本的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07
詳解Flutter中網(wǎng)絡(luò)框架dio的二次封裝
其實(shí)dio框架已經(jīng)封裝的很好了,但是在實(shí)戰(zhàn)項(xiàng)目中,為了項(xiàng)目可以統(tǒng)一管理,還是需要對(duì)dio框架進(jìn)行二次封裝。本文將詳細(xì)講解一下dio如何二次封裝,需要的可以參考一下2022-04-04
OpenGL Shader實(shí)例分析(1)Wave效果
這篇文章主要為大家詳細(xì)介紹了OpenGL Shader實(shí)例分析第一篇,Wave效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02

