当前位置: 首页 > 工具软件 > Android Query > 使用案例 >

Android Query(AQuery)

郗鹏
2023-12-01
      做app开发也近7年了,今天在看一个第三方SDK时,才发现居然有AQuery这个框架,真是孤陋寡闻呀。
 上github去看了一下,发现这个框架的star和fork都很少,难道在项目中用到这个框架的不多吗?
 看了下介绍,发现还是蛮好用的,所以也把其Github的上说明代码copy点过来,下次有机会也用下这个框架。
      框架地址:https://github.com/androidquery/androidquery
Initializing AQuery :

// AQuery in Activity
public class MainActivity extends AppCompatActivity{

    private AQuery aq;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        aq = new AQuery(this);
    }
}

// AQuery in Fragment
public class MainFragment extends Fragment{

    private AQuery aq;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_home, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        aq = new AQuery(getActivity());
    }
}

// AQuery in ViewHolder
public class ViewHolder extends RecyclerView.ViewHolder{

    private AQuery aq;

    public ViewHolder(View itemView) {
        super(itemView);
        aq = new AQuery(itemView.getContext(), itemView);
    }
}
Query Activity :

// Fast intent
aq.open(HomeActivity.class);

// Custom intent
Intent intent = new Intent(this, HomeActivity.class);
intent.putExtra("id",1);
aq.open(intent);

// Intent transition from left and right
aq.openFromRight(HomeActivity.class);
aq.openFromLeft(HomeActivity.class);
aq.openFromRight(intent);
aq.openFromLeft(intent);

// Close activity transition
aq.closeToRight();
aq.closeToLeft();
Query View :

// OnClickListener
aq.id(R.id.login).click(v -> {
  // Do stuff
});

// Check is text not empty
boolean valid = aq.id(R.id.input_email).isValid();

// Get text value of view is avaliable
String email = aq.id(R.id.input_email).text();

// Set Visibilty Gone
aq.id(R.id.input_email).hide();

// Set Visibilty Visible
aq.id(R.id.input_email).show();

// Cast view
DefaultTextField defaultTextField = aq.id(R.id.input_email).as(DefaultTextField.class);

// Active state View
aq.id(R.id.input_email).active();
aq.id(R.id.input_email).inActive();

// If you want rounded image
aq.id(R.id.image).image(user.getAvatar_url()).rounded();

// If you want set from drawable
aq.id(R.id.image).image(R.drawable.profile);
Query Toas :

// Simple toas
aq.snack("Message");
aq.toast("Message");
Query Network :

// Ajax get
aq.ajax("https://api.github.com/users/ar-android")
        .get()
        .showLoading()
        .toObject(GithubUsers.class, (user, error) -> {
            // Do stuff
        });

// Ajax POST form
Map<String, String> params = new HashMap<>();
params.put("email", aq.id(R.id.email).text());
params.put("password", aq.id(R.id.password).text());
aq.ajax("https://ocit-tutorial.herokuapp.com/index.php")
        .postForm(params)
        .showLoading()
        .response((response, error) -> {
            if (response != null){
                aq.openFromRight(MainActivity.class);
            }
        });
Query Shared Preferences :

// Save string to shared preferences
aq.saveString("token", response.getData().getToken());

// Grab string from shared preferences
String token = aq.grabString("token");
Query SQLite :

//Storing data to SQLite
Map<String, Object> data = new HashMap<>();
data.put("nama", "Putri Nuraini");
data.put("email", "alahmadrosid@gmail.com");
aq.sql().table("user").insert(data);

// Get All row from table
List<Map<String, String>> user = aq.sql().table("user").all();

// Update row from table by id
boolean update = aq.sql().table("user").update(3, data);

// Get row table by id
Map<String, String> user = aq.sql().table("user").get(3);

// Get table by first row
Map<String, String> user = aq.sql().table("user").first();

// Get table by last row
Map<String, String> user = aq.sql().table("user").last();

// Delete table by id
boolean delete = aq.sql().table("user").delete(2);

// Clear database sqlite
aq.sql().clearDb();

To use this library Add in you top build.gradle :


allprojects {
    repositories {
      maven { url 'https://jitpack.io' }
    }
  }

Setup java 8 compiler for enable lambada expression in you app build.gradle :


compileOptions {
  sourceCompatibility 1.8
  targetCompatibility 1.8
}

Add the dependency in you app build.gradle :

implementation 'com.github.ar-android:AQuery:1.0.3'

相关阅读

相关文章

相关问答