ListView

安卓

  • MVP设计模式
  • ListView

MVP设计模式

ListView_第1张图片 类似Javaweb中的MVC,model view controller

ListView

菜鸟教程

纯文字的滑动列表

public class MainActivity extends Activity {
     
    
    //Model:模型层,即数据源(使用集合来存储)
    private ArrayList<String> mDataList;
    
    //View:视图层,即滑动列表
    private ListView mListView;
    
    //Model:模型层,即适配器
    private ArrayAdapter<String> mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
     
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        initModel();
        initAdapter();
        initList();
    }
    
    /**
     * 初始化模型层
     */
    private void initModel() {
     
        mDataList = new ArrayList<String>();
        //添加数据
        mDataList.add("娘子");
        mDataList.add("半岛铁盒");
        mDataList.add("西界");
        mDataList.add("大碗宽面");
        mDataList.add("七里香");
        mDataList.add("告白气球");
        mDataList.add("缘分一道桥");
        mDataList.add("想自由");
        mDataList.add("夜曲");
        mDataList.add("青花瓷");
    }
    
    /**
     * 初始化视图层
     */
    private void initList() {
     
        mListView = (ListView) findViewById(R.id.list_view);
        //补充V和P的关联
        mListView.setAdapter(mAdapter);
    }
    
    private void initAdapter() {
     
        //参数一:当前的类名.this
        //参数二:包含TextView的布局文件的ID
        //参数三:数据源
        mAdapter = new ArrayAdapter<String>(MainActivity.this, R.layout.item_layout, mDataList);
    }
}

文字+图片 (最常用)

public class MainActivity extends Activity {
     

	// Model
	private ArrayList<String> mtextList;
	private ArrayList<Integer> mImageList;
	private ArrayList<HashMap<String, Object>> mDataList;

	// view
	private GridView mGridView;
	// M adapter
	private SimpleAdapter mSimpleAdapter;

	private void initModel() {
     
		mtextList = new ArrayList<String>();
		mtextList.add("奔驰");
		mtextList.add("宝马");
		mtextList.add("大众");
		mtextList.add("法拉利");
		mtextList.add("兰博基尼");
		mtextList.add("玛莎拉蒂");
		mtextList.add("保时捷");
		mtextList.add("劳斯莱斯");
		mtextList.add("五菱宏光");

		mImageList = new ArrayList<Integer>();
		mImageList.add(R.drawable.benz);
		mImageList.add(R.drawable.bmw);
		mImageList.add(R.drawable.dazhong);
		mImageList.add(R.drawable.falali);
		mImageList.add(R.drawable.lambo);
		mImageList.add(R.drawable.maserati);
		mImageList.add(R.drawable.porsche);
		mImageList.add(R.drawable.rolls);
		mImageList.add(R.drawable.wl);
		//
		mDataList = new ArrayList<HashMap<String, Object>>();
		for (int i = 0; i < mtextList.size(); i++) {
     
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("img", mImageList.get(i));
			map.put("txt", mtextList.get(i));

			mDataList.add(map);
		}
	}

	private void intiAdaptr() {
     
		String[] keys = new String[] {
      "img", "txt" };
		int[] ids = new int[] {
      R.id.item_img, R.id.item_txt };
		mSimpleAdapter = new SimpleAdapter(MainActivity.this, mDataList,
				R.layout.item_layout, keys, ids);
	}

	private void initView() {
     
		mGridView = (GridView) findViewById(R.id.grid);
		mGridView.setAdapter(mSimpleAdapter);
		mGridView.setOnItemClickListener(new OnItemClickListener() {
     
			// 参数一:点击的Item所在的容器控件对象
			// 参数二:单个Item布局的最外层的布局/控件
			// 参数三:点击的item的序号(重要)
			// 参数四:id,通常等同于参数三
			@Override
			public void onItemClick(AdapterView<?> parent, View v,
					int position, long id) {
     
				Toast.makeText(MainActivity.this, Integer.toString(position),
						Toast.LENGTH_SHORT).show();
			}
		});

		mGridView.setOnItemLongClickListener(new OnItemLongClickListener() {
     

			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view,
					int position, long id) {
     
				// 长按删除
				// 先删除数据源,再刷新页面(增加和修改数据同理)
				Toast.makeText(MainActivity.this,
						"已删除" + mDataList.get(position).get("txt").toString(),
						Toast.LENGTH_SHORT).show();

				mDataList.remove(position); // 删除数据
				mSimpleAdapter.notifyDataSetChanged(); // 刷新页面
				// 建议最终项目如果要用长按,长按收藏
				return true; // 改了true就不会跟点击冲突了
				// Log.d()
			}
		});
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
     
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_main);
		initModel();
		intiAdaptr();
		initView();
	}
}

效果如下:
ListView_第2张图片

你可能感兴趣的:(Android)