編輯:關於Android編程
先看效果圖:
控件類:
import android.content.Context; import android.util.AttributeSet; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class LocationSpinner extends Button implements OnClickListener { public LocationSpinner(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); // TODO Auto-generated constructor stub setOnClickListener(this); } public LocationSpinner(Context context, AttributeSet attrs) { this(context, attrs,0); // TODO Auto-generated constructor stub } public LocationSpinner(Context context) { this(context,null); // TODO Auto-generated constructor stub } @Override public void onClick(View v) { // TODO Auto-generated method stub if(listener!=null){ listener.onShow(); } } private OnSpinnerListener listener; public OnSpinnerListener getListener() { return listener; } public void setListener(OnSpinnerListener listener) { this.listener = listener; } public interface OnSpinnerListener{ void onShow(); void onCheckChange(LocationBean bean); } }
import java.util.List; import android.content.Context; import android.view.View; import com.lanyan.adapter.ListAdapter; import com.lanyan.adapter.ViewHolder; import com.lanyan.floatdialot.R; import com.lanyan.interfaces.OnListViewListener; public class LocationSpinnerAdapter extends ListAdapterPopupWindow:{ public LocationSpinnerAdapter(Context context, List list) { super(context, list); // TODO Auto-generated constructor stub } @Override public int setContentView() { // TODO Auto-generated method stub return R.layout.item; } @Override public void initial(View view, LocationBean arg1, OnListViewListener arg2) { // TODO Auto-generated method stub ViewHolder.setTextView(view, R.id.head, arg1.getName()); } }
import android.app.Activity; import android.content.Context; import android.graphics.drawable.ColorDrawable; import android.util.DisplayMetrics; import android.view.View; import android.view.WindowManager; import android.widget.PopupWindow; public class SpinnerPopupWindow { private int mScreenWidth; private int mScreenHeight; public SpinnerPopupWindow(){ } public static SpinnerPopupWindow getInstance(){ return new SpinnerPopupWindow(); } public PopupWindow onCreateSpinnerPopupWindow(Activity mActivity,View mView){ initScreen(mActivity); PopupWindow mPopupWindow=new PopupWindow(mView,mScreenWidth/3*2,mScreenHeight/3*2); mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog); mPopupWindow.setBackgroundDrawable(new ColorDrawable(0x000000)); mPopupWindow.setOutsideTouchable(false); return mPopupWindow; } public void initScreen(Activity mActivity){ WindowManager manager=(WindowManager)mActivity.getSystemService(Context.WINDOW_SERVICE); DisplayMetrics outMetrics=new DisplayMetrics(); manager.getDefaultDisplay().getMetrics(outMetrics); mScreenWidth=outMetrics.widthPixels; mScreenHeight=outMetrics.heightPixels; } }PopupWindow的寬高可根據自己的需求定義,ousideTouch事件 手動修改即可,最好是自定義style popouWindow公開方法。這裡的傳入View使用的個人比較喜歡的一種方式:
import java.util.List; import android.annotation.SuppressLint; import android.app.Activity; import android.view.LayoutInflater; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ListView; import com.lanyan.floatdialot.R; import com.lanyan.widget.LocationSpinner.OnSpinnerListener; import com.lidroid.xutils.ViewUtils; import com.lidroid.xutils.view.annotation.ViewInject; @SuppressLint(InflateParams) public class SpinnerPopupView { @ViewInject(R.id.spinner_listview) private ListView mListView; private int mPosition=-1; private View mView; private LocationSpinnerAdapter mAdapter; public SpinnerPopupView(){ } public static SpinnerPopupView getInstance(){ return new SpinnerPopupView(); } public View getCurrentView(Activity mActivity,ListmList,final OnSpinnerListener mListener){ mView=LayoutInflater.from(mActivity).inflate(R.layout.spinner, null); ViewUtils.inject(this,mView); mAdapter=new LocationSpinnerAdapter(mActivity, mList); mListView.setAdapter(mAdapter); mListView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView parent, View view, int position, long id) { // TODO Auto-generated method stub if(mPosition!=position){ mPosition=position; mListener.onCheckChange(mAdapter.getItem(position)); }else{ mListener.onCheckChange(null); } } }); return mView; } }
import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.WindowManager; import android.widget.PopupWindow; import android.widget.Toast; import com.lanyan.widget.LocationBean; import com.lanyan.widget.SpinnerPopupView; import com.lanyan.widget.SpinnerPopupWindow; import com.lanyan.widget.LocationSpinner.OnSpinnerListener; import com.lidroid.xutils.ViewUtils; public abstract class BaseActivity extends Activity implements OnSpinnerListener{ protected PopupWindow mPopupWindow=null; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(getContentView()); ViewUtils.inject(this); initView(); } public abstract int getContentView(); protected void initView() { // TODO Auto-generated method stub } @Override public void onShow() { // TODO Auto-generated method stub if(mPopupWindow!=null&&mPopupWindow.isShowing()){ return; }else if(mPopupWindow==null){ mPopupWindow=SpinnerPopupWindow.getInstance().onCreateSpinnerPopupWindow(this, getPopupWindowView()); } if(mPopupWindow!=null&&!mPopupWindow.isShowing()){ mPopupWindow.showAsDropDown(getDropDownView()); WindowManager.LayoutParams lp=getWindow().getAttributes(); lp.alpha = 0.4f; getWindow().setAttributes(lp); } } protected View getDropDownView() { // TODO Auto-generated method stub return null; } protected View getPopupWindowView() { // TODO Auto-generated method stub return SpinnerPopupView.getInstance().getCurrentView(this, LocationBean.getData(), this); } @Override public void onCheckChange(LocationBean bean) { // TODO Auto-generated method stub if(mPopupWindow!=null&&mPopupWindow.isShowing()){ mPopupWindow.dismiss(); WindowManager.LayoutParams lp=getWindow().getAttributes(); lp.alpha = 1f; getWindow().setAttributes(lp); } } protected void toastShow(String message){ Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show(); } }
import android.view.View; import com.lanyan.widget.LocationBean; import com.lanyan.widget.LocationSpinner; import com.lidroid.xutils.view.annotation.ViewInject; public class MainActivity extends BaseActivity{ @ViewInject(R.id.botton) private LocationSpinner mSpinner; @Override public int getContentView() { // TODO Auto-generated method stub return R.layout.activity_main; } @Override protected void initView() { // TODO Auto-generated method stub super.initView(); mSpinner.setListener(this); } @Override protected View getDropDownView() { // TODO Auto-generated method stub return mSpinner; } @Override public void onCheckChange(LocationBean bean) { // TODO Auto-generated method stub super.onCheckChange(bean); if(bean!=null){ mSpinner.setText(bean.getName()); toastShow(Name: +bean.getName()+ ID: +bean.getId()); } } }
備注:這個demo用了第三方框架Xutil的注解,還有自己整合的一個ListView適配器的ListAdapter+ViewHolder用法,若需要源碼聯系我即可。
支付接入流程官方文檔https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=8_5項目的應用(AppRegi
微信對話列表滑動刪除效果很不錯的,借鑒了github上SwipeListView(項目地址:https://github.com/likebamboo/SwipeList
1.利用SimpleAdapter適配器實現。 這裡以每一個網格中添加一張圖片和相應的文字說明為例: main.xml 因為除了添加圖片
前言:好幾天電腦打不開CSDN博客,也不知道怎麼回事,今天下班回來突然能打開了,遂將周末實現的一個效果貼上。實現功能:獲取手機應用圖標,名稱,時間(安裝時間/更新時間),