仿天猫 购物车(Android studio 仿天猫 详情页面 添加购物车选择 颜色 尺寸demo)

版权声明:本文为博主原创文章,未经博主允许不得转载。 转载需附加原创出版地址 可加入QQ群:586620031讨论技术 https://blog.csdn.net/yangdep/article/details/62884393

这是我第一次编写博客,有不好的地方请发邮件或留言告知.
注*请勿转载—转载需博主同意
1.首先就是设计页面了–先效果图
这里写图片描述
2.现在开始看代码,没有啥好说的
这是布局文件 MainActivity 里面的图片呀颜色自己设置

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!---头布局部分-->
    <RelativeLayout
        android:id="@+id/rl_head_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/tv_head"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:background="#e9e9e9"></TextView>

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:src="@mipmap/back" />
    </RelativeLayout>
    <!--主体展示部分-->
    <ScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/rl_head_layout">

        <RelativeLayout
            android:id="@+id/rl_scrollview_son"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <!---商品图片展示部分-->
            <RelativeLayout
                android:id="@+id/rl_carousel"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <android.support.v4.view.ViewPager
                    android:id="@+id/viewPager"
                    android:layout_width="match_parent"
                    android:layout_height="250dp" />
                <!-- <TextView
                    android:id="@+id/tv_img_number"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/viewPager"
                    android:layout_centerHorizontal="true"
                    android:textSize="22dp"
                    android:text="1/6"
                    android:background="#55000000"
                    android:padding="5dp"
                    android:layout_marginTop="-50dp"
                    android:gravity="center"
                    />-->
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="30dp"
                    android:layout_below="@+id/viewPager"
                    android:layout_marginTop="-30dp"
                    android:background="#55000000"
                    android:gravity="center"
                    android:orientation="horizontal">

                    <ImageView
                        android:id="@+id/iv_first"
                        android:layout_width="30dp"
                        android:layout_height="30dp"
                        android:paddingLeft="15dp"
                        android:src="@mipmap/point_pressed" />

                    <ImageView
                        android:id="@+id/iv_two"
                        android:layout_width="30dp"
                        android:layout_height="30dp"
                        android:paddingLeft="15dp"
                        android:src="@mipmap/point_unpressed" />

                    <ImageView
                        android:id="@+id/iv_three"
                        android:layout_width="30dp"
                        android:layout_height="30dp"
                        android:paddingLeft="15dp"
                        android:src="@mipmap/point_unpressed" />

                    <ImageView
                        android:id="@+id/iv_four"
                        android:layout_width="30dp"
                        android:layout_height="30dp"
                        android:paddingLeft="15dp"
                        android:src="@mipmap/point_unpressed" />
                </LinearLayout>
            </RelativeLayout>

            <!--商品参数部分-->
            <!--图文详情-->
            <RelativeLayout
                android:id="@+id/rl_graphic_details"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/rl_shopping_details"
                android:background="#005555">

                <TextView
                    android:id="@+id/tv_graphic_details"
                    android:layout_width="match_parent"
                    android:layout_height="45dp"
                    android:layout_marginLeft="10dp"
                    android:gravity="center_vertical"
                    android:text="图文详情"
                    android:textColor="#000000"
                    android:textSize="18dp" />

                <GridView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_below="@+id/tv_graphic_details"
                    android:numColumns="2" />
            </RelativeLayout>

            <RelativeLayout
                android:id="@+id/rl_shopping_details"
                android:layout_width="match_parent"
                android:layout_height="150dp"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_below="@+id/rl_carousel">

                <TextView
                    android:id="@+id/tv_shopping_name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="10dp"
                    android:paddingLeft="10dp"
                    android:text="蝴蝶结手钩棉线玻璃花瓶矮款"
                    android:textColor="#444444"
                    android:textSize="22dp" />

                <TextView
                    android:id="@+id/tv_shopping_no"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/tv_shopping_name"
                    android:paddingLeft="20dp"
                    android:text="货号:3713163#8094090083" />

                <TextView
                    android:id="@+id/tv_shopping_price"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/tv_shopping_no"
                    android:layout_marginTop="10dp"
                    android:paddingLeft="20dp"
                    android:text="市场价: 11.50元 /个            平台会员价: 10.90元 /个" />
            </RelativeLayout>
        </RelativeLayout>
    </ScrollView>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:layout_alignParentBottom="true"
        android:background="#eeeeee">

        <Button
            android:id="@+id/btn_buy"
            android:layout_width="120dp"
            android:layout_height="48dp"
            android:layout_alignParentRight="true"
            android:background="#ff2255"
            android:text="立即购买" />

        <Button
            android:id="@+id/btn_shopping_cart"
            android:layout_width="120dp"
            android:layout_height="48dp"
            android:layout_toLeftOf="@+id/btn_buy"
            android:background="#ff5500"
            android:text="加入购物车" />
    </RelativeLayout>


    <!--           添加购物车选择颜色属性部分                   -->

    <LinearLayout
        android:id="@+id/ll_add_shopping_cart"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#55000000"
        android:orientation="vertical">
        <!--空白背景部分-->
        <LinearLayout
            android:id="@+id/ll_transparent"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal"></LinearLayout>
        <!--选择属性部分-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="5"
            android:orientation="vertical">

            <!--图片价格部分-->
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="3">

                <TextView
                    android:layout_width="116dp"
                    android:layout_height="126dp"
                    android:layout_marginLeft="12dp"
                    android:background="#000000" />

                <ImageView
                    android:layout_width="110dp"
                    android:layout_height="120dp"
                    android:layout_marginLeft="15dp"
                    android:layout_marginTop="3dp"
                    android:background="#ffffff"
                    android:src="@mipmap/ic_launcher" />

                <ImageView
                    android:id="@+id/iv_hide_shopping_cart"
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:layout_alignParentRight="true"
                    android:layout_marginRight="20dp"
                    android:layout_marginTop="20dp"
                    android:src="@mipmap/point_pressed" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_alignBottom="@+id/textView3"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"
                    android:background="#ffffff" />


            </RelativeLayout>
            <!--颜色尺寸购买数量部分-->
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="7"
                android:background="#ffffff"
                android:orientation="vertical">
                <!--颜色分类-->
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="0.98"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:paddingLeft="15dp"
                        android:paddingTop="10dp"
                        android:text="颜色分类"
                        android:textColor="#000000"
                        android:textSize="18dp" />

                    <GridView
                        android:id="@+id/gv_attribute_color"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:numColumns="3"
                        android:paddingTop="20dp" />

                </LinearLayout>
                <!--尺寸大小-->
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:paddingLeft="15dp"
                        android:paddingTop="10dp"
                        android:text="尺码"
                        android:textColor="#000000"
                        android:textSize="18dp" />

                    <GridView
                        android:id="@+id/gv_attribute_size"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:numColumns="3"
                        android:paddingTop="20dp" />

                </LinearLayout>
                <!--购买数量部分-->
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="2"
                    android:clickable="true"
                    android:orientation="vertical">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="0dp"
                        android:layout_weight="2"
                        android:gravity="right|center_vertical"
                        android:orientation="horizontal"
                        android:paddingRight="20dp">

                        <TextView
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:layout_weight="1"
                            android:paddingLeft="15dp"
                            android:text="购买数量"
                            android:textColor="#000000"
                            android:textSize="18dp"

                            />

                        <Button
                            android:layout_width="60dp"
                            android:layout_height="wrap_content"
                            android:gravity="center"
                            android:text="+"
                            android:textSize="22dp" />

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:padding="20dp"
                            android:text="1" />

                        <Button
                            android:layout_width="60dp"
                            android:layout_height="wrap_content"
                            android:gravity="center"
                            android:text="-"
                            android:textSize="22dp" />
                    </LinearLayout>

                    <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="0dp"
                        android:layout_weight="3">

                        <Button
                            android:id="@+id/btn_shopping_cart_rbAttribute_submit"
                            android:layout_width="match_parent"
                            android:layout_height="48dp"
                            android:layout_alignParentBottom="true"
                            android:layout_alignParentLeft="true"
                            android:layout_alignParentStart="true"
                            android:background="#ff0000"
                            android:gravity="center"
                            android:text="确定"
                            android:textColor="#ffffff"
                            android:textSize="16dp" />
                    </RelativeLayout>
                </LinearLayout>
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>


</RelativeLayout>

2.开始写代码了
Constant 这类 个是控制改变颜色的工具类(我觉得这可以改进)

public class Constant {
    /**
     * 由购物尺寸Adapter属性,发送广播商品详情页,修改当前选中的尺寸
     */
    public static final String SHOPPINGCART_SIZEADAPTER_SEND_SHOPPINGCART_RECORD_SIZE = "SHOPPINGCART_SIZEADAPTER_SEND_SHOPPINGCART_RECORD_SIZE";
    /**
     * 由购物颜色Adapter属性,发送广播商品详情页,修改当前选中的颜色
     */
    public static final String SHOPPINGCART_COLORADAPTER_SEND_SHOPPINGCART_RECORD_COLOR = "SHOPPINGCART_COLORADAPTER_SEND_SHOPPINGCART_RECORD_COLOR";
}

这是一个万能的配置Viewpager的类

public class FirstFragment extends Fragment {

/**
 *生成方法
 *
 */
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    Log.i("info", "onCreateView.....");
    //配置布局
    View view=inflater.inflate(R.layout.first_fragment, null);
    return view;
}

@Override
public void onDestroyView() {
    super.onDestroyView();
}

}

有这么多基础以后就可以写了
这MainActivity 的类中时 那些颜色种类 都是GrvidView 布局写的 编写者个Demo的人比较懒点就直接写两个adapter

public class MainActivity extends FragmentActivity implements OnClickListener, OnPageChangeListener {
    private ViewPager viewPager;
    private ArrayList<Fragment> fragments;
    private MyPagerAdapter adapter;
    private ImageView ivFirst;
    private ImageView ivTwo;
    private ImageView ivThree;
    private ImageView ivFour;
    private List<ImageView> images;
    //显示购物车 颜色尺寸
    private Button btnAddCart;
    private LinearLayout llAddShoppingCart;
    private ImageView hideShoppingCart;
    private LinearLayout llTransparent;


    //准备加载假数据
    List<String> listSize;
    List<String> listColor;
    private GridView gvAttributeColor;
    private GridView gvAttributeSize;
    private ShoppingCartColorAdapter attributeColorAdapter;
    private ShoppingCartSizeAdapter attributeSizeAdapter;
    InnerBroadcastReceiver receiver;
    private String seledtedColor;
    private String seledtedSize;
    private Button rbAttributeSubmit;
    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    private GoogleApiClient client;

    // private TextView tvImgNumber;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        // 初始化控件
        setView();
        //准备数据源
        setFragment();
        //加载测试数据
        addData();
        //继承自FragmentActivity才可以调用该方法 获取Fragment管理器,要求Activity 调用getSupportFragmentManager方法
        adapter = new MyPagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(adapter);
        //配置监听器
        setListener();
        //注册广播接收者
        registerReceiver();
        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
    }
    //注册广播接收者
    private void registerReceiver(){
        IntentFilter filter = new IntentFilter();
        filter.addAction(Constant.SHOPPINGCART_SIZEADAPTER_SEND_SHOPPINGCART_RECORD_SIZE);
        filter.addAction(Constant.SHOPPINGCART_COLORADAPTER_SEND_SHOPPINGCART_RECORD_COLOR);
        receiver = new InnerBroadcastReceiver();
        registerReceiver(receiver,filter);
    }

    class InnerBroadcastReceiver extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
        if(Constant.SHOPPINGCART_SIZEADAPTER_SEND_SHOPPINGCART_RECORD_SIZE.equals(action)){
            //当前选中的尺寸
            seledtedSize = intent.getStringExtra("currentPositionSize");
        }else if(Constant.SHOPPINGCART_COLORADAPTER_SEND_SHOPPINGCART_RECORD_COLOR.equals(action)){
            //当前选中的颜色
            seledtedColor = intent.getStringExtra("currentPositionColor");
        }
        }
    }


    /**
     * 准备数据源
     */
    private void setFragment() {
        fragments = new ArrayList<Fragment>();
        fragments.add(new FirstFragment());
        fragments.add(new FirstFragment());
        fragments.add(new FirstFragment());
        fragments.add(new FirstFragment());


        images = new ArrayList<ImageView>();
        images.add(ivFirst);
        images.add(ivTwo);
        images.add(ivThree);
        images.add(ivFour);
    }

    private void addData(){
        listColor = new ArrayList<>();
        listSize = new ArrayList<>();
        listColor.add("大红");
        listColor.add("驼色");
        listColor.add("咖啡");
        listColor.add("紫色");
        gvAttributeColor.setNumColumns(listColor.size());
        listSize.add("M");
        listSize.add("L");
        listSize.add("XL");
        listSize.add("XXL");
        listSize.add("XXXL");
        gvAttributeSize.setNumColumns(listSize.size());
        setAttributeAdapter();
    }

    private void setAttributeAdapter(){
        if(listColor.size()>0){
            attributeColorAdapter = new ShoppingCartColorAdapter(MainActivity.this,listColor,gvAttributeColor);
            gvAttributeColor.setAdapter(attributeColorAdapter);
        }
        if(listSize.size()>0){
            attributeSizeAdapter = new ShoppingCartSizeAdapter(MainActivity.this,listSize,gvAttributeSize);
            gvAttributeSize.setAdapter(attributeSizeAdapter);
        }
    }

    /**
     * 监听ImageView
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_first:
                viewPager.setCurrentItem(0);
                break;
            case R.id.iv_two:
                viewPager.setCurrentItem(1);
                break;
            case R.id.iv_three:
                viewPager.setCurrentItem(2);
                break;
            case R.id.iv_four:
                viewPager.setCurrentItem(3);
                break;

        }

    }

    /**
     * 配置监听器
     */
    private void setListener() {
        ivFirst.setOnClickListener(this);
        ivTwo.setOnClickListener(this);
        ivThree.setOnClickListener(this);
        ivFour.setOnClickListener(this);

        viewPager.setOnPageChangeListener(this);
        //viewPager.setOnTouchListener
        //监听ViewPager图片滑动点击事件
        viewPager.setOnTouchListener(new View.OnTouchListener() {
            int flage = 0 ;
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        Log.d("hxl", "11111111111111");
                        flage = 0;
                        break;
                    case MotionEvent.ACTION_MOVE:
                        Log.d("hxl", "2222222222");
                        flage = 1;
                        break;
                    case MotionEvent.ACTION_UP:
                        Log.d("hxl", "33333333333");
                        if (flage == 0) {
                            Log.d("hxl", "delatils");
                        }
                        return false;
                }
                return false;}
        });
        //监听点击添加购物车按钮
        btnAddCart.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
            llAddShoppingCart.setVisibility(View.VISIBLE);
                setAnimation();
            }
        });
        //监听关闭购物车布局选项按钮
        hideShoppingCart.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                llAddShoppingCart.setVisibility(View.GONE);
            }
        });
        //监听头部透明布局部分,点击即关闭购物车选项布局
        llTransparent.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                llAddShoppingCart.setVisibility(View.GONE);
            }
        });
        //监听属性确定按钮
        rbAttributeSubmit.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if(seledtedColor !=null){
                    if(seledtedSize !=null){
                        Toast.makeText(MainActivity.this,"选中尺寸:"+seledtedColor+"    选中尺寸"+seledtedSize,Toast.LENGTH_SHORT).show();
                    }else{
                        Toast.makeText(MainActivity.this,"请选择尺寸",Toast.LENGTH_SHORT).show();
                    }
                }else{
                    Toast.makeText(MainActivity.this,"请选择颜色",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    /**
     * 平移动画
     */
    private void setAnimation() {
        // 从右向左平移
        TranslateAnimation anim = new TranslateAnimation(0, 0, llAddShoppingCart.getHeight(),
                0);
        anim.setDuration(100);
        anim.setInterpolator(new BounceInterpolator());
        llAddShoppingCart.startAnimation(anim);
    }



    /**
     * 初始化控件
     */
    private void setView() {

        ivFirst = (ImageView) findViewById(R.id.iv_first);
        ivTwo = (ImageView) findViewById(R.id.iv_two);
        ivThree = (ImageView) findViewById(R.id.iv_three);
        ivFour = (ImageView) findViewById(R.id.iv_four);
        // tvImgNumber = (TextView) findViewById(R.id.tv_img_number);

        btnAddCart = (Button) findViewById(R.id.btn_shopping_cart);
        llAddShoppingCart = (LinearLayout) findViewById(R.id.ll_add_shopping_cart);
        hideShoppingCart = (ImageView) findViewById(R.id.iv_hide_shopping_cart);
        llTransparent = (LinearLayout) findViewById(R.id.ll_transparent);

        //添加假数据
        gvAttributeColor = (GridView) findViewById(R.id.gv_attribute_color);
        gvAttributeSize = (GridView)findViewById(R.id.gv_attribute_size);

        //属性提交按钮
        rbAttributeSubmit = (Button) findViewById(R.id.btn_shopping_cart_rbAttribute_submit);
    }

    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    public Action getIndexApiAction() {
        Thing object = new Thing.Builder()
                .setName("Main Page") // TODO: Define a title for the content shown.
                // TODO: Make sure this auto-generated URL is correct.
                .setUrl(Uri.parse("http://[ENTER-YOUR-URL-HERE]"))
                .build();
        return new Action.Builder(Action.TYPE_VIEW)
                .setObject(object)
                .setActionStatus(Action.STATUS_TYPE_COMPLETED)
                .build();
    }

    @Override
    public void onStart() {
        super.onStart();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client.connect();
        AppIndex.AppIndexApi.start(client, getIndexApiAction());
    }

    @Override
    public void onStop() {
        super.onStop();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        AppIndex.AppIndexApi.end(client, getIndexApiAction());
        client.disconnect();
    }

    /**
     * ViewPager配置适配器
     *
     * @author Android
     */
    class MyPagerAdapter extends FragmentPagerAdapter {
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        /**
         * viewPager将会调用该方法
         * 通过当前position  获取Fragment 进而
         * 调用Fragment.onCreateView方法获取View对象
         * 显示到ViewPager中。
         */
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

    }

    //设置ImageView小图标
    public void setImageView(int position) {
//
        ivFirst.setImageResource(drawable.timg);
        ivTwo.setImageResource(drawable.timg2);
        ivThree.setImageResource(drawable.timg3);
        ivFour.setImageResource(drawable.timg1);
        Log.e("tedu", "position= " + position);
        images.get(position).setImageResource(drawable.point_pressed);
    }

    /**
     * 监听ViewPager
     *
     * @param
     */
    @Override
    public void onPageScrollStateChanged(int index) {
    }
    @Override
    public void onPageScrolled(int arg0, float ar
                               , int arg2) {
    }
    @Override
    //当选中时
    public void onPageSelected(int index) {
        switch (index) {
            case 0:
                setImageView(0);
                // tvImgNumber.setText("1/6");
                break;
            case 1:
                setImageView(1);
                //tvImgNumber.setText("2/6");
                break;
            case 2:
                setImageView(2);
                //tvImgNumber.setText("3/6");
                break;
            case 3:
                setImageView(3);
                //tvImgNumber.setText("4/6");
                break;
        }


    }


}

3贴的有点多了,不好意思。下面是adapter的调用工具类的方法 我只放了一个就是改变颜色的


/**
 * Created by Administrator on 2016/11/2.
 */

public class ShoppingCartSizeAdapter extends BaseAdapter{
    private LayoutInflater inflater;
    private List<String> listSize;
    private Context context;
    private GridView gridView;
    private int dimensionSize;


    public ShoppingCartSizeAdapter(Context context, List<String> listSize, GridView gridView){
        this.context = context;
        this.listSize = listSize;
        this.gridView = gridView;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        dimensionSize = listSize.size();
        return listSize.size();
    }

    @Override
    public String getItem(int i) {
        return listSize.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        ViewHolder holder;
        String size = getItem(i);
        if(convertView == null){
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.item_shoppingcart_attribute,null);
            holder.rbAttribute = (RadioButton) convertView.findViewById(R.id.rb_attribute);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        holder.rbAttribute.setText(size);
        Log.d("hxl","color=="+size);
        holder.rbAttribute.setTag("rbAttribute"+i);

        holder.rbAttribute.setOnClickListener(new MyOnclickListerner(i));

        return convertView;
    }
    class ViewHolder{
        RadioButton rbAttribute;
    }

    class MyOnclickListerner implements View.OnClickListener {
        private int currentPositionSize;
        public MyOnclickListerner(int i) {
            currentPositionSize = i;
        }
        @Override
        public void onClick(View view) {
            RadioButton rbAttribute = (RadioButton) gridView.findViewWithTag("rbAttribute" + currentPositionSize);
            for(int i=0;i<dimensionSize;i++){
                RadioButton rbAttributeAll = (RadioButton) gridView.findViewWithTag("rbAttribute" + i);
                rbAttributeAll.setChecked(false);
            }
            rbAttribute.setChecked(true);
            sendBrodcastReceiver(rbAttribute.getText().toString());
           // Toast.makeText(context,rbAttribute.getText(),Toast.LENGTH_SHORT).show();
        }
    }
    //发送广播给详情页面记录选中的尺寸
    private void sendBrodcastReceiver(String str){
        Intent intent = new Intent(Constant.SHOPPINGCART_SIZEADAPTER_SEND_SHOPPINGCART_RECORD_SIZE);
        intent.putExtra("currentPositionSize",str);
        context.sendBroadcast(intent);
    }


}

写的太烂了,以后有啥功能想要的发邮件过来我会每周中选一个写出来
并且写到博客中.
55463869@qq.com或者15917398112@163.cm


下载地址:这是个Demo的下载地址–如有需要可以找我拿不用积分

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试

关闭