ios如何快速转型安卓开发-专题2

ios如何快速转型安卓开发-专题2,第1张

1.活动的生命周期 1.返回栈

安卓通过返回栈对活动进行管理,按下Back或者执行finish方法时,栈顶的活动销毁。

2.活动状态

(1)运行状态:栈顶。
(2)暂停状态:不处于栈顶,但仍然可见。
(3)停止状态:不处于栈顶,不可见。
(4)销毁状态:从返回栈移除

3.活动的生存期

(1)onCreate:活动第一次创建的时候调用
(2)onStart:活动由不可见变为可见
(3)onResume:活动准备好和用户交互时调用
(4)onPause:系统准备去启动或者恢复另一个活动的时候调用。通常会在这个方法中将一些消耗CPU的资源释放,保存一些关键数据,执行速度一定要快,否则会影响到栈顶活动的使用。
(5)onStop:活动完全不可见的时候调用。与onPause方法的主要区别在于,如果启动的新活动是一个对话框式的活动,onPause会执行,而onStop不会执行
(6)onDestory:活动销毁之前调用
(7)onRestart:活动由停止状态变成运行状态之前调用,也就是活动被重新启动了。
完整生存期:onCreate 到onDestroy之间。
可见生存期:onStart和onStop之间,处于可见。
前台生存期:onResume和onPause之间,处于运行形态。
Activity中提供了一个onSaveInstanceState()回调方法,保证活动在回收之前一定会被调,通过这个方法可以解决活动被回收时临时数据得不到保存的情况。
onSaveInstanceState()会提供一个Bundle类型的参数,提供了一系列的方法用于保存数据。
保存的数据可以在onCreate上的Bundle参数取对应的保存信息。Intent可以结合Bundle一起用于传输局,可以吧传递的数据保存在Bundle对象中,然后将Bundle对象放在Intent中,到了目标活动之后再取出对应的Bundle。

4.活动的启动模式

给activity标签指定android:launchMode属性来选择启动模式。
(1)standard:活动默认启动模式。启动后位于返回栈顶。
(2)singleTop:如果返回栈顶已经是该活动,直接使用它,不会创建新的活动,该活动没有处于栈顶还是会创建多个活动
(3)singleTask:返回栈是否存在该活动,存在直接使用,将所有位于该活动之上的出栈。
(4)singleInstance:启用一个新的返回栈来管理这个活动
实际用法:修改活动的继承方式,输出当前类名,可以快速定位当前类。创建活动管理器,通过List管理活动,通过finishAll,将所有Activity销毁。继承活动,将对应的启动函数hook掉,可以解决传数据的问题,而不用阅读对应的代码。

2.UI基本控件 1.TextView

android🆔控件标识符
match_parent/fill_parent:控件大小和父布局一样
wrap_parent:与当前的内容大小一致
gravity:文字对齐方式
textSize:文字大小
textColor:文字颜色

2.button

textAlllCaps:文字全大写
setOnClickListener:添加点击事件 实现接口/实现匿名类

3.EditText

hint:提示文本
maxLines:最大行数
findViewById:获取实例

4.ImageView

src:资源路径
setImageResource:设置图片

5.ProgressBar

progressBar.getVisibility() == View.GONE

android:max="100"
int progress = progressBar.getProgress();
progress = progress + 10;
progressBar.setProgress(progress);

6.AlertDialog
点击查看代码
AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
        dialog.setTitle("This is Dialog");
        dialog.setMessage("Something important.");
        dialog.setCancelable(false);
        dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which) {

          }
        });
        dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which) {

          }
        });
        dialog.show();
7.ProgressDialog
点击查看代码
ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
        progressDialog.setTitle("This is ProgressDialog");
        progressDialog.setMessage("Loading ...");
        progressDialog.setCancelable(true);
        progressDialog.show();
3.UI基本布局 1.线性布局

将所包含空间由上到下一次排列
layout_gravity属性,对齐方式
layout_weight,按比例方式指定控件大小,所有控件权重值相加,按比例值分配大小
orientation:设置线性布局方向

2.相对布局

layout_alignParentLeft左对齐...centerInParent中间,每个空间相对于父布局进行对齐。
layout_toRightOf:表示让一个控件位于另一个控件的右侧。

3.帧布局

所有空间都会摆放在布局的左上角

4.百分比布局

在dependencies闭包添加 compile ' 待确认

5.约束布局

可以按照比例约束调整空间位置和大小,更好适配机型

点击查看代码
android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    android:text="Back"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginLeft="0dp"
6.自定义布局

创建TitleLayout继承自LinearLayout,重写TitleLayout控件的构造函数,对于布局进行动态加载,需要借助LayoutInflater

4.ListView
点击查看代码
public class MainActivity extends AppCompatActivity {
  private String[] data = {"Apple", "Banana", "Couple", "Banana", "Couple", "Banana", "Couple", "Banana", "Couple", "Banana", "Couple", "Couple", "Banana", "Couple", "Banana", "Couple", "Banana", "Couple", "Couple", "Banana", "Couple", "Banana", "Couple", "Banana", "Couple"};
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ArrayAdapter adapter = new ArrayAdapter( MainActivity.this, android.R.layout.simple_list_item_1, data);
    ListView listView = (ListView) findViewById(R.id.list_view);
    listView.setAdapter(adapter);
  }
}
1.定制ListView的界面
点击查看代码
public class FruitAdapter extends ArrayAdapter {

  private  int resourceId;

  public FruitAdapter(@NonNull Context context, int resource,
      @NonNull List objects) {
    super(context, resource, objects);
    resourceId = resource;
  }

  public View getView(int position, View convertView, ViewGroup praent) {
    Fruit fruit = getItem(position);
    View view = LayoutInflater.from(getContext()).inflate(resourceId, praent, false);
    TextView textView1 = (TextView) view.findViewById(R.id.left_text);
    TextView textView2 = (TextView) view.findViewById(R.id.right_text);
    textView1.setText(fruit.getName());
    textView2.setText(fruit.getDetail());
    return view;
  }
}
	public class Fruit {
  private String name;
  private String detail;
  public Fruit (String name, String detail) {
    this.name = name;
    this.detail = detail;
  }

  public String getName() {
    return name;
  }

  public String getDetail() {
    return detail;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }
    Fruit fruit = (Fruit) o;
    return Objects.equals(name, fruit.name) &&
        Objects.equals(detail, fruit.detail);
  }

  @Override
  public int hashCode() {
    return Objects.hash(name, detail);
  }
}

private List 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.item_layout ,fruitList);
    ListView listView = (ListView) findViewById(R.id.list_view);
    listView.setAdapter(adapter);
  }
  private void initFruits () {
    for (int i=0; i < 2; i++) {
      Fruit apple = new Fruit("Apple", "Juice");
      fruitList.add(apple);
      apple = new Fruit("Apple1", "Juice");
      fruitList.add(apple);
      apple = new Fruit("Apple2", "Juice");
      fruitList.add(apple);
      apple = new Fruit("Apple3", "Juice");
      fruitList.add(apple);
      apple = new Fruit("Apple4", "Juice");
      fruitList.add(apple);
      apple = new Fruit("Apple5", "Juice");
      fruitList.add(apple);
      apple = new Fruit("Apple5", "Juice");
      fruitList.add(apple);
      apple = new Fruit("Apple5", "Juice");
      fruitList.add(apple);
      apple = new Fruit("Apple5", "Juice");
      fruitList.add(apple);
      apple = new Fruit("Apple5", "Juice");
      fruitList.add(apple);
      apple = new Fruit("Apple5", "Juice");
      fruitList.add(apple);
      apple = new Fruit("Apple5", "Juice");
      fruitList.add(apple);
      apple = new Fruit("Apple5", "Juice");
      fruitList.add(apple);
      apple = new Fruit("Apple5", "Juice");
      fruitList.add(apple);
      apple = new Fruit("Apple4", "Juice");
      fruitList.add(apple);
      apple = new Fruit("Apple5", "Juice");
      fruitList.add(apple);
      apple = new Fruit("Apple5", "Juice");
      fruitList.add(apple);
      apple = new Fruit("Apple5", "Juice");
      fruitList.add(apple);
      apple = new Fruit("Apple5", "Juice");
      fruitList.add(apple);
      apple = new Fruit("Apple5", "Juice");
      fruitList.add(apple);
      apple = new Fruit("Apple5", "Juice");
      fruitList.add(apple);
      apple = new Fruit("Apple5", "Juice");
      fruitList.add(apple);
      apple = new Fruit("Apple5", "Juice");
      fruitList.add(apple);
      apple = new Fruit("Apple5", "Juice");
      fruitList.add(apple);

    }

提升ListView的运行效率:类似于oc的复用机制,判断View是否创建,创建了直接使用,否则创建新的。

方式1:

点击查看代码
public View getView(int position, View convertView, ViewGroup praent) {
    Fruit fruit = getItem(position);
    View view;
    if (convertView == null) {
      view = LayoutInflater.from(getContext()).inflate(resourceId, praent, false);
    } else {
      view = convertView;
    }
}

方式二:

点击查看代码
public View getView(int position, View convertView, ViewGroup praent) {
    Fruit fruit = getItem(position);
    View view;
    ViewHolder viewHolder;
    if (convertView == null) {
      view = LayoutInflater.from(getContext()).inflate(resourceId, praent, false);
      viewHolder = new ViewHolder();
      viewHolder.left = (TextView) view.findViewById(R.id.left_text);
      viewHolder.right = (TextView) view.findViewById(R.id.right_text);
      view.setTag(viewHolder);
    } else {
      view = convertView;
      viewHolder = (ViewHolder)view.getTag();
    }
    viewHolder.left.setText(fruit.getName());
    viewHolder.left.setText(fruit.getName());
    //TextView textView1 = (TextView) view.findViewById(R.id.left_text);
    //TextView textView2 = (TextView) view.findViewById(R.id.right_text);
    //textView1.setText(fruit.getName());
    //textView2.setText(fruit.getDetail());
    return view;
  }

  class ViewHolder {
    TextView left;
    TextView right;
  }
2.设置点击事件
点击查看代码
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
      @Override
      public void onItemClick(AdapterView parent, View view, int position, long id) {
        Fruit fruit = fruitList.get(position);
        Toast.makeText(MainActivity.this,fruit.getName(),Toast.LENGTH_SHORT).show();
      }
    });
5.RecycleView
点击查看代码


  
  


	
	

  

  

  


	
	package com.example.listviewtest;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.app.Fragment;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
  private String[] data = {"Apple", "Banana", "Couple", "Banana", "Couple", "Banana", "Couple", "Banana", "Couple", "Banana", "Couple", "Couple", "Banana", "Couple", "Banana", "Couple", "Banana", "Couple", "Couple", "Banana", "Couple", "Banana", "Couple", "Banana", "Couple"};
  private List 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.item_layout ,fruitList);
    ListView listView = (ListView) findViewById(R.id.list_view);
    listView.setAdapter(adapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
      @Override
      public void onItemClick(AdapterView parent, View view, int position, long id) {
        Fruit fruit = fruitList.get(position);
        Toast.makeText(MainActivity.this,fruit.getName(),Toast.LENGTH_SHORT).show();
      }
    });*/
    RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycleview);
    LinearLayoutManager layoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(layoutManager);
    FruitsAdapter adapter = new FruitsAdapter(fruitList);
    recyclerView.setAdapter(adapter);

  }
  private void initFruits () {
    for (int i=0; i < 2; i++) {
      Fruit apple = new Fruit("Apple", "Juice");
      fruitList.add(apple);
      apple = new Fruit("Apple1", "Juice");
      fruitList.add(apple);
      apple = new Fruit("Apple2", "Juice");
      fruitList.add(apple);
      apple = new Fruit("Apple3", "Juice");
      fruitList.add(apple);
      apple = new Fruit("Apple4", "Juice");
      fruitList.add(apple);
      apple = new Fruit("Apple5", "Juice");
      fruitList.add(apple);
      apple = new Fruit("Apple5", "Juice");
      fruitList.add(apple);
      apple = new Fruit("Apple5", "Juice");
      fruitList.add(apple);
      apple = new Fruit("Apple5", "Juice");
      fruitList.add(apple);
      apple = new Fruit("Apple5", "Juice");
      fruitList.add(apple);
      apple = new Fruit("Apple5", "Juice");
      fruitList.add(apple);
      apple = new Fruit("Apple5", "Juice");
      fruitList.add(apple);
      apple = new Fruit("Apple5", "Juice");
      fruitList.add(apple);
      apple = new Fruit("Apple5", "Juice");
      fruitList.add(apple);
      apple = new Fruit("Apple4", "Juice");
      fruitList.add(apple);
      apple = new Fruit("Apple5", "Juice");
      fruitList.add(apple);
      apple = new Fruit("Apple5", "Juice");
      fruitList.add(apple);
      apple = new Fruit("Apple5", "Juice");
      fruitList.add(apple);
      apple = new Fruit("Apple5", "Juice");
      fruitList.add(apple);
      apple = new Fruit("Apple5", "Juice");
      fruitList.add(apple);
      apple = new Fruit("Apple5", "Juice");
      fruitList.add(apple);
      apple = new Fruit("Apple5", "Juice");
      fruitList.add(apple);
      apple = new Fruit("Apple5", "Juice");
      fruitList.add(apple);
      apple = new Fruit("Apple5", "Juice");
      fruitList.add(apple);

    }

  }
}

package com.example.listviewtest;

import java.util.List;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class FruitsAdapter extends RecyclerView.Adapter {
  private List mFruitList;

  public FruitsAdapter(List fruitList) {
    mFruitList = fruitList;
  }

  @NonNull
  @Override
  public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout,parent,false);
    ViewHolder holder = new ViewHolder(view);
    return holder;
  }

  @Override
  public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    Fruit fruit = mFruitList.get(position);
    holder.left.setText(fruit.getName());
    holder.right.setText(fruit.getDetail());
  }

  @Override
  public int getItemCount() {
    return mFruitList.size();
  }

  static class ViewHolder extends RecyclerView.ViewHolder {
    TextView left;
    TextView right;
    public ViewHolder(View view) {
      super(view);
      left = (TextView) view.findViewById(R.id.left_text);
      right = (TextView) view.findViewById(R.id.right_text);
    }
  }
}

设置水平滚动

点击查看代码
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycleview);
    LinearLayoutManager layoutManager = new LinearLayoutManager(this);
    layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
    recyclerView.setLayoutManager(layoutManager);
    FruitsAdapter adapter = new FruitsAdapter(fruitList);
    recyclerView.setAdapter(adapter);


  
  

瀑布流

点击查看代码
StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL);

  
  

设置点击事件

点击查看代码
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout,parent,false);
    ViewHolder holder = new ViewHolder(view);
    holder.left.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        int position = holder.getAdapterPosition();
        Toast.makeText(v.getContext(), "you clicked title"+position, Toast.LENGTH_SHORT).show();
      }
    });
    holder.right.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        int position = holder.getAdapterPosition();
        Toast.makeText(v.getContext(), "you clicked detail"+position, Toast.LENGTH_SHORT).show();
      }
    });
    return holder;
  }

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/996743.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存