Back to project page GreenGear.
The source code is released under:
MIT License
If you think the Android project GreenGear listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.bradleycurran.viewquery; //from w ww.j a v a2 s.co m import android.annotation.SuppressLint; import android.app.Activity; import android.os.Build; import android.util.SparseArray; import android.view.View; import android.view.View.OnClickListener; import android.widget.Adapter; import android.widget.AdapterView; import android.widget.ImageView; import android.widget.TextView; import com.bradleycurran.viewquery.Operation.AdapterOperation; import com.bradleycurran.viewquery.Operation.ImageOperation; import com.bradleycurran.viewquery.Operation.TextOperation; import com.bradleycurran.viewquery.Operation.ViewOperation; import com.bradleycurran.viewquery.animation.hc.BounceTouchListener; public class ViewQuery { private static final int INITIAL_CACHE_SIZE = 8; private View mView; private SparseArray<ViewQuery> mCachedQueries; public ViewQuery(Activity activity) { this(activity.findViewById(android.R.id.content)); } public ViewQuery(View view) { mView = view; mCachedQueries = new SparseArray<ViewQuery>(INITIAL_CACHE_SIZE); } public View get() { return mView; } public <T extends View> T get(Class<T> cls) { return Operation.get(mView, cls); } public ViewQuery find(int id) { ViewQuery query = mCachedQueries.get(id); if (query == null) { if (mView == null) { query = new ViewQuery(mView); } else { query = new ViewQuery(mView.findViewById(id)); } mCachedQueries.append(id, query); } return query; } public ViewQuery onClick(final OnClickListener listener) { return run(new ViewOperation() { @Override void execute(View view) { view.setOnClickListener(listener); } }); } public ViewQuery gone(final boolean gone) { return run(new ViewOperation() { @Override void execute(View view) { view.setVisibility(gone ? View.GONE : View.VISIBLE); } }); } public ViewQuery invisible(final boolean invisible) { return run(new ViewOperation() { @Override void execute(View view) { view.setVisibility(invisible ? View.INVISIBLE : View.VISIBLE); } }); } public ViewQuery image(final int resId) { return run(new ImageOperation() { @Override void execute(ImageView view) { view.setImageResource(resId); } }); } public ViewQuery text(final int resId) { return run(new TextOperation() { @Override void execute(TextView view) { view.setText(resId); } }); } public ViewQuery text(final CharSequence str) { return run(new TextOperation() { @Override void execute(TextView view) { view.setText(str); } }); } public ViewQuery adapter(final Adapter adapter) { return run(new AdapterOperation() { @SuppressWarnings({ "unchecked", "rawtypes" }) @Override void execute(AdapterView view) { view.setAdapter(adapter); } }); } @SuppressLint("NewApi") public ViewQuery bounce() { return run(new ViewOperation() { @Override void execute(View view) { if (supportsHoneycomb()) { view.animate().setDuration(250); view.setOnTouchListener(new BounceTouchListener()); } } }); } private <T extends View> ViewQuery run(Operation<T> o) { o.run(mView); return this; } private static boolean supportsHoneycomb() { return supportsVersion(Build.VERSION_CODES.HONEYCOMB); } private static boolean supportsVersion(int version) { return Build.VERSION.SDK_INT >= version; } }