安卓学习第三篇:Intent、Bundle的使用以及RecyclerView、ListView的应用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jining11/article/details/88623172

一、实验题目

Intent、Bundle的使用以及RecyclerView、ListView的应用


二、实现内容

本次实验模拟实现一个健康食品列表,有两个界面,第一个界面用于呈现食品列表 如下所示

在这里插入图片描述

数据在"manual/素材"目录下给出。
点击右下方的悬浮按钮可以切换到收藏夹

在这里插入图片描述

上面两个列表点击任意一项后,可以看到详细的信息:

在这里插入图片描述

UI要求

  • 食品列表
    每一项为一个圆圈和一个名字,圆圈和名字都是垂直居中。圆圈内的内容是该食品的种类,内容要处于圆圈的中心,颜色为白色。食品名字为黑色,圆圈颜色自定义,只需能看见圆圈内的内容即可。

  • 收藏夹
    与食品列表相似

  • 食品详情界面

      1. 界面顶部
        在这里插入图片描述
        顶部占整个界面的1/3。每个食品详情的顶部颜色在数据中已给出。返回图标处于这块区域的左上角,食品名字处于左下角,星标处于右下角,边距可以自己设置。 返回图标与名字左对齐,名字与星标底边对齐。 建议用RelativeLayout实现,以熟悉RelativeLayout的使用。
      2. 界面中部
        在这里插入图片描述
        使用的黑色argb编码值为#D5000000,稍微偏灰色的“富含”“蛋白质”的argb编码值为#8A000000。"更多资料"一栏上方有一条分割线,argb编码值为#1E000000。右边收藏符号的左边也有一条分割线,要求与收藏符号高度一致,垂直居中。字体大小自定。"更多资料"下方分割线高度自定。这部分所有的分割线argb编码值都是#1E000000。
      3. 界面底部
        在这里插入图片描述
        使用的黑色argb编码值为#D5000000。
    • 标题栏
      两个界面的标题栏都需要去掉

    功能要求

    • 使用RecyclerView实现食品列表。点击某个食品会跳转到该食品的详情界面,呈现该食品的详细信息。长按列表中某个食品会删除该食品,并弹出Toast,提示 “删除XX”
    • 点击右下方的FloatingActionButton,从食品列表切换到收藏夹或从收藏夹切换到食品列表,并且该按钮的图片作出相应改变。
    • 使用ListView实现收藏夹。点击收藏夹的某个食品会跳转到食品详情界面,呈现该食品的详细信息。长按收藏夹中的某个食品会弹出对话框询问是否移出该食品,点击确定则移除该食品,点击取消则对话框消失。如长按“鸡蛋”,对话框内容如下图所示。
      在这里插入图片描述
    • 商品详情界面中点击返回图标会返回上一层。点击星标会切换状态,如果原本是空心星星,则会变成实心星星;原本是实心星星,则会变成空心星星。点击收藏图表则将该食品添加到收藏夹并弹出Toast提示 “已收藏”

三、实验结果

(1)实验截图

主页外观:

扫描二维码关注公众号,回复: 5639199 查看本文章

在这里插入图片描述

在这里插入图片描述

单击之后的详情显示:

在这里插入图片描述

星星的切换:

在这里插入图片描述

点击收藏时:

在这里插入图片描述

主页长按事件:

在这里插入图片描述

删除之后主页更新:

在这里插入图片描述

点击收藏夹按钮:

在这里插入图片描述

收藏夹长按事件:

在这里插入图片描述

(2)实验步骤以及关键代码


AndroidManifest.xml

注意要绑定activity

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.a13371.myapplication">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.AppCompat.Light.NoActionBar">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.example.a13371.myapplication.InfoActivity"></activity>
    </application>

</manifest>

MainActivity.java

规定主页行为


  • 引入包
package com.example.a13371.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ListView;
import android.view.View;
import android.widget.SimpleAdapter;
import android.support.design.widget.FloatingActionButton;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import android.widget.TextView;
import android.widget.Toast;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.widget.AdapterView;
import jp.wasabeef.recyclerview.adapters.ScaleInAnimationAdapter;
import jp.wasabeef.recyclerview.animators.OvershootInLeftAnimator;
  • 函数
public class MainActivity extends AppCompatActivity {
    boolean click = true;
    FloatingActionButton btn;
    ListView Favorite;
    List<Map<String, Object>> favoritethings;
    SimpleAdapter simpleAdapter1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Favorite = (ListView) findViewById(R.id.favorite);
        btn = (FloatingActionButton) findViewById(R.id.btn);
        final List<Map<String, Object>> data = new ArrayList<>();
        favoritethings = new ArrayList<>();
        Map<String, Object> f = new LinkedHashMap<>();
        f.put("cycle", "*");
        f.put("name", "收藏夹");
        favoritethings.add(f);
        //final List<Map<String, String>> M = (List<Map<String, String>>)getIntent().getSerializableExtra("favoratethings");

        /*  为每一项数据创建一个对象,并添加在List中  */
        final List<Collection> clist = new ArrayList<Collection>() {{
            add(new Collection("大豆", "粮", "粮食", "蛋白质", "#BB4C3B"));
            add(new Collection("十字花科蔬菜", "蔬", "蔬菜", "维生素C", "#C48D30"));
            add(new Collection("牛奶", "饮", "饮品", "钙", "#4469B0"));
            add(new Collection("海鱼", "肉", "肉食", "蛋白质", "#20A17B"));
            add(new Collection("菌菇类", "蔬", "蔬菜", "微量元素", "#BB4C3B"));
            add(new Collection("番茄", "蔬", "蔬菜", "番茄红素", "#4469B0"));
            add(new Collection("胡萝卜", "蔬", "蔬菜", "胡萝卜素", "#20A17B"));
            add(new Collection("荞麦", "粮", "粮食", "膳食纤维", "#BB4C3B"));
            add(new Collection("鸡蛋", "杂", "杂", "几乎所有营养物质", "#C48D30"));
        }};

        String[] cycle = new String[clist.size()];
        for (int i = 0; i < clist.size(); i++) {
            String x = clist.get(i).getCycle();
            cycle[i] = x;
        }
        String[] name = new String[clist.size()];
        for (int i = 0; i < clist.size(); i++) {
            String x = clist.get(i).getName();
            name[i] = x;
        }
        for (int i = 0; i < clist.size(); i++) {
            Map<String, Object> temp = new LinkedHashMap<>();
            temp.put("cycle", cycle[i]);
            temp.put("name", name[i]);
            data.add(temp);
        }

        /*  RecycleView单击事件  */
        final RecyclerView RView = (RecyclerView) findViewById(R.id.List);
        RView.setLayoutManager(new LinearLayoutManager(this));
       /* final SimpleAdapter simpleAdapter = new SimpleAdapter(this, data, R.layout.information,
                new String[]{"cycle", "name"}, new int[]{R.id.cycle, R.id.name});*/
        final MyRecyclerViewAdapter myAdapter = new MyRecyclerViewAdapter<Collection>(MainActivity.this, R.layout.information, clist) {
            @Override
            public void convert(MyViewHolder holder, Collection s) {
                TextView cycle = holder.getView(R.id.cycle);
                cycle.setText(s.getCycle());
                TextView name = holder.getView(R.id.name);
                name.setText(s.getName());
            }
        };

        myAdapter.setOnItemClickListener(new MyRecyclerViewAdapter.OnItemClickListener() {
            @Override
            public void onClick(int i) {
                Intent intent = new Intent(MainActivity.this, InfoActivity.class);
                //Toast.makeText(getApplication(), "hh"+ M.toString(), Toast.LENGTH_SHORT).show();
                Collection temp = clist.get(i);
                intent.putExtra("Collection", temp);
                //startActivity(intent);
                startActivityForResult(intent, 1);
            }

            @Override
            public void onLongClick(int position) {
                AlertDialog.Builder message = new AlertDialog.Builder(MainActivity.this);
                message.setTitle("删除");
                message.setMessage("确定删除" + clist.get(position).getName()+"?");
                final int k = position;
                message.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(getApplication(), "删除"+clist.get(k).getName(), Toast.LENGTH_SHORT).show();
                        clist.remove(k);
                        data.remove(k);
                        myAdapter.notifyDataSetChanged();
                        dialogInterface.dismiss();
                    }
                });
                message.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialogInterface, int i) {
                    }
                });
                message.create().show();
            }

        });

       //RView.setAdapter(myAdapter);
        ScaleInAnimationAdapter scaleInAnimationAdapter = new ScaleInAnimationAdapter(myAdapter);
        scaleInAnimationAdapter.setDuration(1000);
        RView.setAdapter((scaleInAnimationAdapter));
        RView.setItemAnimator(new OvershootInLeftAnimator());

        /*  Favorite单击事件*/
        //final SimpleAdapter simpleAdapter1 = new SimpleAdapter(this, favoritethings, R.layout.information,
                //new String[]{"cycle", "name"}, new int[]{R.id.cycle, R.id.name});
        simpleAdapter1 = new SimpleAdapter(this, favoritethings, R.layout.information,
                new String[]{"cycle", "name"}, new int[]{R.id.cycle, R.id.name});
        Favorite.setAdapter(simpleAdapter1);

        Favorite.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (i > 0) {
                    Intent intent = new Intent(MainActivity.this, InfoActivity.class);
                    int pos = 0;
                    final Iterator iter = favoritethings.get(i).keySet().iterator();
                    //iter.next();
                    String key = iter.next().toString();
                    String name = favoritethings.get(i).get(key).toString();
                    //Toast.makeText(getApplication(), name, Toast.LENGTH_SHORT).show();
                    //String fina = "";
                    for (pos = 0; pos < clist.size(); pos++) {
                        //fina = fina + clist.get(pos).getName()+name;
                        if (name.equals(clist.get(pos).getName())) {
                            //Toast.makeText(getApplication(), clist.get(pos).getName()+name, Toast.LENGTH_SHORT).show();
                            break;
                        }
                    }
                   // Toast.makeText(getApplication(), fina, Toast.LENGTH_SHORT).show();
                    //Toast.makeText(getApplication(), pos+"", Toast.LENGTH_SHORT).show();
                    if (pos < clist.size()) {
                        Collection temp = clist.get(pos);
                        intent.putExtra("Collection", temp);
                        startActivity(intent);
                    }
                    else {
                        Toast.makeText(getApplication(), "该食物已被删除", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });

        /*  Favorite长按事件  */
        Favorite.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
                if (position > 0) {
                    AlertDialog.Builder message = new AlertDialog.Builder(MainActivity.this);
                    message.setTitle("删除");
                    final Iterator iter = favoritethings.get(position).keySet().iterator();
                    //iter.next();
                    final String key = iter.next().toString();
                    message.setMessage("确定删除" + favoritethings.get(position).get(key)+"?");
                    message.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialogInterface, int i) {
                            Toast.makeText(getApplication(), "删除"+ favoritethings.get(position).get(key), Toast.LENGTH_SHORT).show();
                            favoritethings.remove(position);
                            simpleAdapter1.notifyDataSetChanged();
                            dialogInterface.dismiss();
                        }
                    });
                    message.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialogInterface, int i) {
                        }
                    });
                    message.create().show();
                }
                return true;
            }
        });

        /*收藏页的点击事件*/
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (click) {
                    Favorite.setVisibility(View.VISIBLE);
                    RView.setVisibility(View.INVISIBLE);
                    btn.setImageDrawable(getResources().getDrawable(R.mipmap.favorate));
                } else {
                    Favorite.setVisibility(View.INVISIBLE);
                    RView.setVisibility(View.VISIBLE);
                    btn.setImageDrawable(getResources().getDrawable(R.mipmap.collect));
                }
                click = !click;
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        //Toast.makeText(getApplication(), "8888888", Toast.LENGTH_SHORT).show();
        if(requestCode == 1 && resultCode ==1){
            Map<String, Object> t  = (Map<String, Object>)intent.getExtras().get("favoratethings");
            this.favoritethings.add(t);
            //Toast.makeText(getApplication(), "666", Toast.LENGTH_SHORT).show();
            simpleAdapter1.notifyDataSetChanged();
        }
    }

}


InfoActivity.java

规定具体信息页的行为

  • 引入包
package com.example.a13371.myapplication;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.Map;
  • 函数
public class InfoActivity extends AppCompatActivity {
    private boolean tag = false;
//    private List<Map<String, String>> favoritethings = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {

//        Map<String, String> f = new LinkedHashMap<>();
//        f.put("cycle", "*");
//        f.put("name", "收藏夹");
//        favoritethings.add(f);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.detail);

        Collection c = (Collection) getIntent().getSerializableExtra("Collection");

        RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.Top);
        relativeLayout.setBackgroundColor(Color.parseColor(c.getBackground()));

        Button back = (Button) findViewById(R.id.Back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                InfoActivity.this.finish();
                //finish();
                //onDestroy();
            }
        });

        TextView name = (TextView) findViewById(R.id.Name);
        name.setText(c.getName());

        TextView Category = (TextView) findViewById(R.id.Category);
        Category.setText(c.getCategory());

        TextView contain = (TextView) findViewById(R.id.contain);
        contain.setText("富含");

        TextView Nutrition = (TextView) findViewById(R.id.Nutrition);
        Nutrition.setText(c.getNutrition());


        String[] operations1 = new String[]{"更多资料"};
        ArrayAdapter<String> arrayAdapter1 = new ArrayAdapter<>(this, R.layout.more, operations1);
        ListView listView1 = (ListView) findViewById(R.id.more);
        listView1.setAdapter(arrayAdapter1);

        String[] options = new String[]{"分享信息", "不感兴趣", "查看更多信息", "出错反馈"};
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(this, R.layout.more, options);
        ListView listView = (ListView) findViewById(R.id.listview);
        listView.setAdapter(arrayAdapter);

        /*  星星的切换 */
        final Button star = (Button) findViewById(R.id.star);
        star.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!tag) {
                    star.setBackground(getResources().getDrawable(R.mipmap.full_star));
                    tag = true;
                } else {
                    star.setBackground(getResources().getDrawable(R.mipmap.empty_star));
                    tag = false;
                }
            }
        });

        Button shoppingcart = (Button)findViewById(R.id.shoppingcart);;
        /*加入购物车的点击事件*/
        final TextView Name = findViewById(R.id.Name);
        final TextView Catogary = findViewById(R.id.Category);
        shoppingcart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String sname = Name.getText().toString();
                String scircle = Catogary.getText().toString().substring(0,1);
                Map<String, Object> t = new LinkedHashMap<>();
                t.put("cycle", scircle);
                t.put("name", sname);
                Toast.makeText(InfoActivity.this, "已收藏", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(InfoActivity.this, MainActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("favoratethings", (Serializable)t);
                intent.putExtras(bundle);
                setResult(1,intent);
            }
        });
    }
}

Collection.java

用于自定义Collection类

  • 引入包

    package com.example.a13371.myapplication;
    import java.io.Serializable;
    
  • 函数

    /*  存储相应的数据  */
    public class Collection implements Serializable {
    
        private String name;
        private String Cycle;
        private String category;
        private String nutrition;
        private String background_colour;
    
        public Collection(String name, String Cycle, String category, String nutrition, String background_colour) {
            this.name = name;
            this.Cycle = Cycle;
            this.category = category;
            this.nutrition = nutrition;
            this.background_colour = background_colour;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getCycle() {
            return Cycle;
        }
    
        public void setCycle(String Cycle) {
            this.Cycle = Cycle;
        }
    
        public String getCategory() {
            return category;
        }
    
        public void setCategory(String category) {
            this.category = category;
        }
    
        public String getNutrition() {
            return nutrition;
        }
    
        public void setNutrition(String nutrition) {
            this.nutrition = nutrition;
        }
    
        public String getBackground() {
            return background_colour;
        }
    
        public void setBackground(String background) {
            this.background_colour = background;
        }
    }
    

MyRecyclerViewHolder.java

  • 引入包
package com.example.a13371.myapplication;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import java.util.List;
  • 函数

    public abstract class MyRecyclerViewAdapter<T> extends RecyclerView.Adapter<MyViewHolder> {
        private Context MyContext;
        private int MylayoutId;
        private List<T> Mydata;
        private OnItemClickListener onItemClickListener = null;
    
        public abstract void convert(MyViewHolder holder, T t);
        public MyRecyclerViewAdapter(Context _context, int _layoutId, List<T> _data) {
            MyContext = _context;
            MylayoutId = _layoutId;
            Mydata = _data;
        }
    
        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            MyViewHolder holder = MyViewHolder.get(MyContext, parent, MylayoutId);
            return holder;
        }
    
        @Override
        public int getItemCount() {
            return this.Mydata.size();
        }
    
    
        @Override
        public void onBindViewHolder(final MyViewHolder holder, int position) {
            convert(holder, Mydata.get(position)); // convert函数需要重写,下面会讲
            if (onItemClickListener != null) {
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        onItemClickListener.onClick(holder.getAdapterPosition());
                    }
                });
                holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View view) {
                        onItemClickListener.onLongClick(holder.getAdapterPosition());
                        return false;
                    }
                });
            }
        }
    
        public interface OnItemClickListener{
            void onClick(int position);
            void onLongClick(int position);
        }
    
        public void setOnItemClickListener(OnItemClickListener _onItemClickListener) {
            this.onItemClickListener = _onItemClickListener;
        }
    
    
    

MyViewHolder.java

  • 引入包
package com.example.a13371.myapplication;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
  • 函数
public class MyViewHolder extends RecyclerView.ViewHolder {
    private SparseArray<View> views;
    private View view;
    public MyViewHolder(Context _context, View _view, ViewGroup _viewGroup){
        super(_view);
        view = _view;
        views = new SparseArray<View>();
    }

    public static MyViewHolder get(Context _context, ViewGroup _viewGroup, int _layoutId) {
        View _view = LayoutInflater.from(_context).inflate(_layoutId, _viewGroup, false);
        MyViewHolder holder = new MyViewHolder(_context, _view, _viewGroup);
        return holder;
    }

    public <T extends View> T getView(int _viewId) {
        View _view = views.get(_viewId);
        if (_view == null) {
            // 创建view
            _view = view.findViewById(_viewId);
            // 将view存入views
            views.put(_viewId, _view);
        }
        return (T)_view;
    }
}

shape.xml

定义圆形背景

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <solid android:color="#3F51B5" />
    <corners android:radius="30dip" />
    <padding
        android:bottom="10dp"
        android:left="5dp"
        android:right="5dp"
        android:top="10dp" />
    <size
        android:width="60dp"
        android:height="60dp"/>
</shape>

activity_main.xml

规定列表页和收藏页的布局

<?xml version="1.0" encoding="utf-8" ?>
<android.support.constraint.ConstraintLayout 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"
    android:orientation="horizontal"
    tools:context=".MainActivity">

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/List"
        android:visibility="visible"
    />

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/favorite"
        android:visibility="invisible">
    </ListView>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/collect"
        app:backgroundTint="@color/white"
        app:backgroundTintMode="src_atop"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_margin="25dp" />

</android.support.constraint.ConstraintLayout>

detail.xml

规定详情页的布局

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/white"
    tools:context="com.example.a13371.myapplication.InfoActivity">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:id="@+id/Top"
        android:layout_weight="1"
        android:background="@color/colorPrimaryDark">

        <!--返回键-->
        <Button
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:id="@+id/Back"
            android:layout_marginStart="10dp"
            android:background="@mipmap/back" />

        <!--星星-->
        <Button
            android:layout_width="35dp"
            android:layout_height="35dp"
            android:id="@+id/star"
            android:layout_alignParentBottom="true"
            android:layout_alignParentEnd="true"
            android:layout_marginBottom="25dp"
            android:layout_marginEnd="20dp"
            android:background="@mipmap/empty_star" />

        <!--品名-->
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:id="@+id/Name"
            android:layout_alignParentBottom="true"
            android:layout_alignParentStart="true"
            android:textSize="25sp"
            android:layout_marginBottom="15dp"
            android:layout_marginStart="20dp"
            android:textColor="@color/white" />
    </RelativeLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2"
        android:orientation="vertical">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <!--种类-->
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/Category"
                android:layout_marginStart="12dp"
                android:layout_marginTop="10dp"
                android:textSize="20sp"
                android:textColor="@color/category" />

            <!--富含-->
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/contain"
                android:layout_alignStart="@+id/Category"
                android:layout_below="@+id/Category"
                android:textSize="15sp"
                android:layout_marginTop="8dp"
                android:textColor="@color/nutrition" />

            <!--营养-->
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/Nutrition"
                android:layout_alignBottom="@+id/contain"
                android:layout_marginStart="10dp"
                android:textSize="15sp"
                android:layout_toEndOf="@+id/contain"
                android:textColor="@color/nutrition" />

            <!--购物车图标-->
            <Button
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:id="@+id/shoppingcart"
                android:layout_alignParentEnd="true"
                android:layout_alignTop="@+id/Category"
                android:layout_marginEnd="15dp"
                android:layout_marginTop="8dp"
                android:background="@mipmap/collect" />

            <!--竖线-->
            <View
                android:layout_width="2dp"
                android:layout_height="40dp"
                android:layout_alignTop="@+id/shoppingcart"
                android:layout_marginEnd="18dp"
                android:layout_toStartOf="@+id/shoppingcart"
                android:background="@color/line" />

            <!--横线-->
            <View
                android:layout_width="match_parent"
                android:layout_height="2dp"
                android:id="@+id/line"
                android:layout_alignStart="@+id/contain"
                android:layout_below="@+id/contain"
                android:layout_marginTop="10dp"
                android:background="@color/line" />

            <!--更多资料-->
            <ListView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/more"
                android:layout_below="@+id/line" />

           <!--横分隔线-->
            <View
                android:layout_width="match_parent"
                android:layout_height="18dp"
                android:layout_below="@+id/more"
                android:background="@color/line" />
        </RelativeLayout>

        <ListView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/listview" />
    </LinearLayout>
</LinearLayout>

information.xml

规定circle和name对的布局

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/white"
    tools:context="com.example.a13371.myapplication.InfoActivity">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:id="@+id/Top"
        android:layout_weight="1"
        android:background="@color/colorPrimaryDark">

        <!--返回键-->
        <Button
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:id="@+id/Back"
            android:layout_marginStart="10dp"
            android:background="@mipmap/back" />

        <!--星星-->
        <Button
            android:layout_width="35dp"
            android:layout_height="35dp"
            android:id="@+id/star"
            android:layout_alignParentBottom="true"
            android:layout_alignParentEnd="true"
            android:layout_marginBottom="25dp"
            android:layout_marginEnd="20dp"
            android:background="@mipmap/empty_star" />

        <!--品名-->
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:id="@+id/Name"
            android:layout_alignParentBottom="true"
            android:layout_alignParentStart="true"
            android:textSize="25sp"
            android:layout_marginBottom="15dp"
            android:layout_marginStart="20dp"
            android:textColor="@color/white" />
    </RelativeLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2"
        android:orientation="vertical">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <!--种类-->
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/Category"
                android:layout_marginStart="12dp"
                android:layout_marginTop="10dp"
                android:textSize="20sp"
                android:textColor="@color/category" />

            <!--富含-->
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/contain"
                android:layout_alignStart="@+id/Category"
                android:layout_below="@+id/Category"
                android:textSize="15sp"
                android:layout_marginTop="8dp"
                android:textColor="@color/nutrition" />

            <!--营养-->
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/Nutrition"
                android:layout_alignBottom="@+id/contain"
                android:layout_marginStart="10dp"
                android:textSize="15sp"
                android:layout_toEndOf="@+id/contain"
                android:textColor="@color/nutrition" />

            <!--购物车图标-->
            <Button
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:id="@+id/shoppingcart"
                android:layout_alignParentEnd="true"
                android:layout_alignTop="@+id/Category"
                android:layout_marginEnd="15dp"
                android:layout_marginTop="8dp"
                android:background="@mipmap/collect" />

            <!--竖线-->
            <View
                android:layout_width="2dp"
                android:layout_height="40dp"
                android:layout_alignTop="@+id/shoppingcart"
                android:layout_marginEnd="18dp"
                android:layout_toStartOf="@+id/shoppingcart"
                android:background="@color/line" />

            <!--横线-->
            <View
                android:layout_width="match_parent"
                android:layout_height="2dp"
                android:id="@+id/line"
                android:layout_alignStart="@+id/contain"
                android:layout_below="@+id/contain"
                android:layout_marginTop="10dp"
                android:background="@color/line" />

            <!--更多资料-->
            <ListView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/more"
                android:layout_below="@+id/line" />

           <!--横分隔线-->
            <View
                android:layout_width="match_parent"
                android:layout_height="18dp"
                android:layout_below="@+id/more"
                android:background="@color/line" />
        </RelativeLayout>

        <ListView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/listview" />
    </LinearLayout>
</LinearLayout>

more.xml

规定更多信息的布局

<?xml version="1.0" encoding="utf-8"?>
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/Final"
    android:textSize="23sp"
    android:textColor="@color/black"
    android:padding="12dp"
    xmlns:android="http://schemas.android.com/apk/res/android" />

colors.xml

规定颜色

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>
    <color name="white">#FFFFFF</color>
    <color name="black">#000000</color>
    <color name="category">#D5000000</color>
    <color name="nutrition">#8A000000</color>
    <color name="line">#1E000000</color>
</resources>

(3)实验遇到的困难以及解决思路


  • R标红Cannot resolve symbol R

网上说是因为数据过大,但是尝试之后并没有用,rebuild之后就好了。

  • 提示"getDrawable()方法过时"。

参照这篇博客解决。

  • Android编译时提示“Namespace ‘app’ not bound”

参照这篇博客解决。

  • 提示"getDrawable()方法过时"。

参照这篇博客解决

  • 不知道如何获取map的键值。

参照这篇博客解决

  • 在查找setimagesource的使用方法时看到了这个,所以换了一种处理方式。

参照这篇博客解决

  • 不知道如何获取字符串的第一个字。

参照这篇博客解决

  • 不知道android Intent如何传递list<Map<String,Object>>类型。

参照这篇博客解决

  • Android 解决 adapter.notifyDataSetChanged() 不起作用。

参照这篇博客这篇博客解决

  • Android将数字转换成字符串。

数字+""

  • Android无法用==判断两个字符串相等。

调用equals方法

  • 按返回键时需要按两次才能返回。

经检查不慎同时打开了两个activity。

  • 调用recyclerview时打开一片空白。

添加语句RView.setLayoutManager(new LinearLayoutManager(this));进行绑定

四、实验思考及感想


由于以前对java语言不是很熟悉,这次写代码的过程十分吃力,基本上各种方法都不会,经常要靠搜索引擎和直觉来完成代码,但有时候依靠从前从其他编程语言里面获得的经验也会掉到自己给自己挖的坑里面,比如说java判断字符串相等就不能像其他语言那样直接用’=='来实现。

这次运行程序时遇到了大量的错误,必然也需要大量的调试,在调试时,我采用的方法一般是toast弹出想要的内容,这有两个缺陷,一个是每次跑起来时间都很长(给虚拟机预个热)什么的,另一个缺点是不能查看某些非字符串的数据类型,导致我花了非常多的周折在这上面。以后还是要采取更灵活方便的调试方式,比如说设置断点和利用log什么的。

猜你喜欢

转载自blog.csdn.net/jining11/article/details/88623172