GridView点击更换头像案例+适配器优化

package com.yztc_gridview;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

/**
* GridView的演示以及优化
*/

public class MainActivity extends Activity {

private static final int REQUEST_CODE = 1;// 请求码
private ImageView iv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    iv = (ImageView) findViewById(R.id.iv_show);
}

// 点击跳转到ResultActivity界面,选择头像并且展示
public void onClick(View v) {
    Intent intent = new Intent(MainActivity.this, ResultActivity.class);
    startActivityForResult(intent, REQUEST_CODE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == REQUEST_CODE && resultCode == Activity.RESULT_OK) {
        // 获取回传选择头像图片资源id,并进行展示
        int imageId = data.getIntExtra("imageId", R.drawable.ic_launcher);
        iv.setImageResource(imageId);
        }
    }
}

主界面布局

<RelativeLayout 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" >

<ImageView
    android:id="@+id/iv_show"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:src="@drawable/a" />

<Button
    android:id="@+id/btn_photo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/iv_show"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="27dp"
    android:onClick="onClick"
    android:text="点击选择头像" />

</RelativeLayout>

GridView界面

package com.yztc_gridview;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

/**
* GridView的演示以及优化
*/

public class ResultActivity extends Activity {

private GridView gv;
private List<Map<String, Object>> list;
private int[] images = { R.drawable.a, R.drawable.b, R.drawable.c,
        R.drawable.d, R.drawable.e, R.drawable.f, R.drawable.g,
        R.drawable.h, R.drawable.i, R.drawable.j };

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_result);

    gv = (GridView) findViewById(R.id.gv);
    // 创建一个数据源
    list = new ArrayList<Map<String, Object>>();
    for (int i = 0; i < images.length; i++) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("img", images[i]);
        map.put("text", "头像" + i);
        list.add(map);
    }

    MyBaseAdapter adapter = new MyBaseAdapter();
    gv.setAdapter(adapter);

    // 点击GridView中某一项时触发的回调方法
    gv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            Intent intent = new Intent();
            intent.putExtra("imageId", images[position]);// 获取当前点击头像的资源id
            setResult(Activity.RESULT_OK, intent);// 将资源id存储到intent中进行回传
            ResultActivity.this.finish();
        }
    });
}

// 自定义适配器
class MyBaseAdapter extends BaseAdapter {

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.grid_item, null);
            holder = new ViewHolder();
            holder.iv = (ImageView) convertView.findViewById(R.id.imageView1);
            holder.tv = (TextView) convertView.findViewById(R.id.textView1);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tv.setText((CharSequence) list.get(position).get("text"));
        holder.iv.setImageResource((Integer) list.get(position).get("img"));
        return convertView;
    }
}

// 优化
static class ViewHolder {
    ImageView iv;
    TextView tv;
    }
}

GridView布局

<RelativeLayout 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/gv"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:columnWidth="80dp"
    android:horizontalSpacing="10dp"
    android:numColumns="auto_fit"
    android:verticalSpacing="10dp" >
</GridView>
<!--
android:numColumns="auto_fit" GridView一行中展示item的列数 
android:columnWidth="80dp" 设置每一列的宽度
android:horizontalSpacing="10dp"行间距
android:verticalSpacing="10dp"列间距
-->

</RelativeLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值