开发者社区> 问答> 正文

GridView 设置项监听事件 怎么无效?:报错

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>

各位大牛 帮忙看看 那里出问题了?

展开
收起
kun坤 2020-06-14 06:58:56 471 0
1 条回答
写回答
取消 提交回答
  • Log.i("ywspace",""+position);

    这行代码执行了吗?

    ######

    引用来自“鉴客”的答案

    Log.i("ywspace",""+position);

    这行代码执行了吗?

    没有噢
    ######

    引用来自“鉴客”的答案

    Log.i("ywspace",""+position);

    这行代码执行了吗?

    很是不解,在这里随便设置其他UI控件 的监听事件 都好好的  就这个GridView  不起效。
    2020-06-14 06:59:04
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载