Android Imitation Meituan Takeaway Menu Interface

  • 2021-11-01 04:49:42
  • OfStack

The Android implementation code of the US group take-out menu interface is for your reference. The specific contents are as follows

Layout file

General layout


<?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>

Left side layout


<?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=" Menu category "
  android:textSize="18sp" />

</RelativeLayout>

Right side layout


<?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=" Stir-fried bacon with potato powder "
    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=" Stir-fried bacon with potato powder "
    android:textSize="16sp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

  <TextView
    android:id="@+id/tv_right_count"
    android:text=" Monthly sales 54 Portions "
    android:textSize="18sp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</LinearLayout>

</RelativeLayout>

Adapter

Left side adapter


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;
// Create 1 A construction method 

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);

    // Create viewHolder Object 
    viewHolder = new ViewHolder();
    viewHolder.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
    // Jean viewholder Hang on convertview Above 1 Start multiplexing 
    convertView.setTag(viewHolder);
  }else {
    // When convertView When not empty , Right viewholder Take it out 
    viewHolder = (ViewHolder) convertView.getTag();
  }

  // Get the contents of the corresponding entry 
  LeftBean leftBean = getItem(position);
  // Set the contents of the corresponding entry on the control 
  viewHolder.tv_title.setText(leftBean.title);


  // Set the color for the left entry 
  if (currentLeftItem ==position){
    viewHolder.tv_title.setTextColor(Color.RED);
  }
  return convertView;
}

public void setCurrentSelect(int currentLeftItem){
  this.currentLeftItem = currentLeftItem;
}

// Create 1 A viewholder, Used to reuse objects 
class ViewHolder{
  TextView tv_title;
}
}

Right adapter


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);
  // Use Random Get random number 
  Random random = new Random();
  int i = random.nextInt(100);
  viewHolder.count.setText(" Monthly sales volume "+i+" Portions ");
  return convertView;
}

static class ViewHolder{
  TextView title1;
  TextView title2;
  TextView count;
}
}

javabean file

Left side


package com.example.a1_.Bean;


public class LeftBean {
public String title;
public int type;

}

Right side


package com.example.a1_.Bean;

import android.R.string;

public class RightBean {

public String biaoti;
public String type;
public int typeId;

}

Data source


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 Special package "," Coarse grain staple food "," Snacks with meals "," Nutrition package with heart ( No staple food )","3 Cup chicken double spelling exclusive package "," Hairtail double spelling exclusive package "," Braised pork double spelling exclusive package "};
private static String[] rightData0 = new String[]{" Stir-fried bacon with potato powder "," Stewed mushrooms with native chicken "," Xinjiang Dapan chili pepper native chicken "," Stewed chicken nuggets "," Farmhouse steaming bowl  "," Spicy wild pork "," Prawn with spicy French fries "," Spicy pig blood "};
private static String[] rightData1 = new String[]{" Braised pork with Chinese cheese "," Mutton and radish "," Dried fish  "," Stir-fried wild pork  "," Sparerib chafing dish "," Chicken hot pot "," Beef chafing dish "," Dog meat chafing dish  "};
private static String[] rightData2 = new String[]{" Fried bacon with tiger skin and chili pepper "," Chongqing fragrant boiled fish "," Braised chicken nuggets "," Stir-fried chili pepper native chicken "," Stewed whole chicken  "};
private static String[] rightData3 = new String[]{" Stir-fried bacon with potato powder "," Stewed mushrooms with native chicken "," Xinjiang Dapan chili pepper native chicken "," Stewed chicken nuggets "," Farmhouse steaming bowl  "," Spicy wild pork "," Prawn with spicy French fries "," Spicy pig blood "};
private static String[] rightData4 = new String[]{" Stir-fried bacon with potato powder "," Stewed mushrooms with native chicken "," Xinjiang Dapan chili pepper native chicken "," Stewed chicken nuggets "," Farmhouse steaming bowl  "," Spicy wild pork "," Prawn with spicy French fries "," Spicy pig blood "};
private static String[] rightData5 = new String[]{" Stir-fried bacon with potato powder "," Stewed mushrooms with native chicken "," Xinjiang Dapan chili pepper native chicken "," Stewed chicken nuggets "," Farmhouse steaming bowl  "," Spicy wild pork "," Prawn with spicy French fries "," Spicy pig blood "};
private static String[] rightData6 = new String[]{" Stir-fried bacon with potato powder "," Stewed mushrooms with native chicken "," Xinjiang Dapan chili pepper native chicken "," Stewed chicken nuggets "," Farmhouse steaming bowl  "," Spicy wild pork "," Prawn with spicy French fries "," Spicy pig blood "};

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;
}
}

Core code


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();


}

// Initialize control 
private void initView() {
  // Initialize control 
  lv_left = (ListView) findViewById(R.id.lv_left);
  lv_right = (StickyListHeadersListView) findViewById(R.id.lv_right);
}

// Setting adapter 
private void initData() {
  // Create adapter 
  final LeftAdapter leftAdapter = new LeftAdapter(Data.getLeftData());
  // Get left side data 
  final List<LeftBean> leftData = Data.getLeftData();
  // Get data on the right 
  final List<RightBean> rightData = Data.getRightData(leftData);
  RightAdapter rightAdapter = new RightAdapter(leftData, rightData, this);

  // Set the adapter for the left layout 
  lv_left.setAdapter(leftAdapter);
  lv_right.setAdapter(rightAdapter);


  // Set the click event for the entry on the left 
  lv_left.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
      // When the entry on the left is clicked , Record the value of the clicked item type
      int type = leftData.get(position).type;
      // Traverse the right-hand entry , Object for the entry on the right typeId, The same as the type Contrast , Whether or not 1 To 
      for (int i = 0; i < rightData.size(); i++) {
        if (type == rightData.get(i).typeId) {
          // If the corresponding entry is found , Then scroll the right entry to the corresponding entry , And jump out of the loop 
          lv_right.smoothScrollToPosition(i);
          currentLeftItem = i;
          // Sets the currently selected left-hand entry 
          leftAdapter.setCurrentSelect(currentLeftItem);
          // Refresh data adapter 
          leftAdapter.notifyDataSetChanged();
          break;
        }
      }

//        Toast.makeText(MainActivity.this, " You have selected "+leftData.get(position).title, Toast.LENGTH_SHORT).show();
    }
  });

  // Set the click event for the entry on the right 
      lv_right.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//        Toast.makeText(MainActivity.this, " The entry on the right was clicked "+position, Toast.LENGTH_SHORT).show();

      // When the entry on the right is clicked , Object of the clicked item typeId
      int typeId = rightData.get(position).typeId;
      // Traverse the left entry 
      for (int i = 0; i < leftData.size(); i++) {
        // Object for the entry on the left type, Of the entry on the right typeId Contrast whether 1 To 
        if (typeId == leftData.get(i).type) {
          // Explain that the corresponding entry was found , Out of the loop , Set the currently selected entry 
          currentLeftItem = i;
          // Sets the currently selected left-hand entry 
          leftAdapter.setCurrentSelect(currentLeftItem);
          // Refresh data adapter 
          leftAdapter.notifyDataSetChanged();
          break;
        }

      }
    }
  });
}
}

Related articles: