一些控件
Textview:
EditText:
ImageView:
ProgressBar:转圈那个
AlertDialog:对话框
ProgressDialog:ProgeressDialog过时这个问题,网上很多博主给出的解决方案都是:用progressBar代替。但是使用progressBar代替,函数需要大改,使用上也不太一样,把ProgressDialog改成AlertDialog就可以用了
一些布局
LinearLayout:weight
RelativeLayout:相对布局
layout_above/below:在。。
layout_toLeftOf=“sb” 在sb左边
layout_alignBottom=“sb” 跟sb下面对齐
FrameLayout:
PercentRealtiveLayout:
PercentFrameLayout:
ListVIew:ArrayAdapter
RecyclerView:RecyclerView.Adapter<RecyclerView.ViewHolder>
自定义控件
<include layout="@layout/test_include"/>
public class TestInclude extends LinearLayout {
private TextView textView;
private Button button;
public TestInclude(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.test_include,this);
textView = findViewById(R.id.textView);
button = findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getContext(),"click button",Toast.LENGTH_SHORT).show();
}
});
}
}
ListView
看源码->listview->基础->用法
简单用法
public class MainActivity extends AppCompatActivity {
private String[] data = {"a","b","c","d","e","f","g","h","i","j","k","l"};
private ListView listView;
private ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getSupportActionBar();
if(actionBar != null){
actionBar.hide();
}
listView = findViewById(R.id.list_view);
adapter = new ArrayAdapter<>(MainActivity.this,R.layout.item,data);
listView.setAdapter(adapter);
}
}
RecyclerView
自己敲的:
MainActivity :
public class MainActivity extends AppCompatActivity {
private RecyclerView rv_main;
private List<Fruit> fruitList = new ArrayList<>();
private FruitAdapter fruitAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化
rv_main = findViewById(R.id.rv_main);
init();
LinearLayoutManager manager = new LinearLayoutManager(MainActivity.this);
fruitAdapter = new FruitAdapter(fruitList);
//设置资源
manager.setOrientation(LinearLayoutManager.HORIZONTAL);
// rv_main.setLayoutManager(manager);
//rv_main.setLayoutManager(new GridLayoutManager(MainActivity.this,6));//5列
//rv_main.setLayoutManager(new StaggeredGridLayoutManager(7,LinearLayoutManager.HORIZONTAL));//7hang
rv_main.setLayoutManager(new StaggeredGridLayoutManager(7,LinearLayoutManager.VERTICAL));//7lie
//设置adapter
rv_main.setAdapter(fruitAdapter);
}
private void init() {
for (int i = 0; i < 20; i++) {
Fruit f = new Fruit(R.drawable.a,"apple");
fruitList.add(f);
Log.d("TAG", f.toString());
Log.d("TAG", fruitList.size()+"");
}
}
}
Fruit:
package com.example.myrecycleview;
public class Fruit {
private int imageViewid;
private String name;
public Fruit(int imageView, String name) {
this.imageViewid = imageView;
this.name = name;
}
public int getImageView() {
return imageViewid;
}
public void setImageView(int imageView) {
this.imageViewid = imageView;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Fruit{" +
"imageViewid=" + imageViewid +
", name='" + name + '\'' +
'}';
}
}
FruitAdapter:
public class FruitAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<Fruit> mlist;
//初始化
static class ViewHolder extends RecyclerView.ViewHolder{
View v;
ImageView imageView;
TextView textView;
public ViewHolder(@NonNull View itemView) {
super(itemView);
v = itemView;
imageView = itemView.findViewById(R.id.iv_item);
textView = itemView.findViewById(R.id.tv_item);
}
}
public FruitAdapter(List<Fruit> list) {
mlist = list;
}
//返回holder
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item,parent,false);
ViewHolder vh = new ViewHolder(view);
vh.v.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int p = vh.getAdapterPosition();
Fruit fruit = mlist.get(p);
Toast.makeText(parent.getContext(),"click view" +fruit.getName(),Toast.LENGTH_SHORT).show();
}
});
vh.imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int p = vh.getAdapterPosition();
Fruit fruit = mlist.get(p);
Toast.makeText(parent.getContext(),"click view" +fruit.getName(),Toast.LENGTH_SHORT).show();
}
});
return vh;
}
//设置资源
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
ViewHolder viewHolder = (ViewHolder) holder;
Fruit fruit = mlist.get(position);
viewHolder.imageView.setImageResource(fruit.getImageView());
viewHolder.textView.setText(fruit.getName());
}
@Override
public int getItemCount() {
return mlist.size();
}
}
练习项目
练习聊天项目:
MainActivity:
public class MainActivity extends AppCompatActivity {
private List<MyMsg> myMsgList = new ArrayList<>();
private RecyclerView rv_main;
private MyMsgAdapter adapter;
private LinearLayoutManager linearLayoutManager;
private EditText tv_main;
private Button btn_main;
private LinearLayout ll_main;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
rv_main = findViewById(R.id.rv_main);
linearLayoutManager = new LinearLayoutManager(MainActivity.this);
adapter = new MyMsgAdapter(myMsgList);
tv_main = findViewById(R.id.tv_main);
btn_main = findViewById(R.id.btn_main);
ll_main = findViewById(R.id.ll_main);
rv_main.setLayoutManager(linearLayoutManager);
rv_main.setAdapter(adapter);
btn_main.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String s = tv_main.getText().toString();
if(!s.equals("")){
MyMsg msg = new MyMsg(MyMsg.TYPE_RIGHT_SEND,s);
myMsgList.add(msg);
adapter.notifyItemInserted(myMsgList.size()-1);
rv_main.scrollToPosition(myMsgList.size()-1);
tv_main.setText("");
}
}
});
}
private void init() {
MyMsg msg = new MyMsg(MyMsg.TYPE_LEFT_RECIVER,"hello,i am left");
myMsgList.add(msg);
msg = new MyMsg(MyMsg.TYPE_RIGHT_SEND,"i am right");
myMsgList.add(msg);
}
}
MyMsg:
public class MyMsg {
int type;
String content;
static final int TYPE_RIGHT_SEND = 1;
static final int TYPE_LEFT_RECIVER = 2;
public MyMsg(int type, String content) {
this.type = type;
this.content = content;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
MyMsgAdapter
class MyMsgAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
class MyViewHolder extends RecyclerView.ViewHolder{
LinearLayout linearLayoutright;
LinearLayout linearLayoutleft;
TextView textViewright;
TextView textViewleft;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
linearLayoutleft = itemView.findViewById(R.id.ll_left);
linearLayoutright = itemView.findViewById(R.id.ll_right);
textViewright = itemView.findViewById(R.id.tv_item_right);
textViewleft = itemView.findViewById(R.id.tv_item_left);
}
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item,parent,false);
myViewholder = new MyViewHolder(view);
return myViewholder;
}
private MyViewHolder myViewholder;
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {//卡了
MyMsg msg = mlist.get(position);
myViewholder = (MyViewHolder) holder;
if(msg.getType() == msg.TYPE_RIGHT_SEND){
myViewholder.textViewright.setText(msg.getContent());
myViewholder.linearLayoutright.setVisibility(View.VISIBLE);
myViewholder.linearLayoutleft.setVisibility(View.INVISIBLE);
}else if(msg.getType() == msg.TYPE_LEFT_RECIVER){
myViewholder.textViewleft.setText(msg.getContent());
myViewholder.linearLayoutright.setVisibility(View.INVISIBLE);
myViewholder.linearLayoutleft.setVisibility(View.VISIBLE);
}
}
private List<MyMsg> mlist;
public MyMsgAdapter(List<MyMsg> list) {
mlist = list;
}
@Override
public int getItemCount() {
return mlist.size();
}
}