美文网首页
第三章--UI开发的点点滴滴

第三章--UI开发的点点滴滴

作者: dong_hui | 来源:发表于2017-03-17 00:55 被阅读222次

    1.几个控件

    • ProgressBar横向
        <ProgressBar
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            style="?android:attr/progressBarStyleHorizontal"
            android:max="100"
            android:progress="20"
            />
    
    • ProgressDialog 对话框进度条

    2.几个布局

    • 相对布局中:
      layout_alignParentTop,layout_alignParentBottom …… layout_centerInParent 相对父布局,上下左右中。<b/>
      layout_above …… 相对于控件上下左右

    • gradle引包,使用百分比布局

    3.简单自定义一个标题栏

    • how ?
    public class MyTitle  extends LinearLayout {
    
        private TextView  title_tv;
    
        public MyTitle(Context context, AttributeSet attrs) {
            super(context, attrs);
            LayoutInflater.from(context).inflate(R.layout.mytitle,this);
            findViewById(R.id.back_btn).setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    ((Activity)getContext()).finish();
                }
            });
            findViewById(R.id.setting_btn).setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
    
                }
            });
            title_tv= (TextView) findViewById(R.id.title_tv);
        }
    
        public void setTitle(String titleTxt){
                 title_tv.setText(titleTxt);
        }
        
    }
    
    
    • to use
        <com.study.dh.lockapp.myview.MyTitle
            android:id="@+id/title_mt"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        </com.study.dh.lockapp.myview.MyTitle>
    
         private MyTitle  title_mt;
       ……
            title_mt= (MyTitle) findViewById(R.id.title_mt);
            title_mt.setTitle("登陆");
       ……
    

    4. RecycleListView的使用

    • 简单使用
    • 横向展示
    • 瀑布流显示 可用于展示不同大小的item
           StaggeredGridLayoutManager  staggeredGridLayoutManager=new
                    StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL);
    
        //    LinearLayoutManager  linearLayoutManager=new LinearLayoutManager(this);
         //   linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);   //横向滚动
            fruit_rv.setLayoutManager(staggeredGridLayoutManager);
    
    • 点击事件
    public class FruitAdapter extends RecyclerView.Adapter<FruitAdapter.ViewHolder> {
        private List<Fruit> fruitList;
        public FruitAdapter(List<Fruit> fruitList) {
            this.fruitList = fruitList;
        }
    
        @Override
        public FruitAdapter.ViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {
            View  view= LayoutInflater.from(parent.getContext()).inflate(R.layout.fruit_horizontal_item,parent,false);
            final ViewHolder  holder=new ViewHolder(view);
            holder.fruitView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int position=holder.getAdapterPosition();
                    Fruit fruit=fruitList.get(position);
                    Toast.makeText(view.getContext(),"click item"+fruit.getName(),Toast.LENGTH_SHORT).show();
                }
            });
            holder.fruitImag.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int position=holder.getAdapterPosition();
                    Fruit fruit=fruitList.get(position);
                    Toast.makeText(view.getContext(),"click  img"+fruit.getName(),Toast.LENGTH_SHORT).show();
                }
            });
            return holder;
        }
    
        @Override
        public void onBindViewHolder(FruitAdapter.ViewHolder holder, int position) {
                 Fruit  fruit=fruitList.get(position);
                 holder.fruitTxt.setText(fruit.getName());
                 holder.fruitImag.setImageResource(fruit.getImgId());
        }
    
        @Override
        public int getItemCount() {
            return fruitList.size();
        }
    
        static class ViewHolder extends RecyclerView.ViewHolder {
            View  fruitView;
            ImageView  fruitImag;
            TextView   fruitTxt;
    
            public ViewHolder(View itemView) {
                super(itemView);
                fruitView=itemView;
                fruitImag= (ImageView) itemView.findViewById(R.id.fruitImg);
                fruitTxt= (TextView) itemView.findViewById(R.id.fruitTxt);
            }
        }
    }
    
    

    注: RecyclerView的可以轻松实现子项中任意空间或布局的点击事件

    5.实例--聊天界面的编写

    • Nine-Patch图片的制作:android -sdk下的tool工具,打开Nine-Patch制作图片即可。

    • recycleview刷新发送消息, 增加数据,和定位光标 ,清空输入框

        @Override
        public void onClick(View view) {
              switch (view.getId()){
                  case R.id.sendmsg_btn:
                        String content=input_et.getText().toString();
                      if (!content.equals("")){
                          Random  random=new Random();
                          Msg msg=new Msg(content,random.nextInt(2));
                          msgs.add(msg);
                          msgAdapter.notifyItemInserted(msgs.size()-1);   //刷新新消息
                          fruit_rv.scrollToPosition(msgs.size()-1);    //将recycleview定位到最后一行
                          input_et.setText("");
                      }
                      break;
              }
        }
    
    • adapter 做个消息类型的区分即可
    
    public class MsgAdapter extends RecyclerView.Adapter<MsgAdapter.ViewHolder> {
        private List<Msg>  list;
    
    
        public MsgAdapter(List<Msg> list) {
            this.list = list;
        }
    
        @Override
        public 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(ViewHolder holder, int position) {
                Msg  msg=list.get(position);
            if (msg.getType()==Msg.TYPE_RECEIVED){
                 holder.right_layout.setVisibility(View.GONE);
                 holder.left_layout.setVisibility(View.VISIBLE);
                 holder.leftMsg_tv.setText(msg.getContent());
            }else if (msg.getType()==Msg.TYPE_SEND){
                holder.right_layout.setVisibility(View.VISIBLE);
                holder.left_layout.setVisibility(View.GONE);
                holder.rightMsg_tv.setText(msg.getContent());
            }
        }
    
        @Override
        public int getItemCount() {
            return list.size();
        }
    
        static  class ViewHolder  extends RecyclerView.ViewHolder{
            LinearLayout  right_layout;
            LinearLayout  left_layout;
            TextView    rightMsg_tv;
            TextView    leftMsg_tv;
            public ViewHolder(View itemView) {
                super(itemView);
                right_layout= (LinearLayout) itemView.findViewById(R.id.right_layout);
                left_layout= (LinearLayout) itemView.findViewById(R.id.left_layout);
                rightMsg_tv= (TextView) itemView.findViewById(R.id.rightMsg_tv);
                leftMsg_tv= (TextView) itemView.findViewById(R.id.leftMsg_tv);
            }
        }
    
    }
    
    

    相关文章

      网友评论

          本文标题:第三章--UI开发的点点滴滴

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