JAVA代码:
package cn.ywspace.demo;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity; import android.app.ActivityGroup; import android.content.Context; import android.graphics.Color; import android.graphics.drawable.ColorDrawable; import android.os.Bundle; import android.util.Log; import android.view.Gravity; 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.RelativeLayout; import android.widget.TextView; import android.widget.Toast;
public class FirstProActivity extends Activity { /** Called when the activity is first created. */
GridView gView;
List<Integer> icoList;
List<Integer> unselIcoList;
List<String> titlelList;
int lastClickNum,curClickNum;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gView=(GridView)findViewById(R.id.grid);
SetData();
GridAdper gridAdper=new GridAdper(this);
gView.setAdapter(gridAdper);
gView.setNumColumns(4);
gView.setGravity(Gravity.CENTER);
gView.setSelector(new ColorDrawable(Color.TRANSPARENT));
gView.setBackgroundResource(R.drawable.tab_bg);
setListener();
}
private void setListener(){
gView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
Log.i("ywspace",""+position);
lastClickNum=curClickNum;
curClickNum=position;
if (curClickNum==lastClickNum) {
return;
}
RelativeLayout layout=(RelativeLayout)gView.getChildAt(position);
layout.setBackgroundResource(R.drawable.product_title_bg);
layout.getChildAt(0).setBackgroundResource(icoList.get(position));
for (int i = 0; i < icoList.size(); i++) {
if(i!=position){
RelativeLayout temp=(RelativeLayout)gView.getChildAt(i);
temp.setBackgroundDrawable(null);
temp.getChildAt(0).setBackgroundResource(unselIcoList.get(i));
}
}
}
});
}
private void SetData(){
icoList=new ArrayList<Integer>();
unselIcoList=new ArrayList<Integer>();
titlelList=new ArrayList<String>();
icoList.add(R.drawable.tab_home_selected);
unselIcoList.add(R.drawable.tab_home_normal);
icoList.add(R.drawable.tab_category_selected);
unselIcoList.add(R.drawable.tab_category_normal);
icoList.add(R.drawable.tab_rank_selected);
unselIcoList.add(R.drawable.tab_rank_normal);
icoList.add(R.drawable.tab_app_select);
unselIcoList.add(R.drawable.tab_app_unselect);
titlelList.add("主页");
titlelList.add("分类");
titlelList.add("排行");
titlelList.add("管理");
}
private class GridAdper extends BaseAdapter{
LayoutInflater inflater;
public GridAdper(Context context){
inflater=LayoutInflater.from(context);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return titlelList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ItemHolder holder;
if (convertView==null) {
convertView=(View)inflater.inflate(R.layout.grid_item, null);
holder=new ItemHolder();
holder.img=(ImageView)convertView.findViewById(R.id.img);
holder.txt=(TextView)convertView.findViewById(R.id.title);
convertView.setTag(holder);
}else{
holder=(ItemHolder)convertView.getTag();
}
holder.img.setBackgroundResource(unselIcoList.get(position));
holder.txt.setText(titlelList.get(position));
return convertView;
}
}
private final class ItemHolder{
public ImageView img;
public TextView txt;
}
}
XML 布局:<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ViewFlipper android:id="@+id/fliper" ; android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" /> <GridView android:id="@+id/grid" ; android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:layout_gravity="bottom" android:layout_weight="0"
/>
</LinearLayout>
各位大牛 帮忙看看 那里出问题了?
Log.i("ywspace"
,
""
+position);
这行代码执行了吗?
######
"ywspace"
,
""
+position);
这行代码执行了吗?
"ywspace"
,
""
+position);
这行代码执行了吗?
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。