百分比布局:
在build.gradle中添加百分比布局的依赖时由于版本的不同
原句:compile ‘com.android.support:percent:24.2.1’
改为:implementation ‘androidx.percentlayout:percentlayout:1.0.0’
引入布局:
新建一个name.xml文件后,在需要添加该布局的xml中直接添加
代码:
创建自定义控件:
在创建好布局后,再新建一个类 TitleLayout (该控件的点击事件也写道这个类里),该类继承自 LinearLayout,
代码如下:
public class TitleLayout extends LinearLayout {
public TitleLayout(Context context, AttributeSet attrs)
{
super(context,attrs);
LayoutInflater.from(context).inflate(R.layout.title,this,true);
Button titleBack=(Button)findViewById(R.id.title_back);
Button titleEdit=(Button)findViewById(R.id.title_edit);
titleBack.setOnClickListener(new MyClickListener());
titleEdit.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
((Activity)getContext()).finish();
}
});
}
public class MyClickListener implements View.OnClickListener{
@Override
public void onClick(View v)
{
((Activity)getContext()).finish();
}
}
}
在构造函数中对标题栏(即刚刚自己加的布局)进行动态加载,这就需要LayoutInflater来实现。通过LayoutInflater的from()方法可以构造出一个LayoutInflater对象,然后调用inflate()方法就可以加载一个布局文件。inflate()方法接受两个参数,一个是要加载的布局文件的id,另一个是给加载好布局添加一个父布局
引用到activity_main.xml时注意此时不同于引入布局那样的方法,这时要把完整包名写出来,如:
<com.example.myapplication.TitleLayout
android:layout_width=“match_parent”
android:layout_height=“wrap_content”/>
最后在 TitleLayout 中添加点击事件。
ListView:
- 简单用法
先加入一个ListView
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
接着在MainActivity中添加一个数据,再创建适配器,适配器里要包含, 1、当前上下文,2、ListView的子布局,3、和适配的数据。然后通过setAdapter加入数据。
2.定制ListView的界面
先定义一个实体类
public class Fruit{
private String name;
private int imageId;
public Fruit(String name,int imageId){
this.name=name;
this.imageId=imageId;
}
public String getName(){
return name;
}
public int getImageId(){
return imageId;
}
}
然后为ListView的子布局自定义布局fruit_item.xml(前面有讲到适配器要包括ListView的子布局)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/fruit_image"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#008577"/>/>
<TextView
android:id="@+id/fruit_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:background="#008577"/>
</LinearLayout>
然后我们创建一个自定义适配器 继承自ArrayAdapter,泛型指定为Fruit类
public class FruitAdapter extends ArrayAdapter<MainActivity.Fruit> {
private int resourceId;
public FruitAdapter(Context context, int textViewResourceId, List<MainActivity.Fruit> objects){
super(context,textViewResourceId,objects);
resourceId=textViewResourceId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
MainActivity.Fruit fruit=getItem(position);
View view;
ViewHolder viewHolder;
if(convertView==null){
view= LayoutInflater.from(getContext()).inflate(resourceId,parent,false);
viewHolder=new ViewHolder();
viewHolder.fruitImage=(ImageView)view.findViewById(R.id.fruit_image);
viewHolder.fruitName=(TextView)view.findViewById(R.id.fruit_name);
view.setTag(viewHolder);
}else{
view=convertView;
viewHolder=(ViewHolder)view.getTag();
}
viewHolder.fruitImage.setImageResource(fruit.getImageId());
viewHolder.fruitName.setText(fruit.getName());
return view ;}
class ViewHolder{
ImageView fruitImage;
TextView fruitName;
}
}
然后添加MainActivity中的代码
public class MainActivity extends AppCompatActivity {
private List<Fruit>fruitList=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initFruits();
FruitAdapter adapter=new FruitAdapter(MainActivity.this,R.layout.fruit_item,fruitList);
ListView listView=(ListView)findViewById(R.id.list_view);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Fruit fruit=fruitList.get(i);
Toast.makeText(MainActivity.this,fruit.getName(),Toast.LENGTH_SHORT).show();
}
});
}
private void initFruits(){
for(int i=0;i<2;i++){
Fruit apple=new Fruit("Apple",R.drawable.apple_pic);
fruitList.add(apple);
Fruit banana=new Fruit("Banana",R.drawable.banana_pic);
fruitList.add(banana);
Fruit orange=new Fruit("Orange",R.drawable.orange_pic);
fruitList.add(orange);
Fruit watermelon=new Fruit("Watermelon",R.drawable.watermelon_pic);
fruitList.add(watermelon);
Fruit pear=new Fruit("Pear",R.drawable.pear_pic);
fruitList.add(pear);
Fruit grape=new Fruit("Grape",R.drawable.grape_pic);
fruitList.add(grape);
Fruit pineapple=new Fruit("Pineapple",R.drawable.pineapple_pic);
fruitList.add(pineapple);
Fruit strawberry=new Fruit("Strawberry",R.drawable.strawberry_pic);
fruitList.add(strawberry);
Fruit cherry=new Fruit("Cherry",R.drawable.cherry_pic);
fruitList.add(cherry);
Fruit mango=new Fruit("Mango",R.drawable.mango_pic);
fruitList.add(mango);
}
}
}
ListView 的点击事件也包含在上述代码中。
RecyclerView
RecyclerView属于新加的控件,需要在build.gradle中添加相应的依赖库
由于版本的原因,书上原句:compile ‘com.android:recyclerview-v7:24.2.1’
改为:implementation ‘androidx.recyclerview:recyclerview:1.1.0’
然后再activity_main.xml中添加RecyclerView控件(RecyclerView不是内置在系统SDK中的,所以需要把完整的包名写出来)
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
同样的先给RecyclerView准备一个适配器,新建FruitAdapter类,让其继承自RecyclerView.Adapter
将泛型指为FruitAdapter.ViewHolder ,ViewHolder是我们在Fr/uitAdapter中定义 的一个内部类
代码如下:
public class FruitAdapter extends RecyclerView.Adapter<FruitAdapter.ViewHolder> {
private List<Fruit>mFruitList;
static class ViewHolder extends RecyclerView.ViewHolder{
View fruitView;
ImageView fruitImage;
TextView fruitName;
public ViewHolder(View view){//通常这个参数就是RecyclerView子布局的布局
super(view);
fruitView=view;
fruitName=(TextView)view.findViewById(R.id.fruit_name);
fruitImage=(ImageView)view.findViewById(R.id.fruit_image);
}
}
public FruitAdapter(List<Fruit>fruitList){
mFruitList=fruitList;//将要展示的数据传进来,赋值给全局变量mFruitList后续的操作都在这个数据源上
}
@Override//onBindViewHolderd()方法是对RecyclerView子项的数据进行赋值的
public ViewHolder onCreateViewHolder(ViewGroup parent,int viewType){
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.fruit_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=mFruitList.get(position);
Toast.makeText(view.getContext(),"you clicked view "+fruit.getName(),Toast.LENGTH_SHORT).show();
}
});
holder.fruitImage.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
int position=holder.getAdapterPosition();
Fruit fruit=mFruitList.get(position);
Toast.makeText(v.getContext(),"you clicked image "+fruit.getName(),Toast.LENGTH_SHORT).show();
}
});
return holder;
}
@Override
public void onBindViewHolder(ViewHolder holder,int position){
Fruit fruit=mFruitList.get(position);
holder.fruitImage.setImageResource(fruit.getImageId());
holder.fruitName.setText(fruit.getName());
}
@Override
public int getItemCount(){
return mFruitList.size();
}
}
最后编辑MainActivity
public class MainActivity extends AppCompatActivity {
private List<Fruit> fruitList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initFruits();
RecyclerView recyclerView=(RecyclerView)findViewById(R.id.recycler_view);
StaggeredGridLayoutManager layoutManager=new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
FruitAdapter adapter=new FruitAdapter(fruitList);
recyclerView.setAdapter(adapter);
}
private void initFruits(){
for(int i=0;i<2;i++){
Fruit apple=new Fruit(getRandomLengthName("Apple"),R.drawable.apple_pic);
fruitList.add(apple);
Fruit banana=new Fruit(getRandomLengthName("Banana"),R.drawable.banana_pic);
fruitList.add(banana);
Fruit orange=new Fruit(getRandomLengthName("Orange"),R.drawable.orange_pic);
fruitList.add(orange);
Fruit watermelon=new Fruit(getRandomLengthName("Watermelon"),R.drawable.watermelon_pic);
fruitList.add(watermelon);
Fruit pear=new Fruit(getRandomLengthName("Pear"),R.drawable.pear_pic);
fruitList.add(pear);
Fruit grape=new Fruit(getRandomLengthName("Grape"),R.drawable.grape_pic);
fruitList.add(grape);
Fruit pineapple=new Fruit(getRandomLengthName("Pineapple"),R.drawable.pineapple_pic);
fruitList.add(pineapple);
Fruit strawberry=new Fruit(getRandomLengthName("Strawberry"),R.drawable.strawberry_pic);
fruitList.add(strawberry);
Fruit cherry=new Fruit(getRandomLengthName("Cherry"),R.drawable.cherry_pic);
fruitList.add(cherry);
Fruit mango=new Fruit(getRandomLengthName("Mango"),R.drawable.mango_pic);
fruitList.add(mango);
}
}
private String getRandomLengthName(String name)
{
Random random=new Random();
int length=random.nextInt(20)+1;
StringBuilder builder=new StringBuilder();
for(int i=0;i<length;i++){
builder.append(name);
}
return builder.toString();
}
}