最近在做一個項目,要求實現上下能滾動,每項能左右滾動,最後效果是實現了,可資料的通訊可能有點麻煩,暫時又被砍掉了,先放上來存檔,轉載請註明出處,謝謝
代碼如下
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); } }}