android之使用ListView实现多菜单功能

我们知道很多软件都有设置菜单项如微信墨迹天气等的设置界面。当设置里有很多选项这时我们可以选择使用ListView实现。

布局文件如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	android:background="@drawable/splash_background"
	>
	<ListView android:id="@+id/more_List"
		android:layout_width="fill_parent"
		android:layout_height="fill_parent"
		android:divider="@drawable/list_bottom_line"
		android:listSelector="#00000000"
		android:cacheColorHint="#00000000">
	</ListView>
</LinearLayout>
很简单只有一个ListView

item的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="horizontal"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:padding="8dip"
	android:background="@drawable/list_item"
	>
	<ImageView android:id="@+id/item_icon"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
	/>
	<TextView android:id="@+id/more_item_text"
		android:layout_marginLeft="10dip"
		android:textSize="20dip"
		android:textColor="#000000"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"/>
	<ImageView android:id="@+id/item_image"
		android:layout_marginTop="5dip"
		android:layout_marginLeft="10dip"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:background="@drawable/triangle"
	/>
</LinearLayout>
设置成水平布局,一次是图标,文字说明,箭头图标

主要代码如下:

package com.cloay.news;

import com.cloay.news.bean.MoreAdapter;
import com.cloay.news.service.MainService;
import com.cloay.news.util.AlertDlgUtil;
import com.waps.AppConnect;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

/**
 * 更多界面包含帮助,关于等。
 * @author Cloay
 * 2012-3-3
 * 下午09:45:12
 */
public class MoreActivity extends NewsReaderActivity {
	private ListView moreList;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.more);
		MainService.allActivity.add(this);
		moreList = (ListView) findViewById(R.id.more_List);
		MoreAdapter adapter = new MoreAdapter(this);
		moreList.setAdapter(adapter);
		moreList.setOnItemClickListener(new OnItemClickListener() { //为每一个item设置相应的响应

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				switch(position){
				case 0: //关于
					startActivity(AboutActivity.class);

					break;
				case 1:
					//分享
					Intent intent=new Intent(Intent.ACTION_SEND);
				    intent.setType("text/plain");
				    intent.putExtra(Intent.EXTRA_SUBJECT, "分享");
				    intent.putExtra(Intent.EXTRA_TEXT, "I would like to share this with you...");
				    startActivity(Intent.createChooser(intent, getTitle()));
					break;
				case 2:
					//帮助
					startActivity(HelpActivity.class);
					break;
				case 3:
					//意见反馈
					AppConnect.getInstance(MoreActivity.this).showFeedback();
					break;
				case 4://退出
					AlertDlgUtil.AlertExit(MoreActivity.this);
					break;
				}
			}
		});
	}
	private void startActivity(Class<?>cls){
		Intent intent = new Intent(this, cls);
		this.startActivity(intent);
	}
}

下面是为ListView提供数据的Adapter:

package com.cloay.news.bean;

import java.util.ArrayList;
import java.util.List;

import com.cloay.news.R;

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

/**
 * 为更多中listview提供数据
 * @author Cloay
 * 2012-3-5
 * 下午06:28:14
 */
public class MoreAdapter extends BaseAdapter{

	private LayoutInflater inflater;
	private Context context;
	public static final List<String> more_list = new ArrayList<String>();//为条目提供数据
	public MoreAdapter(Context context){
		this.context = context;
		inflater = LayoutInflater.from(this.context);
		more_list.add(context.getResources().getString(R.string.main_square));
		more_list.add(context.getResources().getString(R.string.share));
		more_list.add(context.getResources().getString(R.string.help_text));
		more_list.add(context.getResources().getString(R.string.advice));
		more_list.add(context.getResources().getString(R.string.exit_title));
	}
	@Override
	public int getCount() {
		return more_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) {
		if (convertView == null) {  
            convertView = inflater.inflate(  
                    R.layout.more_list, null);  
        } 
		ImageView icon = (ImageView) convertView.findViewById(R.id.item_icon);//设置每个条目的图标
		if(0 == position){
			icon.setBackgroundResource(R.drawable.icon_square);  
		}else if(1 == position){
			icon.setBackgroundResource(android.R.drawable.ic_menu_share);
		}else if(2 == position){
			icon.setBackgroundResource(R.drawable.icon_setting);
		}else if(3 == position){
			icon.setBackgroundResource(android.R.drawable.ic_menu_edit);
		}else{
			icon.setBackgroundResource(R.drawable.menu_exit);
		}
		TextView text = (TextView) convertView.findViewById(R.id.more_item_text); //设置条目的文字说明
		text.setText(more_list.get(position));
		return convertView;
	}

}

运行效果图如下:

android之使用ListView实现多菜单功能_第1张图片

就介绍到这里,有什么问题请留言大家一起学习交流!

说明:转载请注明出处!


你可能感兴趣的:(android,list,ListView,layout,null,encoding)