Android仿美團(tuán)外賣(mài)菜單界面
美團(tuán)外賣(mài)菜單界面的Android實(shí)現(xiàn)代碼,供大家參考,具體內(nèi)容如下
布局文件
總布局
<?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:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:orientation="horizontal" tools:context="com.example.a1_.activity.MainActivity"> <ListView android:id="@+id/lv_left" android:layout_width="100dp" android:layout_height="match_parent"> </ListView> <se.emilsjolander.stickylistheaders.StickyListHeadersListView android:id="@+id/lv_right" android:layout_width="match_parent" android:layout_height="match_parent"> </se.emilsjolander.stickylistheaders.StickyListHeadersListView> </LinearLayout>
左側(cè)布局
<?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"> <TextView android:layout_margin="10dp" android:id="@+id/tv_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="菜單類(lèi)別" android:textSize="18sp" /> </RelativeLayout>
右側(cè)布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="65dp"
android:orientation="vertical">
<ImageView
android:src="@mipmap/ic_launcher"
android:layout_margin="4dp"
android:id="@+id/iv"
android:layout_width="50dp"
android:layout_height="50dp" />
<LinearLayout
android:layout_toRightOf="@id/iv"
android:orientation="vertical"
android:layout_margin="4dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="洋芋粉炒臘肉"
android:textSize="20sp"
android:id="@+id/tv_right_title1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tv_right_title2"
android:text="洋芋粉炒臘肉"
android:textSize="16sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tv_right_count"
android:text="月銷(xiāo)售54份"
android:textSize="18sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
適配器
左側(cè)適配器
package com.example.a1_.adapter;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.example.a1_.Bean.LeftBean;
import com.example.a1_.R;
import java.util.List;
/**
* Created by Administrator on 2017.05.27.0027.
*/
public class LeftAdapter extends BaseAdapter {
private List<LeftBean> mList;
private int currentLeftItem = 0;
//創(chuàng)建一個(gè)構(gòu)造方法
public LeftAdapter(List<LeftBean> mList) {
this.mList = mList;
}
@Override
public int getCount() {
return mList.size();
}
@Override
public LeftBean getItem(int position) {
return mList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView==null){
convertView = View.inflate(parent.getContext(), R.layout.left_item,null);
//創(chuàng)建viewHolder對(duì)象
viewHolder = new ViewHolder();
viewHolder.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
//讓viewholder掛在convertview上面一起復(fù)用
convertView.setTag(viewHolder);
}else {
//當(dāng)convertView不為空時(shí),吧viewholder取出來(lái)
viewHolder = (ViewHolder) convertView.getTag();
}
//獲取對(duì)應(yīng)條目的內(nèi)容
LeftBean leftBean = getItem(position);
//把對(duì)應(yīng)條目的內(nèi)容設(shè)置在控件上
viewHolder.tv_title.setText(leftBean.title);
//給左側(cè)條目設(shè)置顏色
if (currentLeftItem ==position){
viewHolder.tv_title.setTextColor(Color.RED);
}
return convertView;
}
public void setCurrentSelect(int currentLeftItem){
this.currentLeftItem = currentLeftItem;
}
//創(chuàng)建一個(gè)viewholder,用來(lái)復(fù)用對(duì)象
class ViewHolder{
TextView tv_title;
}
}
右側(cè)適配器
package com.example.a1_.adapter;
import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.example.a1_.Bean.LeftBean;
import com.example.a1_.Bean.RightBean;
import com.example.a1_.R;
import java.util.List;
import java.util.Random;
import se.emilsjolander.stickylistheaders.StickyListHeadersAdapter;
/**
* Created by Administrator on 2017.05.27.0027.
*/
public class RightAdapter extends BaseAdapter implements StickyListHeadersAdapter {
private List<LeftBean> mLeft;
private List<RightBean> mRight;
private Context context;
public RightAdapter(List<LeftBean> mLeft, List<RightBean> mRight, Context context) {
this.mLeft = mLeft;
this.mRight = mRight;
this.context = context;
}
@Override
public View getHeaderView(int position, View convertView, ViewGroup parent) {
TextView tv = new TextView(context);
tv.setTextColor(Color.RED);
tv.setTextSize(18);
tv.setText(mRight.get(position).type);
return tv;
}
@Override
public long getHeaderId(int position) {
return mRight.get(position).typeId;
}
@Override
public int getCount() {
return mRight.size();
}
@Override
public RightBean getItem(int position) {
return mRight.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView==null){
convertView = View.inflate(context, R.layout.right_item,null);
viewHolder = new ViewHolder();
viewHolder.title1 = (TextView) convertView.findViewById(R.id.tv_right_title1);
viewHolder.title2 = (TextView) convertView.findViewById(R.id.tv_right_title2);
viewHolder.count = (TextView) convertView.findViewById(R.id.tv_right_count);
convertView.setTag(viewHolder);
}else {
viewHolder = (ViewHolder) convertView.getTag();
}
RightBean rightBean = mRight.get(position);
viewHolder.title1.setText(rightBean.biaoti);
viewHolder.title2.setText(rightBean.biaoti);
//使用Random獲取隨機(jī)數(shù)
Random random = new Random();
int i = random.nextInt(100);
viewHolder.count.setText("月銷(xiāo)量"+i+"份");
return convertView;
}
static class ViewHolder{
TextView title1;
TextView title2;
TextView count;
}
}
javabean文件
左側(cè)
package com.example.a1_.Bean;
public class LeftBean {
public String title;
public int type;
}
右側(cè)
package com.example.a1_.Bean;
import android.R.string;
public class RightBean {
public String biaoti;
public String type;
public int typeId;
}
數(shù)據(jù)源
package com.example.a1_.data;
import com.example.a1_.Bean.LeftBean;
import com.example.a1_.Bean.RightBean;
import java.util.ArrayList;
import java.util.List;
/**
* Created by wangcaisheng on 2017/5/27.
*/
public class Data {
private static String[] leftData = new String[]{"13.9特價(jià)套餐","粗糧主食","佐餐小吃","用心營(yíng)養(yǎng)套餐(不含主食)","三杯雞雙拼尊享套餐","帶魚(yú)雙拼尊享套餐","紅燒肉雙拼尊享套餐"};
private static String[] rightData0 = new String[]{"洋芋粉炒臘肉","土雞燉香菇","新疆大盤(pán)辣子土雞","清燉土雞塊","農(nóng)家蒸碗 ","香辣野豬肉","香辣薯?xiàng)l大蝦","麻辣豬血"};
private static String[] rightData1 = new String[]{"商芝扣肉","羊肉蘿卜","干燒魚(yú) ","干煸野豬肉 ","排骨火鍋","土雞火鍋","牛肉火鍋","狗肉火鍋 "};
private static String[] rightData2 = new String[]{"虎皮辣子炒咸肉","重慶飄香水煮魚(yú)","紅燒土雞塊","干煸辣子土雞","清燉全雞 "};
private static String[] rightData3 = new String[]{"洋芋粉炒臘肉","土雞燉香菇","新疆大盤(pán)辣子土雞","清燉土雞塊","農(nóng)家蒸碗 ","香辣野豬肉","香辣薯?xiàng)l大蝦","麻辣豬血"};
private static String[] rightData4 = new String[]{"洋芋粉炒臘肉","土雞燉香菇","新疆大盤(pán)辣子土雞","清燉土雞塊","農(nóng)家蒸碗 ","香辣野豬肉","香辣薯?xiàng)l大蝦","麻辣豬血"};
private static String[] rightData5 = new String[]{"洋芋粉炒臘肉","土雞燉香菇","新疆大盤(pán)辣子土雞","清燉土雞塊","農(nóng)家蒸碗 ","香辣野豬肉","香辣薯?xiàng)l大蝦","麻辣豬血"};
private static String[] rightData6 = new String[]{"洋芋粉炒臘肉","土雞燉香菇","新疆大盤(pán)辣子土雞","清燉土雞塊","農(nóng)家蒸碗 ","香辣野豬肉","香辣薯?xiàng)l大蝦","麻辣豬血"};
public static List<LeftBean> getLeftData(){
List<LeftBean> list = new ArrayList<LeftBean>();
for (int i = 0; i < leftData.length; i++) {
LeftBean bean = new LeftBean();
bean.title = leftData[i];
bean.type = i;
list.add(bean);
}
return list;
}
public static List<RightBean> getRightData(List<LeftBean> list){
List<RightBean> rightList = new ArrayList<RightBean>();
for (int i = 0; i < list.size(); i++) {
LeftBean leftBean = list.get(i);
int mType = leftBean.type;
switch (mType) {
case 0:
rightList = getRightList(rightData0, leftBean, mType, rightList);
break;
case 1:
rightList = getRightList(rightData1, leftBean, mType, rightList);
break;
case 2:
rightList = getRightList(rightData2, leftBean, mType, rightList);
break;
case 3:
rightList = getRightList(rightData3, leftBean, mType, rightList);
break;
case 4:
rightList = getRightList(rightData4, leftBean, mType, rightList);
break;
case 5:
rightList = getRightList(rightData5, leftBean, mType, rightList);
break;
case 6:
rightList = getRightList(rightData6, leftBean, mType, rightList);
break;
}
}
return rightList;
}
private static List<RightBean> getRightList(String[] arr, LeftBean leftBean, int mType, List<RightBean> rightList){
for (int j = 0; j < arr.length; j++) {
RightBean bean = new RightBean();
bean.type = leftBean.title;
bean.biaoti = arr[j];
bean.typeId = mType;
rightList.add(bean);
}
return rightList;
}
}
核心代碼
package com.example.a1_.activity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import com.example.a1_.Bean.LeftBean;
import com.example.a1_.Bean.RightBean;
import com.example.a1_.R;
import com.example.a1_.adapter.LeftAdapter;
import com.example.a1_.adapter.RightAdapter;
import com.example.a1_.data.Data;
import java.util.List;
import se.emilsjolander.stickylistheaders.StickyListHeadersListView;
public class MainActivity extends AppCompatActivity {
private ListView lv_left;
private StickyListHeadersListView lv_right;
private int currentLeftItem;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
}
//初始化控件
private void initView() {
//初始化控件
lv_left = (ListView) findViewById(R.id.lv_left);
lv_right = (StickyListHeadersListView) findViewById(R.id.lv_right);
}
//設(shè)置適配器
private void initData() {
//創(chuàng)建適配器
final LeftAdapter leftAdapter = new LeftAdapter(Data.getLeftData());
//獲取左側(cè)數(shù)據(jù)
final List<LeftBean> leftData = Data.getLeftData();
//獲取右側(cè)數(shù)據(jù)
final List<RightBean> rightData = Data.getRightData(leftData);
RightAdapter rightAdapter = new RightAdapter(leftData, rightData, this);
//為左側(cè)布局設(shè)置適配器
lv_left.setAdapter(leftAdapter);
lv_right.setAdapter(rightAdapter);
//為左側(cè)條目設(shè)置點(diǎn)擊事件
lv_left.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//當(dāng)左側(cè)條目被點(diǎn)擊時(shí),記錄下被點(diǎn)擊條目的type
int type = leftData.get(position).type;
//遍歷右側(cè)條目,并獲取右側(cè)條目的typeId,與剛剛獲取的type對(duì)比,是否一致
for (int i = 0; i < rightData.size(); i++) {
if (type == rightData.get(i).typeId) {
//如果找到對(duì)應(yīng)的條目,那就將右側(cè)條目滾動(dòng)至對(duì)應(yīng)條目,并跳出循環(huán)
lv_right.smoothScrollToPosition(i);
currentLeftItem = i;
//設(shè)置當(dāng)前被選中的左側(cè)條目
leftAdapter.setCurrentSelect(currentLeftItem);
//刷新數(shù)據(jù)適配器
leftAdapter.notifyDataSetChanged();
break;
}
}
// Toast.makeText(MainActivity.this, "您選中了"+leftData.get(position).title, Toast.LENGTH_SHORT).show();
}
});
//為右側(cè)條目設(shè)置點(diǎn)擊事件
lv_right.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Toast.makeText(MainActivity.this, "右側(cè)條目被點(diǎn)擊了"+position, Toast.LENGTH_SHORT).show();
//當(dāng)右側(cè)條目被點(diǎn)擊時(shí),獲取被點(diǎn)擊條目的typeId
int typeId = rightData.get(position).typeId;
//遍歷左側(cè)條目
for (int i = 0; i < leftData.size(); i++) {
//獲取左側(cè)條目的type,與右側(cè)條目的typeId對(duì)比是否一致
if (typeId == leftData.get(i).type) {
//說(shuō)明找到了對(duì)應(yīng)條目,跳出循環(huán),設(shè)置當(dāng)前被選中的條目
currentLeftItem = i;
//設(shè)置當(dāng)前被選中的左側(cè)條目
leftAdapter.setCurrentSelect(currentLeftItem);
//刷新數(shù)據(jù)適配器
leftAdapter.notifyDataSetChanged();
break;
}
}
}
});
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android自定義StepView仿外賣(mài)配送進(jìn)度
- Android仿外賣(mài)購(gòu)物車(chē)功能
- Android仿百度外賣(mài)自定義下拉刷新效果
- Android仿美團(tuán)下拉菜單(商品選購(gòu))實(shí)例代碼
- Android仿美團(tuán)分類(lèi)下拉菜單實(shí)例代碼
- Android編程實(shí)現(xiàn)仿美團(tuán)或淘寶的多級(jí)分類(lèi)菜單效果示例【附demo源碼下載】
- Android仿美團(tuán)淘寶實(shí)現(xiàn)多級(jí)下拉列表菜單功能
- Android模仿美團(tuán)頂部的滑動(dòng)菜單實(shí)例代碼
- 模仿美團(tuán)點(diǎn)評(píng)的Android應(yīng)用中價(jià)格和購(gòu)買(mǎi)欄懸浮固定的效果
- Android使用RecyclerView仿美團(tuán)分類(lèi)界面
相關(guān)文章
android使用SkinManager實(shí)現(xiàn)換膚功能的示例
本篇文章主要介紹了android使用SkinManager實(shí)現(xiàn)換膚功能的示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-02-02
Android使用CardView實(shí)現(xiàn)圓角對(duì)話(huà)框
這篇文章主要為大家詳細(xì)介紹了Android使用CardView實(shí)現(xiàn)圓角對(duì)話(huà)框,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11
android項(xiàng)目從Eclipse遷移到Android studio中常見(jiàn)問(wèn)題解決方法
android項(xiàng)目從Eclipse遷移到Android studio中經(jīng)常會(huì)遇到一些問(wèn)題,本文提供了Android studio使用中常見(jiàn)問(wèn)題解決方法2018-03-03
Android實(shí)現(xiàn)右邊抽屜Drawerlayout效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)右邊抽屜Drawerlayout效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
Android實(shí)現(xiàn)底部圖片選擇Dialog
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)底部圖片選擇Dialog,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
Android中使用GridLayout網(wǎng)格布局來(lái)制作簡(jiǎn)單的計(jì)算器App
這篇文章主要介紹了Android中使用GridLayout網(wǎng)格布局來(lái)制作簡(jiǎn)單的計(jì)算器App的實(shí)例,GridLayout比表格布局TabelLayout更容易用來(lái)制作計(jì)算器這樣的多按鈕排列的界面,需要的朋友可以參考下2016-04-04
Android開(kāi)發(fā)中Button組件的使用
這篇文章主要介紹了Android開(kāi)發(fā)中Button組件的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
Android項(xiàng)目實(shí)戰(zhàn)(二十八):使用Zxing實(shí)現(xiàn)二維碼及優(yōu)化實(shí)例
這篇文章主要介紹了Android項(xiàng)目實(shí)戰(zhàn)(二十八):使用Zxing實(shí)現(xiàn)二維碼及優(yōu)化實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2016-11-11
Android屬性動(dòng)畫(huà)特點(diǎn)詳解
這篇文章主要為大家詳細(xì)介紹了Android屬性動(dòng)畫(huà)特點(diǎn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11
Flutter定義tabbar底部導(dǎo)航路由跳轉(zhuǎn)的方法
這篇文章主要為大家詳細(xì)介紹了Flutter定義tabbar底部導(dǎo)航路由跳轉(zhuǎn)的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07

