创新互联基于分布式IDC数据中心构建的平台为众多户提供西部信息机房 四川大带宽租用 成都机柜租用 成都服务器租用。>public class MainActivity extends Activity {
private GridView gridView;
private int[] images = {R.drawable.img_item0,R.drawable.img_item1
,R.drawable.img_item2,R.drawable.img_item3,R.drawable.img_item4,
R.drawable.img_item5,R.drawable.img_item6,R.drawable.img_item7,
R.drawable.img_item8};
private String[] desc = {"该减肥了","该运动运动了","","","","","","",""};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gridview);
gridView= (GridView) findViewById(R.id.gv);
MyAdapter adapter= new MyAdapter(images,desc);
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Animation anim= new TranslateAnimation(-2, 2, 0, 0,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
anim.setDuration(1000);
anim.setRepeatCount(3);
anim.setRepeatMode(Animation.REVERSE);
ImageView image= (ImageView) view.findViewById(R.id.image);
image.setAnimation(anim);
anim.start();
}
});
}
class MyAdapter extends BaseAdapter{
int[] imgItemRes;
String[] imgItemDesc;
public MyAdapter(int[] imgItemRes,String[] imgItemDesc){
this.imgItemRes = imgItemRes;
this.imgItemDesc = imgItemDesc;
}
@Override
public int getCount() {
return imgItemRes.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null){
LayoutInflater inflater= getLayoutInflater();
convertView= inflater.inflate(R.layout.gridviewitem, null);
}
ImageView image= (ImageView) convertView.findViewById(R.id.image);
TextView text= (TextView) convertView.findViewById(R.id.text);
image.setImageResource(imgItemRes[position]);
text.setText(imgItemDesc[position]);
return convertView;
}
}
}
gridview.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" /> <GridView
android:id="@+id/gv"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="60dp"
android:numColumns="3"
android:verticalSpacing="10dp"
android:horizontalSpacing="15dp"
android:stretchMode="columnWidth"
android:gravity="center" /></LinearLayout>
gridviewitem.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="center_horizontal" > <ImageView
android:id="@+id/image"
android:layout_width="fill_parent"
android:layout_height="wrap_content" /> <TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /></LinearLayout>
分享标题:GridView的使用-创新互联
浏览地址:https://www.cdcxhl.com/article36/dpshsg.html
成都网站建设公司_创新互联,为您提供网站设计、静态网站、网站排名、关键词优化、手机网站建设、微信小程序
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联