Android Studio 模仿美团界面

目录

activity_main.xml

shouye_layout.xml

xiaoxi_layout.xml

Wode_layout.xml

首页里面的ListView

        food_item.xml

        Food

         FoodAdapter

        Shouye

消息界面中的机器人聊天功能:

        activity_robot.xml

        msg_item.xml

        Msg.java

        MsgAdapter.java

        Robot.java

        xiaoxi.java

 “我的”界面

        Wode.java

Activity.java

运行截图

工程文件


activity_main.xml




    
    

    
        

shouye_layout.xml

第一个fragment

(代码中涉及到的一些图片,大家可以自己命名一些相同的图片放到对应的文件夹中,或者更改相应的代码,也可以到文章底部的工程文件中下载)




    
    

    
        

xiaoxi_layout.xml

第二个fragment

(代码中涉及到的一些图片,大家可以自己命名一些相同的图片放到对应的文件夹中,或者更改相应的代码,也可以到文章底部的工程文件中下载)




    
    

    
        

Wode_layout.xml

第三个fragment

(代码中涉及到的一些图片,大家可以自己命名一些相同的图片放到对应的文件夹中,或者更改相应的代码,也可以到文章底部的工程文件中下载)



    

        
            
            
            
            
            
        
        

            
            
            
            

        
    

首页里面的ListView

        food_item.xml



    

    

        Food

        在包内创建一个Java类并命名为Food,写入如下代码:

public class Food {
    private String name;
    private int imageId;
    public Food(String name, int imageId) {
        this.name = name;
        this.imageId = imageId;
    }

    public String getName() {
        return name;
    }

    public int getImageId() {
        return imageId;
    }
}

         FoodAdapter

        在包内创建一个Java类并命名为FoodAdapter,写入如下代码:

public class FoodAdapter extends ArrayAdapter {
    private int resourceId;

    public FoodAdapter(Context context, int textViewResourceId,
                       List objects){
        super(context, textViewResourceId, objects);
        resourceId = textViewResourceId;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        Food food = getItem(position);
        View view = LayoutInflater.from(getContext()).inflate(resourceId,parent,false);
        ImageView foodImage = (ImageView) view.findViewById(R.id.food_image);
        TextView foodname = (TextView) view.findViewById(R.id.food_name);
        foodImage.setImageResource(food.getImageId());
        foodname.setText(food.getName());
        return view;
    }
}

        Shouye

        在包内创建一个Java类并命名为Shouye,写入如下代码:

(代码中涉及到的一些图片,大家可以自己命名一些相同的图片放到对应的文件夹中,或者更改相应的代码,也可以到文章底部的工程文件中下载)

public class Shouye extends Fragment implements AdapterView.OnItemClickListener {
    ListView listView;
    SimpleAdapter simpleAdapter;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.shouye_layout, container, false);
        listView = view.findViewById(R.id.list_view);
        simpleAdapter = new SimpleAdapter(getActivity(),getData(),R.layout.food_item,
                new String[]{"title","image"},new int[]{R.id.food_name,R.id.food_image});
        listView.setAdapter(simpleAdapter);
        listView.setOnItemClickListener(this);

        return view;
    }

    private List> getData(){
        String [] title = {"井盒炒饭","全鸡汉堡","小煎鸡饭","猪脚饭"};
        int [] image = {R.drawable.chaofan,R.drawable.hls,R.drawable.xcj,R.drawable.zjf};
        List> list = new ArrayList<>();
        for (int j=0;j<5;j++){
            for (int i=0;i<4;i++){
                Map map = new HashMap();
                map.put("title",title[i]);
                map.put("image",image[i]);
                list.add(map);
            }
        }
        return list;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onItemClick(AdapterView parent, View view, int position, long id) {

    }

}

消息界面中的机器人聊天功能:

        activity_robot.xml




    

    

        

        

         msg_item.xml

(代码中涉及到的一些图片,大家可以自己命名一些相同的图片放到对应的文件夹中,或者更改相应的代码,也可以到文章底部的工程文件中下载)




    

        
    

    

        
    

        Msg.java

        在包内创建一个Java类文件,命名为Msg,写入如下代码:

package com.example.meituanapplication;

public class Msg {
    public static final int TYPE_RECEIVED = 0;
    public static final int TYPE_SENT = 1;
    // 消息内容
    private String content;
    // 消息类型
    private int type;

    public Msg(String content,int type){
        this.content = content;
        this.type = type;
    }

    public int getType(){
        return type;
    }

    public String getContent(){
        return content;
    }

    public void setContent(String content){
        this.content = content;
    }

    public void setType(int type){
        this.type = type;
    }
}

         MsgAdapter.java

        在包内创建一个Java类文件,命名为MsgAdapter,写入如下代码:

public class MsgAdapter extends RecyclerView.Adapter {

    private List mMsgList;
    static class ViewHolder extends RecyclerView.ViewHolder{
        LinearLayout leftLayout;
        LinearLayout rightLayout;
        TextView leftMsg;
        TextView rightMsg;
        public ViewHolder(View view){
            super(view);
            leftLayout = (LinearLayout) view.findViewById(R.id.left_layout);
            rightLayout = (LinearLayout) view.findViewById(R.id.right_layout);
            leftMsg = (TextView) view.findViewById(R.id.left_msg);
            rightMsg = (TextView) view.findViewById(R.id.right_msg);
        }
    }

    public MsgAdapter(List msgList){
        mMsgList = msgList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.msg_item,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder,int position){
        Msg msg = mMsgList.get(position);
        if (msg.getType() == Msg.TYPE_RECEIVED) {
            holder.leftLayout.setVisibility(View.VISIBLE);
            holder.rightLayout.setVisibility(View.GONE);
            holder.leftMsg.setText(msg.getContent());
        }else if (msg.getType() == Msg.TYPE_SENT){
            holder.rightLayout.setVisibility(View.VISIBLE);
            holder.leftLayout.setVisibility(View.GONE);
            holder.rightMsg.setText(msg.getContent());
        }
    }

    @Override
    public int getItemCount() {
        return mMsgList.size();
    }
}

        Robot.java

        在包内创建一个Java类文件,命名为Robot,写入如下代码:

public class Robot extends AppCompatActivity {
    private List msgList = new ArrayList<>();
    private EditText inputText;
    private Button send;
    private RecyclerView msgRecyclerView;
    private MsgAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_robot);

        initMsgs();//初始化消息数据
        inputText = (EditText) findViewById(R.id.input_text);
        send = (Button) findViewById(R.id.send);
        msgRecyclerView = (RecyclerView) findViewById(R.id.msg_recycler_view);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        msgRecyclerView.setLayoutManager(layoutManager);
        adapter = new MsgAdapter(msgList);
        msgRecyclerView.setAdapter(adapter);
        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String content = inputText.getText().toString();
                if (!"".equals(content)) {
                    Msg msg = new Msg(content,Msg.TYPE_SENT);
                    msgList.add(msg);
                    adapter.notifyItemInserted(msgList.size() - 1);
                    msgRecyclerView.scrollToPosition(msgList.size() - 1);
                    inputText.setText("");
                }
            }
        });
    }

    private void initMsgs() {
        Msg msg1 = new Msg("long may the sun shine!",Msg.TYPE_RECEIVED);
        msgList.add(msg1);
        Msg msg2 = new Msg("you are dragon, more dragon thsn me.",Msg.TYPE_SENT);
        msgList.add(msg2);
        Msg msg3 = new Msg("如果你是龙,也好",Msg.TYPE_RECEIVED);
        msgList.add(msg3);
    }
}

         xiaoxi.java

         在包内创建一个Java类文件,命名为Robot,写入如下代码:

public class Xiaoxi extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.xiaoxi_layout, container, false);

        LinearLayout nx = view.findViewById(R.id.xiaoxi_nie);
        nx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(),Robot.class);
                startActivity(intent);
            }
        });
        return view;
    }
}

 “我的”界面

        Wode.java

        在包内创建一个Java类文件,命名为Wode,写入如下代码:

public class Wode extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.wode_layout, container, false);
        return view;
    }
}

Activity.java

最后是主要的活动的代码:

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    //三个fragment
    private Shouye f1;
    private Xiaoxi f2;
    private Wode f3;

    //底部三个按钮
    private Button foot1;
    private Button foot2;
    private Button foot3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        foot1 = (Button) findViewById(R.id.btn1);
        foot2 = (Button) findViewById(R.id.btn3);
        foot3 = (Button) findViewById(R.id.btn4);
        foot1.setOnClickListener(this);
        foot2.setOnClickListener(this);
        foot3.setOnClickListener(this);

        //第一次初始化首页默认显示第一个fragment
        initShouye();
    }

    //显示第一个fragment
    private void initShouye(){
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (f1 == null){
            f1 = new Shouye();
            transaction.add(R.id.main_frame_layout,f1);
        }
        hideFragment(transaction);
        transaction.show(f1);
        transaction.commit();
    }

    //显示第二个fragment
    private void initXiaoxi(){
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        if(f2 == null){
            f2 = new Xiaoxi();
            transaction.add(R.id.main_frame_layout,f2);
        }
        hideFragment(transaction);
        transaction.show(f2);
       transaction.commit();
    }

    //显示第三个fragment
    private void initWode(){
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        if(f3 == null){
            f3 = new Wode();
            transaction.add(R.id.main_frame_layout,f3);
        }
        hideFragment(transaction);
        transaction.show(f3);
        transaction.commit();
    }



    //隐藏所有的fragment
    private void hideFragment(FragmentTransaction transaction){
        if(f1 != null){
            transaction.hide(f1);
        }
        if(f2 != null){
            transaction.hide(f2);
        }
        if(f3 != null){
            transaction.hide(f3);
        }
    }

    @Override
    public void onClick(View v) {
        if(v == foot1){
            initShouye();
        }else if(v == foot2){
            initXiaoxi();
        }else if(v == foot3){
            initWode();
        }
    }

}

运行截图

Android Studio 模仿美团界面_第1张图片    Android Studio 模仿美团界面_第2张图片

Android Studio 模仿美团界面_第3张图片    Android Studio 模仿美团界面_第4张图片

工程文件

下面是整个项目工程的文件压缩包,如果需要的话可以通过积分下载:

AndroidStudio模仿美团界面-Android文档类资源-CSDN下载用AndroidStudio模仿美团界面做一个app更多下载资源、学习资料请访问CSDN下载频道.https://download.csdn.net/download/nazonomaster/85219998

你可能感兴趣的:(android,studio,实验,android,studio,android-studio)