Android06-点九图的使用,聊天窗口的搭建

Nine_Patch图片:一种被特殊处理过的图片,能够制定哪些区域可以被拉伸,哪些区域不可以

  • 在Android Studio中使用.9图很简单:直接将图片名称以”.9.png”结束。
    使用.9图必须注意一点:文件的后缀名必须是.9.png,不能是.png或者是.9.png.png,这样的命名都会导致编译失败。
  • 命名完成后双击图片即可进行编辑
点九图02.gif

第一步:创建项目导入图片

第二步:创建界面布局文件

注:这里因为用到RecyclerView所以要先天际啊RecyclerView相关库的依赖。




    

    

    

        

        

第三步:创建信息窗口布局文件




    

        
    

    

        
    




第四步:创建信息实体类

package com.example.anwser_mac.uibestpractice;

/**
 * Created by anwser_mac on 2017/4/1.
 */

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 String getContent() {
        return content;
    }

    public int getType() {
        return type;
    }
}

第五步:自定义RecyclerView适配器

package com.example.anwser_mac.uibestpractice;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

/**
 * Created by anwser_mac on 2017/4/1.
 */

public class MsgAdapter extends RecyclerView.Adapter {

    private List mMsgList;

    static class Viewholder extends RecyclerView.ViewHolder {

        android.widget.LinearLayout leftLayout;

        android.widget.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 MsgAdapter.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(MsgAdapter.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();
    }
}

第六步:加载数据

package com.example.anwser_mac.uibestpractice;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import java.util.ArrayList;
import java.util.List;

public class MainActivity 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.layout);

        //初始化消息数据
        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("很高兴认识你", Msg.TYPE_RECEIVED);
        msgList.add(msg1);
        Msg msg2 = new Msg("你高兴得太早了", Msg.TYPE_SENT);
        msgList.add(msg2);
        Msg msg3 = new Msg("你丑开", Msg.TYPE_RECEIVED);
        msgList.add(msg3);
    }
}

点九图使用实例.gif

你可能感兴趣的:(Android06-点九图的使用,聊天窗口的搭建)