2019独角兽企业重金招聘Python工程师标准>>>
先来张效果图吧~
先说下思路吧: 这是个商品详情页,然后商品页面里面使用layoutInflater获取出要弹出框框的view,当然了,这里面参数的加载数据也就写在这个popwindow里面啦。
开始贴代码了
商品弹出框布局:(下面的购物车和购买偷懒直接设定了宽度)
activity_product_attribute.xml
然后在商品详情页,也就是第一张图,点击购买或加入购物车的时候,调用弹出框的方法,这里我起名叫showProductAttribute(); 来贴上
private EShopOpenHelper eshop;
private ColorAdapter cAdapter;
private SizeAdapter sAdapter;
private List colorsList = new ArrayList();
private List sizeList = new ArrayList();
private HorizontialListView horCorList;
private HorizontialListView horSizeList;
private PopupWindow window;
private String sizeName = "";
private String colorName = "";
//这两列的请求数据库填充数据我放在oncreate()方法里面了,为了写帖子方便就单独拿出来了。
colorsList = eshop.selColorByProId(info.getId());
sizeList = eshop.selSizeByProId(info.getId());
protected void showProductAttribute() {
// 利用layoutInflater获得View
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.activity_product_attribute, null);
horCorList = (HorizontialListView) view.findViewById(R.id.ColorListView); // 横向的ListView
horSizeList = (HorizontialListView) view.findViewById(R.id.SizeListView);
TextView gouwuche = (TextView) view.findViewById(R.id.pro_gouwuche); // 加入购物车
TextView tobuy = (TextView) view.findViewById(R.id.pro_tobuy); // 立即购买
// 下面是两种方法得到宽度和高度 getWindow().getDecorView().getWidth()
window = new PopupWindow(view, WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.WRAP_CONTENT);
// 设置popWindow弹出窗体可点击,这句话必须添加,并且是true
window.setFocusable(true);
// 必须要给调用这个方法,否则点击popWindow以外部分,popWindow不会消失
// window.setBackgroundDrawable(new BitmapDrawable());
// 实例化一个ColorDrawable颜色为半透明
ColorDrawable dw = new ColorDrawable(0xb0000000);
window.setBackgroundDrawable(dw);
// 在参照的View控件下方显示
// window.showAsDropDown(MainActivity.this.findViewById(R.id.start));
// 设置popWindow的显示和消失动画
window.setAnimationStyle(R.style.mypopwindow_anim_style);
// 在底部显示
window.showAtLocation(GoodsDetailActivity.this.findViewById(R.id.Product_attribute_more), Gravity.BOTTOM, 0,0);
cAdapter = new ColorAdapter(colorsList, getApplicationContext());
sAdapter = new SizeAdapter(sizeList, getApplicationContext());
horCorList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, View view,
int position, long id) {
mColor cor = (mColor) parent.getAdapter().getItem(position);//颜色的实体类
colorName = cor.getColorName();
cAdapter.setSelectedPosition(position);
cAdapter.notifyDataSetChanged();
// ToastUtils.ToastMessage(getApplicationContext(), "" +
// cor.getColorName());
}
});
horSizeList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, View view,
int position, long id) {
Size size = (Size) parent.getAdapter().getItem(position);//尺寸的实体类
sizeName = size.getSizeName();
sAdapter.setSelectedPosition(position);
sAdapter.notifyDataSetChanged();
// ToastUtils.ToastMessage(getApplicationContext(), "" +
// size.getSizeName());
}
});
horCorList.setAdapter(cAdapter);
horSizeList.setAdapter(sAdapter);
// popWindow消失监听方法
window.setOnDismissListener(new OnDismissListener() {
@Override
public void onDismiss() {
Toast.makeText(getApplicationContext(), "popWindow消失", 0);
System.out.println("popWindow消失");
}
});
}
/** 消除弹窗 */
public void dissmiss() {
window.dismiss();
}
这里还有里面的HorizontialListView类,这个是横向的ListView
HorizontialListView.java
/*
* HorizontalListView.java v1.5
*
*
* The MIT License
* Copyright (c) 2011 Paul Soucy ([email protected])
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
package com.shop.eshopservice.xListView;
import java.util.LinkedList;
import java.util.Queue;
import android.content.Context;
import android.database.DataSetObserver;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.Scroller;
public class HorizontialListView extends AdapterView {
public boolean mAlwaysOverrideTouch = true;
protected ListAdapter mAdapter;
private int mLeftViewIndex = -1;
private int mRightViewIndex = 0;
protected int mCurrentX;
protected int mNextX;
private int mMaxX = Integer.MAX_VALUE;
private int mDisplayOffset = 0;
protected Scroller mScroller;
private GestureDetector mGesture;
private Queue mRemovedViewQueue = new LinkedList();
private OnItemSelectedListener mOnItemSelected;
private OnItemClickListener mOnItemClicked;
private OnItemLongClickListener mOnItemLongClicked;
private boolean mDataChanged = false;
public HorizontialListView(Context context, AttributeSet attrs) {
super(context, attrs);
initView();
}
private synchronized void initView() {
mLeftViewIndex = -1;
mRightViewIndex = 0;
mDisplayOffset = 0;
mCurrentX = 0;
mNextX = 0;
mMaxX = Integer.MAX_VALUE;
mScroller = new Scroller(getContext());
mGesture = new GestureDetector(getContext(), mOnGesture);
}
@Override
public void setOnItemSelectedListener(AdapterView.OnItemSelectedListener listener) {
mOnItemSelected = listener;
}
@Override
public void setOnItemLongClickListener(AdapterView.OnItemLongClickListener listener) {
mOnItemLongClicked = listener;
}
@Override
public void setOnItemClickListener(AdapterView.OnItemClickListener listener){
mOnItemClicked = listener;
}
private DataSetObserver mDataObserver = new DataSetObserver() {
@Override
public void onChanged() {
synchronized(HorizontialListView.this){
mDataChanged = true;
}
invalidate();
requestLayout();
}
@Override
public void onInvalidated() {
reset();
invalidate();
requestLayout();
}
};
@Override
public ListAdapter getAdapter() {
return mAdapter;
}
@Override
public View getSelectedView() {
//TODO: implement
return null;
}
@Override
public void setAdapter(ListAdapter adapter) {
if(mAdapter != null) {
mAdapter.unregisterDataSetObserver(mDataObserver);
}
mAdapter = adapter;
mAdapter.registerDataSetObserver(mDataObserver);
reset();
}
private synchronized void reset(){
initView();
removeAllViewsInLayout();
requestLayout();
}
@Override
public void setSelection(int position) {
//TODO: implement
}
private void addAndMeasureChild(final View child, int viewPos) {
LayoutParams params = child.getLayoutParams();
if(params == null) {
params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
}
addViewInLayout(child, viewPos, params, true);
child.measure(MeasureSpec.makeMeasureSpec(getWidth(), MeasureSpec.AT_MOST),
MeasureSpec.makeMeasureSpec(getHeight(), MeasureSpec.AT_MOST));
}
@Override
protected synchronized void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
if(mAdapter == null){
return;
}
if(mDataChanged){
int oldCurrentX = mCurrentX;
initView();
removeAllViewsInLayout();
mNextX = oldCurrentX;
mDataChanged = false;
}
if(mScroller.computeScrollOffset()){
int scrollx = mScroller.getCurrX();
mNextX = scrollx;
}
if(mNextX < 0){
mNextX = 0;
mScroller.forceFinished(true);
}
if(mNextX > mMaxX) {
mNextX = mMaxX;
mScroller.forceFinished(true);
}
int dx = mCurrentX - mNextX;
removeNonVisibleItems(dx);
fillList(dx);
positionItems(dx);
mCurrentX = mNextX;
if(!mScroller.isFinished()){
post(new Runnable(){
@Override
public void run() {
requestLayout();
}
});
}
}
private void fillList(final int dx) {
int edge = 0;
View child = getChildAt(getChildCount()-1);
if(child != null) {
edge = child.getRight();
}
fillListRight(edge, dx);
edge = 0;
child = getChildAt(0);
if(child != null) {
edge = child.getLeft();
}
fillListLeft(edge, dx);
}
private void fillListRight(int rightEdge, final int dx) {
while(rightEdge + dx < getWidth() && mRightViewIndex < mAdapter.getCount()) {
View child = mAdapter.getView(mRightViewIndex, mRemovedViewQueue.poll(), this);
addAndMeasureChild(child, -1);
rightEdge += child.getMeasuredWidth();
if(mRightViewIndex == mAdapter.getCount()-1){
mMaxX = mCurrentX + rightEdge - getWidth();
}
mRightViewIndex++;
}
}
private void fillListLeft(int leftEdge, final int dx) {
while(leftEdge + dx > 0 && mLeftViewIndex >= 0) {
View child = mAdapter.getView(mLeftViewIndex, mRemovedViewQueue.poll(), this);
addAndMeasureChild(child, 0);
leftEdge -= child.getMeasuredWidth();
mLeftViewIndex--;
mDisplayOffset -= child.getMeasuredWidth();
}
}
private void removeNonVisibleItems(final int dx) {
View child = getChildAt(0);
while(child != null && child.getRight() + dx <= 0) {
mDisplayOffset += child.getMeasuredWidth();
mRemovedViewQueue.offer(child);
removeViewInLayout(child);
mLeftViewIndex++;
child = getChildAt(0);
}
child = getChildAt(getChildCount()-1);
while(child != null && child.getLeft() + dx >= getWidth()) {
mRemovedViewQueue.offer(child);
removeViewInLayout(child);
mRightViewIndex--;
child = getChildAt(getChildCount()-1);
}
}
private void positionItems(final int dx) {
if(getChildCount() > 0){
mDisplayOffset += dx;
int left = mDisplayOffset;
for(int i=0;i
相信大家也看到了填充颜色和尺寸数据的适配器了ColorAdapter和SizeAdapter,这两个适配器getView里面共用了一个布局item_attribute.xml
代码如下:
ColorAdapter.java文件
/**
* 商品属性的适配器
*
*/
public class ColorAdapter extends BaseAdapter {
private List colorList;
private Context ctx;
private int selectedPosition = -1;// 选中的位置
public ColorAdapter(List attr, Context ctx) {
this.colorList = attr;
this.ctx = ctx;
}
public void setList(List proList){
this.colorList = proList;
}
@Override
public int getCount() {
return colorList.size();
}
@Override
public Object getItem(int arg0) {
return colorList.get(arg0);
}
@Override
public long getItemId(int arg0) {
return arg0;
}
public void setSelectedPosition(int position){
selectedPosition = position;
}
@Override
public View getView(final int position, View arg1, ViewGroup arg2) {
final Holder hold;
if (arg1 == null) {
hold = new Holder();
arg1 = View.inflate(ctx, R.layout.item_attribute, null);
hold.mTitle = (TextView) arg1.findViewById(R.id.ProductAttrTextView); //产品标题
hold.mTitle.setClickable(true);
arg1.setTag(hold);
} else {
hold = (Holder) arg1.getTag();
}
hold.mTitle.setText(ToDBC(colorList.get(position).getColorName()));
if(selectedPosition == position){
hold.mTitle.setBackgroundColor(Color.rgb(255, 130, 71));
hold.mTitle.setTextColor(Color.rgb(255, 255, 255));
}else{
hold.mTitle.setBackgroundResource(R.drawable.textview_border);
hold.mTitle.setTextColor(Color.rgb(0, 0, 0));
}
return arg1;
}
/**
* TextView自动换行
* @param input
* @return
*/
public static String ToDBC(String input) {
char[] c = input.toCharArray();
for (int i = 0; i< c.length; i++) {
if (c[i] == 12288) {
c[i] = (char) 32;
continue;
}if (c[i]> 65280&& c[i]< 65375)
c[i] = (char) (c[i] - 65248);
}
return new String(c);
}
static class Holder {
TextView mTitle;
}
}
然后SizeAdapter.java文件
/**
* 商品尺寸的适配器
*
*/
public class SizeAdapter extends BaseAdapter {
private List sizeList;
private Context ctx;
private int selectedPosition = -1;// 选中的位置
public SizeAdapter(List attr, Context ctx) {
this.sizeList = attr;
this.ctx = ctx;
}
public void setList(List proList){
this.sizeList = proList;
}
@Override
public int getCount() {
return sizeList.size();
}
@Override
public Object getItem(int arg0) {
return sizeList.get(arg0);
}
@Override
public long getItemId(int arg0) {
return arg0;
}
public void setSelectedPosition(int position){
selectedPosition = position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final Holder hold;
if (convertView == null) {
hold = new Holder();
convertView = View.inflate(ctx, R.layout.item_attribute, null);
hold.mTitle = (TextView) convertView.findViewById(R.id.ProductAttrTextView); //产品标题
hold.mTitle.setClickable(true);
convertView.setTag(hold);
} else {
hold = (Holder) convertView.getTag();
}
hold.mTitle.setText(ToDBC(sizeList.get(position).getSizeName()));
if(selectedPosition == position){
hold.mTitle.setBackgroundColor(Color.rgb(255, 130, 71));
hold.mTitle.setTextColor(Color.rgb(255, 255, 255));
}else{
hold.mTitle.setBackgroundResource(R.drawable.textview_border);
hold.mTitle.setTextColor(Color.rgb(0, 0, 0));
}
return convertView;
}
/**
* TextView自动换行
* @param input
* @return
*/
public static String ToDBC(String input) {
char[] c = input.toCharArray();
for (int i = 0; i< c.length; i++) {
if (c[i] == 12288) {
c[i] = (char) 32;
continue;
}if (c[i]> 65280&& c[i]< 65375)
c[i] = (char) (c[i] - 65248);
}
return new String(c);
}
static class Holder {
TextView mTitle;
}
}
好了,这整一个流程就下来了。。。
代码虽有些啰嗦,但功能也算实现了。。