zoukankan      html  css  js  c++  java
  • 左右上下都能滚动的效果

    最近在做一个项目,要求实现上下能滚动,每项能左右滚动,最后效果是实现了,可数据的通信可能有点麻烦,暂时又被砍掉了,先放上来存档,转载请注明出处,谢谢

    代码如下

    public class GroupActivity extends Activity {

        private List<TeamBean> gbList;
    //    private MyListAdapter adapter;
        private ItemClickEvent listener;
    //    private MyHandler handler;
        private GridView[] gridvs ;

        //
        private LinearLayout ll_main;
        private ScrollView scll ;
        private LinearLayout scll_sub_ll;
        private HorizontalScrollView hls;
        private LinearLayout hls_sub_ll;
        private GridView gv ;
        private LinearLayout.LayoutParams ll_pmm;
        private LinearLayout.LayoutParams ll_pmw;
        private LinearLayout.LayoutParams ll_pww;
        private LinearLayout.LayoutParams ll_head_wh;
        private int headViewW = 0, headViewH = 0;
        
        private TextView tv_type;
        private View headView;
        private LayoutInflater flater;
        private Button bt_back, bt_right;
        private TextView top_title;
        private MyListAdapter[] myAdaps;
        
        
        Handler hand = new Handler(){
            public void handleMessage(Message msg) {
                switch (msg.what) {
                case 1:
                    Log.i("AAA","case1 "+msg.what);
                    if(null == gbList || gbList.size() < 0){
                        return;
                    }
                    
                    init();
                    
                    break;
                    
                case 2:
                    setContentView(ll_main);
                    break;
                default:
                    break;
                }
            };
        };
        
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    //        setContentView(R.layout.main);
            
            new Thread() {
                @SuppressLint("NewApi")
                public void run() {
                    gbList = new ArrayList<TeamBean>();
                    for (int i = 0; i < 10; i++) {
                        
                        TeamBean item = new TeamBean("abc" + i, "123456"
                                + i);
                        Log.i("AAA","item = "+item);
                        gbList.add(item);
                    }
                    Message msg = Message.obtain();
                    msg.what = 1;
                    hand.sendMessage(msg);
                }
            }.start();
            
        }
        private void init(){
            headView = getLayoutInflater().inflate(R.layout.dingbujiemian, null);
            headView.setBackgroundResource(R.drawable.top_bg);
            
            bt_back = (Button) headView.findViewById(R.id.zuobiananniu);
            bt_back.setVisibility(View.VISIBLE);
            bt_back.setBackgroundResource(R.color.transparent);
            bt_back.setText(getResources().getString(R.string.fanhui));
            bt_back.setOnClickListener(new OnClickListener() {
                
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    finish();
                }
            });
            bt_right = (Button) headView.findViewById(R.id.youbiananniu);
            bt_right.setVisibility(View.INVISIBLE);
            top_title = (TextView) headView.findViewById(R.id.zhongjianzifu);
            top_title.setVisibility(View.INVISIBLE);
            
            
            ll_pmm = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
            ll_pmw = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
            ll_pww = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            
            
    //         int w = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
    //            int h = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
    //            headView.measure(w, h);
    //            headViewW =headView.getMeasuredWidth();
    //            headViewH =headView.getMeasuredHeight();
    //            ll_head_wh = new LinearLayout.LayoutParams(headViewW, headViewH);
    //        headView.setLayoutParams(ll_head_wh);
            
            
            ll_main = new LinearLayout(this);
            ll_main.setOrientation(LinearLayout.VERTICAL);
            ll_main.setBackgroundColor(Color.WHITE);
            ll_main.setLayoutParams(ll_pmm);
            
            ll_main.addView(headView);
            
            scll = new ScrollView(this);
            scll.setLayoutParams(ll_pmm);
            
            scll_sub_ll = new LinearLayout(this);
            scll_sub_ll.setOrientation(LinearLayout.VERTICAL);
            scll_sub_ll.setLayoutParams(ll_pmm);
            
            
            int arrs[] = new int[15];
            myAdaps = new MyListAdapter[arrs.length];
            
            for (int i = 0; i < arrs.length; i++) {
                hls = new HorizontalScrollView(this);
                hls.setLayoutParams(ll_pmw);
                hls.setBackgroundColor(Color.GRAY);
                hls.setHorizontalScrollBarEnabled(false);
                
                
                hls_sub_ll = new LinearLayout(this);
                hls_sub_ll.setOrientation(LinearLayout.HORIZONTAL);
                hls_sub_ll.setLayoutParams(ll_pmw);
                
                gv = new GridView(this);
                gv.setLayoutParams(ll_pmw);
                
                LayoutParams params = new LayoutParams(gbList.size()
                        *(120+6) , LayoutParams.WRAP_CONTENT);
                
                gv.setLayoutParams(params);
                gv.setColumnWidth(120);
                gv.setHorizontalSpacing(6);
                /*gv.setStretchMode(gv.NO_STRETCH);*/
                gv.setNumColumns(gbList.size());
                
                myAdaps[i] = new MyListAdapter(GroupActivity.this);
                listener = new ItemClickEvent();
                if(i == arrs.length - 1){
                    
                }else{
                    gv.setAdapter(myAdaps[i]);
                    gv.setOnItemClickListener(listener);
                }
                
                
                hls_sub_ll.addView(gv);
                hls.addView(hls_sub_ll);
                
                tv_type = new TextView(this);
                tv_type.setTextSize(22);
                tv_type.setTextColor(Color.BLACK);
                if(i == arrs.length - 1){
                    tv_type.setText("选中的好友 ");
                }else{
                    tv_type.setText("朋友 "+i);
                }
                scll_sub_ll.addView(tv_type);
                scll_sub_ll.addView(hls);
                
            }
            scll.addView(scll_sub_ll);
            ll_main.addView(scll);
            Message msg = Message.obtain();
            msg.what = 2;
            hand.sendMessage(msg);
        }
        private class MyListAdapter extends BaseAdapter {
            private final String TAG = "MyListAdapter";
            private LayoutInflater mInflater;
            private final Context context;

            public MyListAdapter(Context context) {
                this.context = context;
                mInflater = (LayoutInflater) context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            }

            public int getCount() {
                Log.i("AAA","gbList.size() = "+gbList.size());
                return gbList.size();
                
            }

            public Object getItem(int position) {
                return gbList.get(position);
            }

            public long getItemId(int position) {
                return position;
            }

            @TargetApi(Build.VERSION_CODES.HONEYCOMB)
            @SuppressLint("NewApi")
            public View getView(int position, View convertView, ViewGroup parent) {
                Log.i("AAA","position = "+position);
                TeamBean item = gbList.get(position);
                Log.i("AAA","getView() , item = "+item);
                CellHolder cellHolder;
                if (convertView == null) {
                    cellHolder = new CellHolder();
                    convertView = mInflater.inflate(R.layout.team_item, null);
                    cellHolder.ivIcon = (ImageView) convertView
                            .findViewById(R.id.ivIcon);
                    cellHolder.name = (TextView) convertView
                            .findViewById(R.id.name);
                    convertView.setTag(cellHolder);
                } else {
                    cellHolder = (CellHolder) convertView.getTag();
                }
                cellHolder.ivIcon.setImageResource(R.drawable.icos5);
                cellHolder.name.setText(item.getName());
                cellHolder.name.setTextSize(12);
                return convertView;
            }
        }

        private class CellHolder {
            ImageView ivIcon;
            TextView name;
        }

        private class ItemClickEvent implements OnItemClickListener {
            public void onItemClick(AdapterView<?> parent, View view, int position,
                    long id) {
                Log.i("item","parent = "+parent+", view = "+view+", position = "+position);
                
            }

        }
    }


  • 相关阅读:
    wex5 实战 框架拓展之2 事件派发与data刷新
    wex5 实战 框架拓展之1 公共data组件(Data)
    wex5 实战 HeidiSQL 导入Excel数据
    wex5 实战 手指触屏插件 hammer的集成与优劣
    wex5 实战 登陆帐号更换与用户id一致性
    wex5 实战 用户点评与提交设计技巧
    wex5 实战 省市县三级联动与地址薄同步
    wex5 实战 wex5与js的组件关系与执行顺序(父子与先后)
    wex5 实战 单页模式下的多页面数据同步
    [BZOJ]4237: 稻草人
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3249191.html
Copyright © 2011-2022 走看看