Android之ListView和GridView
程序开发
2023-09-15 13:52:57
用ListView显示列表,列表的每一行包含不同的组件。
响应某行的某个组件。
代码等会再发
以下是GridView的代码:
package com.xplus.easyer.horizontal;import java.util.ArrayList;
import java.util.List;import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;import com.xplus.easyer.R;/*** @author easyer* */
public class MainActivity01 extends Activity {private int[] imageIds = { R.drawable.p1, R.drawable.p2, R.drawable.p3,R.drawable.p4, R.drawable.p5, R.drawable.p6, R.drawable.p7,R.drawable.p8, R.drawable.p9, R.drawable.pp };private GridView gridView;private List list;private MyAdapter adapter;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);setContentView(R.layout.main);gridView = (GridView) findViewById(R.id.gridView_main);list = new ArrayList();getData();LayoutParams params = new LayoutParams(list.size() * (200 + 6),LayoutParams.WRAP_CONTENT);gridView.setLayoutParams(params);gridView.setColumnWidth(200);gridView.setHorizontalSpacing(6);gridView.setStretchMode(GridView.NO_STRETCH);gridView.setNumColumns(list.size());adapter = new MyAdapter(this);gridView.setAdapter(adapter);}/** 自定义适配器*/public class MyAdapter extends BaseAdapter {private Context context;private LayoutInflater mInflater;public MyAdapter(Context context) {super();this.context = context;mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);}@Overridepublic int getCount() {return list.size();}@Overridepublic Object getItem(int position) {return list.get(position);}@Overridepublic long getItemId(int position) {return position;}@Overridepublic View getView(final int position, View convertView, ViewGroup parent) {ColumInfo colum = list.get(position);ViewHolder holder;if (convertView == null) {holder = new ViewHolder();convertView = mInflater.inflate(R.layout.item, null);holder.imageview = (ImageView) convertView.findViewById(R.id.imageView_item);holder.textview = (TextView) convertView.findViewById(R.id.textView_item);holder.imageview.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {System.out.println("你点击的是"+position+"行");}});convertView.setTag(holder);} else {holder = (ViewHolder) convertView.getTag();}holder.imageview.setImageResource(list.get(position).getImageviewId());holder.textview.setText(list.get(position).getTextview());return convertView;}}/** 控件适配*/public class ViewHolder {ImageView imageview;TextView textview;}/** 获取数据*/private void getData() {ColumInfo colum = null;for (int i = 0; i < imageIds.length; i++) {colum = new ColumInfo();colum.setImageviewId(imageIds[i]);colum.setTextview("图片" + i);list.add(colum);}}/** 自定义实体类---每一列的信息*/public class ColumInfo {private int imageid;private String textview;public ColumInfo() {super();}public ColumInfo(int imageid, String textview) {super();this.imageid = imageid;this.textview = textview;}public int getImageviewId() {return imageid;}public void setImageviewId(int imageid) {this.imageid = imageid;}public String getTextview() {return textview;}public void setTextview(String textview) {this.textview = textview;}}}
标签:
上一篇:
pnpm / yarn / npm管理依赖包
下一篇:
相关文章
-
无相关信息