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

2017-03-17  本文已影响222人  dong_hui

1.几个控件

    <ProgressBar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="?android:attr/progressBarStyleHorizontal"
        android:max="100"
        android:progress="20"
        />

2.几个布局

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

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

    <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的使用

       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.实例--聊天界面的编写

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

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

}

上一篇下一篇

猜你喜欢

热点阅读