Android之UIBestPractice

一、下面會用到RecyclerView,在app/build.gradle的dependencies添加依賴庫

implementation 'androidx.recyclerview:recyclerview:1.1.0'

二、activity_main.xml:編寫主界面

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

    <!--指定寬度爲match_parent,由於圖片的寬度不足以填滿整個屏幕的寬度
    整張圖片被均勻地拉伸,效果非常差

    這時可以使用Nine-Patch來進行改善-->

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

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

        <EditText
            android:id="@+id/input_text"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:hint="Type something here"
            android:maxLines="2"
            />

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

    </LinearLayout>


</LinearLayout>

三、Msg類:消息的實體類

package com.example.uibestpractice;

public class Msg {

    public static final int TYPE_RECEIVED=0;

    public static final int TYPE_SEND=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;
    }
}

四、msg_item.xml:編寫RecyclerView子項的佈局

<?xml version="1.0" encoding="utf-8"?>
<!--RecyclerView子項的佈局-->
<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="#000"/>

    </LinearLayout>

    <!-- android:layout_gravity="left" 收到的消息居左對齊   -->

    <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"
            android:textColor="#000"
            />

    </LinearLayout>

    <!-- android:layout_gravity="right" 發出的消息居右對齊   -->

</LinearLayout>

五、MsgAdapter:RecyclerView的適配器

package com.example.uibestpractice;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.util.List;

public class MsgAdapter extends RecyclerView.Adapter<MsgAdapter.ViewHolder> {

    private List<Msg> mMsgList;

    /**
     * 內部類
     */
    static class ViewHolder extends RecyclerView.ViewHolder {


        LinearLayout leftLayout;
        LinearLayout rightLayout;
        TextView leftMsg;
        TextView rightMsg;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            leftLayout=(LinearLayout) itemView.findViewById(R.id.left_layout);
            rightLayout=(LinearLayout)itemView.findViewById(R.id.right_layout);

            leftMsg=(TextView)itemView.findViewById(R.id.left_msg);
            rightMsg=(TextView)itemView.findViewById(R.id.right_msg);
        }
    }


    public MsgAdapter(List<Msg> msgList){
        mMsgList = msgList;
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull 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(@NonNull 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{
            holder.rightLayout.setVisibility(View.VISIBLE);
            holder.leftLayout.setVisibility(View.GONE);
            holder.rightMsg.setText(msg.getContent());
        }

    }

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


}

六、MainActivity:爲RecyclerView初始化一些數據,給發送按鈕加入事件響應

package com.example.uibestpractice;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

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

/**
 * 可以爲RecyclerView初始化一些數據
 */
public class MainActivity extends AppCompatActivity {

    //消息List
    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.activity_main);

        //初始化數據
        initMsgs();

        //獲取控件EditText Button RecyclerView
        inputText = (EditText)findViewById(R.id.input_text);
        send = (Button)findViewById(R.id.send);
        msgRecyclerView=(RecyclerView)findViewById(R.id.msg_recycler_view);


        //this--->MainActivity
        //context:上下文,初始化時,用於構造方法內部加載資源
        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) {

                //獲取在EditText輸入的內容
                String content=inputText.getText().toString();

                //如果內容不爲空
                if(!"".equals(content)){

                    //將消息初始化爲發送消息
                    Msg msg=new Msg(content,Msg.TYPE_SEND);

                    //添加數據
                    msgList.add(msg);

                    //當有新消息時,刷新Lis†View中的顯示
                    adapter.notifyItemInserted(msgList.size()-1);

                    //將ListView定位到最後一行
                    msgRecyclerView.scrollToPosition(msgList.size()-1);

                    //清空輸入框的內容
                    inputText.setText("");
                }
            }
        });


    }


    /*
    初始化幾條數據,用於在RecyclerView中顯示
     */
    private void initMsgs() {

        Msg msg1=new Msg("Hello guy.",Msg.TYPE_RECEIVED);
        msgList.add(msg1);

        Msg msg2=new Msg("Hello. Who is that?",Msg.TYPE_SEND);
        msgList.add(msg2);

        Msg msg3=new Msg("This is Tom. Nice talking to you.",Msg.TYPE_RECEIVED);
        msgList.add(msg3);

    }

}

七、效果圖

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章