美文网首页
Android第一行代码3.7.2

Android第一行代码3.7.2

作者: SJoker_0105 | 来源:发表于2018-10-24 15:45 被阅读0次

    1.聊天界面

    在app/build.gradle中添加依赖库

     implementation'com.android.support:recyclerview-v7:28.0.0'

    修改activity_mian_xml代码

    1.android.support.v7.widget.RecyclerView 的完整包路径。

    定义实体类Msg                 content表示内容 ,type表示类型:发送,接收。

    public class Msg {

    public static final int TYPE_RECEIVED=0;

    public static final int TYPE_SENT=0;

    private Stringcontent;

    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;

    }

    }

    新建一个item.xml 为RecyclerVIew的子项

    包含两个对话图片用两个(Linearlayout)

    创建RecyclerVIew的适配器MsgAdapter

    public class MsgAdapterextends RecyclerView.Adapter {

    private List<Msg>  mMsglist;

    static class ViewHolderextends RecyclerView.ViewHolder{//避免findviewbyid重复注册

    LinearLayoutleftLayout;//成员变量

    LinearLayoutrightLayout;

    TextViewleftmsg;

    TextViewrightmsg;

    public ViewHolder(View view){//封装到Viewholder里面

    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;//把要展示的数据赋值给全局变量

    }

    @NonNull

    @Override

        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent,int viewType) {//oncreate创建Viewholder的实例

    View view=LayoutInflater.from(prent.getContext()).inflate(R.layout.msg_item,parent,false);

    return new ViewHolder(view);

    }

    @Override

        public void onBindViewHolder(@NonNull ViewHolder holder,int position) {bin方法是对子项数据赋值

    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.leftLayout.setVisibility(View.GONE);

    holder.rightLayout.setVisibility(View.VISIBLE);

    holder.rightmsg.setText(msg.getContent());

    }

    }

    @Override

        public int getItemCount() {//告诉我们一共有多少子项

    return mMsglist.size();

    }

    }

    最后是MainActivity中的代码

    public class MainActivityextends 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.activity_main);

    inirMsgs();//初始化数据

    inputText=(EditText)findViewById(R.id.input_text);

    send=(Button)findViewById(R.id.send);

    msgRecyclerView=(RecyclerView)findViewById(R.id.msg_parent);

    LinearLayoutManager layoutManager=new LinearLayoutManager(this);

    msgRecyclerView.setAdapter(adapter);//绑定适配器

    Log.d("MainActivity" ,"adpter");

    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("");//清空数据

    }

    Toast.makeText(MainActivity.this,"true",Toast.LENGTH_SHORT).show();

    }

    });

    }

    private void inirMsgs(){

    Msg msg1=new Msg("1",Msg.TYPE_RECEIVED);

    msgList.add(msg1);

    Msg msg2=new Msg("2",Msg.TYPE_SENT);

    msgList.add(msg2);

    Msg msg3=new Msg("3",Msg.TYPE_RECEIVED);

    msgList.add(msg3);

    }

    }

    logcat日志显示错误:No adapter attached; skipping layout

    1.没有绑定适配器 setAdpter()已绑定

    2.布局设置成RelativeLayout    没有用

    3.使用 msgRecyclerView.setLayoutManager(layoutManager);无用

    解决  private MsgAdapter   adapter;定义全局变量

    MsgAdapter adapter=new MsgAdapter(msgList);//创建adpter的实例

    相关文章

      网友评论

          本文标题:Android第一行代码3.7.2

          本文链接:https://www.haomeiwen.com/subject/rhxxtqtx.html