菜单选项按钮(2)

在上一篇的基础上 多了一种自定义的 Adapter:

代码:

package com.zhangli.listdialog;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		//定义一个按钮 点出菜单对话框
		final Activity activity = this;
		findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				activity.openOptionsMenu();
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	// 自定义Adapter

	private class MyAdapter extends ArrayAdapter {

		private LayoutInflater infalter;

		public MyAdapter(Context context, int resource) {
			super(context, resource);

			infalter = LayoutInflater.from(context);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null)
				convertView = infalter.inflate(R.layout.item_view, null);

			TextView text = (TextView) convertView.findViewById(R.id.text);
			text.setText("data " + position);

			return convertView;
		}

		@Override
		public int getCount() {
			return 50;
		}

	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}

		// 自定义ListView对话框
		if (id == R.id.listview) {
			AlertDialog.Builder builder = new AlertDialog.Builder(this);

			LayoutInflater mLayoutInflater = getLayoutInflater();
			View view = mLayoutInflater.inflate(R.layout.listview, null);

			ListView lv = (ListView) view.findViewById(R.id.list_view);

			MyAdapter adapter = new MyAdapter(this, -1);
			lv.setAdapter(adapter);

			lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long id) {
					Toast.makeText(getApplicationContext(), "分享成功!" + pos, Toast.LENGTH_SHORT).show();
				}
			});

			lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

				@Override
				public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
					Toast.makeText(getApplicationContext(), "长按 -> 分享成功!" + pos, Toast.LENGTH_SHORT).show();
					return true;
				}
			});

			builder.setView(view);
			builder.setTitle("分享");
			builder.create().show();
		}

		return super.onOptionsItemSelected(item);
	}
}

 

main.xml:

    <item
        android:id="@+id/listview"
        android:orderInCategory="95"
        android:showAsAction="never"
        android:title="自定义ListView"/>


新建一个空的ListView布局,取名list_view:

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/list_view" >
    

</ListView>


新建一个item_view布局,显示出自定义会话框:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    
    <ImageView
         android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/imageView"
        android:text="MyView" />

</RelativeLayout>


效果图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值