Android06-點九圖的使用,聊天視窗的搭建

weixin_33751566發表於2017-04-02

Nine_Patch圖片:一種被特殊處理過的圖片,能夠制定哪些區域可以被拉伸,哪些區域不可以

  • 在Android Studio中使用.9圖很簡單:直接將圖片名稱以”.9.png”結束。
    使用.9圖必須注意一點:檔案的字尾名必須是.9.png,不能是.png或者是.9.png.png,這樣的命名都會導致編譯失敗。
  • 命名完成後雙擊圖片即可進行編輯
2897594-d160dcf304819bca.gif
點九圖02.gif

第一步:建立專案匯入圖片

第二步:建立介面佈局檔案

注:這裡因為用到RecyclerView所以要先天際啊RecyclerView相關庫的依賴。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#d8e0e8"
    >

    <android.support.v7.widget.RecyclerView
        android:id="@+id/msg_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

    </android.support.v7.widget.RecyclerView>

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

        <EditText
            android:id="@+id/input_text"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_marginBottom="0dp"
            android:hint="請輸入資訊"
            android:maxLines="2"/>

        <Button
            android:id="@+id/send"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Send"/>
    </LinearLayout>
</LinearLayout>

第三步:建立資訊視窗布局檔案

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp">

    <LinearLayout
        android:id="@+id/left_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:background="@drawable/message_left">

        <TextView
            android:id="@+id/left_msg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="10dp"
            android:textColor="#fff"/>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/right_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:background="@drawable/message_right">

        <TextView
            android:id="@+id/right_msg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="10dp" />
    </LinearLayout>


</LinearLayout>

第四步:建立資訊實體類

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<MsgAdapter.Viewholder> {

    private List<Msg> 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<Msg> 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<Msg> 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);
    }
}

2897594-0cec6ca9de51851d.gif
點九圖使用例項.gif

相關文章