Android实现购物车详情简单效果

效果图

Android实现购物车详情简单效果_第1张图片

 

1.MainActivity

package com.example.sj.bigtracing;

import android.os.Bundle;
import android.support.design.widget.TabItem;
import android.support.design.widget.TabLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

//实现购物车详情效果
public class MainActivity extends AppCompatActivity {

    private TabItem name1;
    private TabItem name2;
    private TabItem name3;
    private ListView lv;
    private TabLayout tab;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
    }

    int noyong;
    private int position;

    private void initData() {
        List list = new ArrayList<>();
        for (int i = 0; i < 40; i++) {
            list.add("天王盖地虎,拉祜。。。" + i);
        }
        MyAdapter adapter = new MyAdapter(this, list);
        lv.setAdapter(adapter);


        lv.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                switch (noyong) {
                    case 0:
                        TabLayout.Tab tabAt1 = tab.getTabAt(0);
                        tabAt1.select();
                        break;
                    case 10:
                        TabLayout.Tab tabAt2 = tab.getTabAt(1);
                        tabAt2.select();
                        break;
                    case 20:
                        TabLayout.Tab tabAt3 = tab.getTabAt(2);
                        tabAt3.select();
                        break;
                }
            }




            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                switch (firstVisibleItem) {
                    case 0:
                        noyong = 0;

                        break;
                    case 10:
                        noyong = 10;

                        break;
                    case 20:
                        noyong = 20;

                        break;
                    case 9:
                        noyong = 0;

                        break;

                    case 19:
                        noyong = 10;

                        break;


                }
            }
        });


        for (int i = 0; i < tab.getTabCount(); i++) {
            TabLayout.Tab tabAt = tab.getTabAt(i);
            if (tabAt == null) {
                Class c = tabAt.getClass();

                try {
                    Field field = c.getDeclaredField("mView");
                    field.setAccessible(true);
                    final View view = (View) field.get(tabAt);
                    if (view == null) {
                        return;
                    }
                    view.setTag(i);
                    view.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            position = (int) view.getTag();
                            switch (position) {
                                case 0:
                                    lv.setSelection(20);
                                    break;
                                case 1:
                                    lv.setSelection(10);
                                    break;
                                case 2:
                                    lv.setSelection(0);
                                    break;


                            }

                        }
                    });


                } catch (NoSuchFieldException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }


    }

    private void initView() {
        name1 = (TabItem) findViewById(R.id.name1);
        name2 = (TabItem) findViewById(R.id.name2);
        name3 = (TabItem) findViewById(R.id.name3);
        lv = (ListView) findViewById(R.id.lv);
        tab = (TabLayout) findViewById(R.id.tab);
    }
}

MainActivity的xml布局




    

        

        

        

            
                

                

                

            

        
    

    

    


2.MyAdapter 适配器

package com.example.sj.bigtracing;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

public class MyAdapter extends BaseAdapter {
    private Context context;

    public MyAdapter(Context context, List list) {
        this.context = context;
        this.list = list;
    }

    private List list;

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

    @Override
    public Object getItem(int position) {
        return null;
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.item_tv.setText(list.get(position));
        return convertView;
    }

    public static class ViewHolder {
        public View rootView;
        public TextView item_tv;

        public ViewHolder(View rootView) {
            this.rootView = rootView;
            this.item_tv = (TextView) rootView.findViewById(R.id.item_tv);
        }

    }
}

适配器xml布局




    

        
    

 

你可能感兴趣的:(Android实现购物车详情简单效果)