Android网络编程之一个Android下菜单系统模块的实现(主操作界面)
我们向上一个系统中增加一些功能,在成功登陆后,跳转到一个主操作界面,此界面为所有功能的入口,用一个GridView来实现
GridView的经典实现方法流程:布局文件中声明GridView ---> 编写一个ImageAdapter继承BaseAdapter并主要复写getView()方法 ---> 给GridView添加此adapter
布局文件很简单:
<LinearLayout 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" > <GridView android:id="@+id/gridView" android:layout_width="match_parent" android:layout_height="match_parent" android:columnWidth="90dp" android:numColumns="auto_fit" android:verticalSpacing="10dp" android:horizontalSpacing="10dp" android:stretchMode="columnWidth" android:gravity="center" /> </LinearLayout>
主Activity同样很简单,放在com.moka.activities中:
public class MainMenuActivity extends Activity { private GridView gridView = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_menu); gridView = (GridView) findViewById(R.id.gridView); gridView.setAdapter(new ImageAdapter(this)); } }
ImageAdapter的编写稍微复杂,因为在这里我们把所用监听事件均放在这里处理,在这里我们先添加的是点菜功能,所以姑且把别的监听事件做简单处理
它放在com.moka.provider中:
public class ImageAdapter extends BaseAdapter { private Context context = null; // 所需的图片资源 private Integer[] functionImgs = { R.drawable.diancai, R.drawable.bingtai, R.drawable.zhuantai, R.drawable.chatai, R.drawable.gengxin, R.drawable.shezhi, R.drawable.zhuxiao, R.drawable.jietai }; public ImageAdapter(Context context) { this.context = context; } @Override public int getCount() { return functionImgs.length; } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // 准备向GridView中填充的ImageView图片与其一些参数 ImageView imageView = null; if (convertView == null) { imageView = new ImageView(context); imageView.setLayoutParams(new GridView.LayoutParams(85, 85)); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); imageView.setPadding(8, 8, 8, 8); } else { imageView = (ImageView) convertView; } // 设置图片源 imageView.setImageResource(this.functionImgs[position]); // 设置监听事件 switch (position) { case 0: imageView.setOnClickListener(new OrderListener()); break; case 1: imageView.setOnClickListener(new UnionTableListener()); break; case 2: imageView.setOnClickListener(new ChangeTableListener()); break; case 3: imageView.setOnClickListener(new CheckTableListener()); break; case 4: imageView.setOnClickListener(new UpdateListener()); break; case 5: imageView.setOnClickListener(new SetupListener()); break; case 6: imageView.setOnClickListener(new ExitListener()); break; case 7: imageView.setOnClickListener(new PayListener()); break; } return imageView; } private class OrderListener implements OnClickListener { @Override public void onClick(View v) { Intent intent = new Intent(context, OrderActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent); } } private class UnionTableListener implements OnClickListener { @Override public void onClick(View v) { Toast.makeText(context, "并台!", Toast.LENGTH_SHORT).show(); } } private class ChangeTableListener implements OnClickListener { @Override public void onClick(View v) { Toast.makeText(context, "转台!", Toast.LENGTH_SHORT).show(); } } private class CheckTableListener implements OnClickListener { @Override public void onClick(View v) { Toast.makeText(context, "查台!", Toast.LENGTH_SHORT).show(); } } private class UpdateListener implements OnClickListener { @Override public void onClick(View v) { Toast.makeText(context, "更新!", Toast.LENGTH_SHORT).show(); } } private class SetupListener implements OnClickListener { @Override public void onClick(View v) { Toast.makeText(context, "设置!", Toast.LENGTH_SHORT).show(); } } private class ExitListener implements OnClickListener { @Override public void onClick(View v) { Toast.makeText(context, "注销!", Toast.LENGTH_SHORT).show(); } } private class PayListener implements OnClickListener { @Override public void onClick(View v) { Toast.makeText(context, "结台!", Toast.LENGTH_SHORT).show(); } } }
关于点菜的OrderActivity及其服务器端有一定的难度,我们稍后再讨论,先看一下主操作界面效果: