Android APP 开发技术阶段总结

最近在做Android APP的开发,对用到的开发技术做一个总结。

大量结构化数据的展示:

可以使用RecyclerViewadaper实现。
结构化数据可以用一个类封装起来,作为构造构造adapter的数据来源。

public class RecyclerViewDepartmentAdapter  extends RecyclerView.Adapter {
    private Context context;
    private List data;

    public RecyclerViewDepartmentAdapter(Context context,List data){
        this.context = context;
        this.data = data;
    }

    @Override
    public RecyclerViewDepartmentAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        RecyclerViewDepartmentAdapter.ViewHolder holder = new RecyclerViewDepartmentAdapter.ViewHolder(LayoutInflater.from(
                context).inflate(R.layout.item_layout_department, parent,
                false));
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerViewDepartmentAdapter.ViewHolder holder, final int position) {

        final Context context = this.context;

        holder.id.setText(data.get(position).getId());
        holder.department.setText(data.get(position).getDepartmentName());

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("gwj","这里是点击每一行item的响应事件" + position + "item");
            }
        });
    }

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

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{

        private TextView id;
        private TextView department;

        public ViewHolder(View itemView) {
            super(itemView);
            id = itemView.findViewById(R.id.id);
            department = itemView.findViewById(R.id.department);
        }
    }
}

如上面代码所示,构造函数传入的数据是封装好的类的list。
可以对每一行的点击进行相应。

弹出式菜单

PopupMenu popup = new PopupMenu(context, v);//第二个参数是绑定的那个view
//获取菜单填充器
MenuInflater inflater = popup.getMenuInflater();
//填充菜单
inflater.inflate(R.menu.menu_department, popup.getMenu());
//绑定菜单项的点击事件
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
	@Override
	public boolean onMenuItemClick(MenuItem item) {
		return false
	}
});

菜单需要事先在xml中定义




    
    

简单对话框

//弹出对话框,消息框,确认是否删除
final AlertDialog.Builder normalDialog =
		new AlertDialog.Builder(context);
//normalDialog.setIcon(R.drawable.icon_dialog);
normalDialog.setTitle("是否确定删除该部门?");
normalDialog.setMessage("确认:删除,取消:不执行任何操作");
normalDialog.setPositiveButton("确定",
		new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				Log.e("ggg","删除了一个条目!");
			}
		});
normalDialog.setNegativeButton("取消",
		new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				Log.e("ggg","没有执行删除操作!");
			}
		});
// 显示
normalDialog.show();

自定义对话框

xml布局文件

 


    
        
        

            

            
        

        
        

            

            
        

        
        

            

            
        

        
        

            

            
        

        
        

            

            
        

        
        

            

            
        

        

            

对话框样式


类实现

public class PersonDialog  extends Dialog{
    //在构造方法里提前加载了样式
    private Context context;//上下文
    private Button button_confirm;
    private Button button_cancle;
    private Person person;

    public EditText editText_name;
    public EditText editText_phone;
    public EditText editText_email;
    public EditText editText_password;

    public Spinner spinner_permission;
    public Spinner spinner_department;

    public PersonDialog(Context context, Person person){
        super(context, R.style.MyDialog);//加载dialog的样式
        this.context = context;
        this.person = person;
    }

    public interface OnDialogButtonClickListener {
        void okButtonClick();
        void cancelButtonClick();
    }

    OnDialogButtonClickListener buttonClickListner;

    public void setOnButtonClickListener(OnDialogButtonClickListener listener) {
        this.buttonClickListner = listener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //提前设置Dialog的一些样式
        Window dialogWindow = getWindow();
        dialogWindow.setGravity(Gravity.CENTER);//设置dialog显示居中
        //dialogWindow.setWindowAnimations();设置动画效果
        setContentView(R.layout.dialog_person);


        WindowManager windowManager = ((Activity)context).getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = display.getWidth()*4/5;// 设置dialog宽度为屏幕的4/5
        getWindow().setAttributes(lp);
        setCanceledOnTouchOutside(true);//点击外部Dialog消失
        //遍历控件id添加点击注册

        button_confirm = findViewById(R.id.button_confirm);
        button_cancle = findViewById(R.id.button_cancle);
        button_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                buttonClickListner.okButtonClick();
            }
        });
        button_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                buttonClickListner.cancelButtonClick();
            }
        });

        //显示控件
        editText_name = findViewById(R.id.person_name_edit);
        editText_phone = findViewById(R.id.person_phone_edit);
        editText_email = findViewById(R.id.person_email_edit);
        editText_password = findViewById(R.id.person_password_edit);

        spinner_department = findViewById(R.id.person_department_spinner);
        spinner_permission = findViewById(R.id.person_permission_spinner);

    }
}

类使用

final PersonDialog personDialog1 = new PersonDialog(context,person1);
personDialog1.setOnButtonClickListener(new PersonDialog.OnDialogButtonClickListener() {
	@Override
	public void okButtonClick() {
		personDialog1.dismiss();
	}

	@Override
	public void cancelButtonClick() {
		personDialog1.dismiss();
	}
});
personDialog1.show();

这里使用Listener,在类的外部实现按钮的相应代码。

下拉列表

spinner_department = findViewById(R.id.person_department_spinner);
final DepartmentDao departmentDao = new DepartmentDao(context);
List ld = departmentDao.getDepartments();

List list1 = new ArrayList<>();
for(int i = 0;i adapter_department = new ArrayAdapter(context,R.layout.item_gwj_register_org, R.id.text,list1);
spinner_department.setAdapter(adapter_department);

把从数据库中查询到内容显示到下拉列表中

你可能感兴趣的:(随便写点,开发技术,安卓)