RecyclerView实现聊天界面

 目前在根据《第一行代码》学习安卓开发,没有恒心断断续续的,所以准备通过写博客来促进自己学习。从第三章RecyclerView开始,会把学习过程中遇到的问题和解决方法都写出来,希望自己能有收获。

1.首先在app/build.grade当中添加依赖库

 

dependencies {

    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    compile 'com.android.support:recyclerview-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}

添加完后SyncNow同步一下,需要注意recyclerview后面跟的版本号需要和appcompat的版本号一致才可以,否则同步会报错。PS:从网上找了好久,最后尝试了一下没想到好了。

2.制作气泡图片,百度一下找了两个气泡图片,一左一右。右键图片 create 9-patch file,打开生成的文件,

RecyclerView实现聊天界面_第1张图片RecyclerView实现聊天界面_第2张图片

3.编写主界面,修改activity_main.xml代码,如下:



    
    
        
        

界面如下:

RecyclerView实现聊天界面_第3张图片

4.编写Msg实体类,代码如下:

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;
    }
}

5.接下来写RecyclerView子项的布局,新建msg_item.xml,代码如下



        

            
        

        

            
        

    


界面如下:

RecyclerView实现聊天界面_第4张图片

6.编写RecyclerView的适配类,新建类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)//传入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 list)
    {
        mMsgList=list;
    }
    public ViewHolder onCreateViewHolder(ViewGroup parent,int viewType)
    {
        View view=LayoutInflater.from(parent.getContext()).inflate(R.layout.msg_item,parent,false);
        return new ViewHolder(view);
    }//创建ViewHolder实例,加载msg_item布局,
    public void onBindViewHolder(ViewHolder holder,int position)
    {
        Msg msg=mMsgList.get(position);//获取当前的Msg实例
        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.leftLayout.setVisibility(View.GONE);
            holder.rightLayout.setVisibility(View.VISIBLE);
            holder.rightMsg.setText(msg.getContent());
        }
    }//对RecyclerView的子项的数据进行赋值,会在每个子项被滚动到屏幕时执行。
    public int getItemCount()
    {
        return mMsgList.size();//告诉RecyclerView一共有多少子项。直接返回数据源长度。
    }
}
7.
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.activity_main);
        initMsg();
        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);
        //LayoutManager用来指定RecyclerView的布局方式,layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL)设置横向滚动,StaggeredLayoutManager layoutManager=new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL) 可以设置3列的瀑布排列
        msgRecyclerView.setLayoutManager(layoutManager);
        adapter=new MsgAdapter(msgList);
        msgRecyclerView.setAdapter(adapter);
        send.setOnClickListener(new View.OnClickListener(){
            public void onClick(View view)
            {
                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("");
                }
            }
        });
    }
    public void initMsg()//初始化信息
    {
        Msg msg1=new Msg("hello guy",Msg.TYPE_RECEIVED);
        msgList.add(msg1);
        Msg msg2=new Msg("hello guy",Msg.TYPE_SENT);
        msgList.add(msg2);
        Msg msg3=new Msg("hello guy",Msg.TYPE_RECEIVED);
        msgList.add(msg3);
    }
}


运行结果如下:

RecyclerView实现聊天界面_第5张图片

你可能感兴趣的:(Android)