Trying to highlight a selected item from a GridView

≯℡__Kan透↙ 提交于 2019-12-12 05:22:49

问题


I have a Gridview and when i press a certain element, i want paint the background.

I have an adapter to load the gridview elements dinamically. And i have a listener on that adapter. On that listener, i put the background with the color that i want, but then it also paints another element down the list (i guess the one with the same position, after the view is reloaded).

Important : My minimum API is 9, and i can't really change it.

Here is the code for my getView method (from the adapter) :

   @Override
public View getView(int position, View convertView, ViewGroup parent) {

    viewHolder = null;
    if (convertView == null) {

        viewHolder=new ViewHolder();

        LayoutInflater layoutInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view=layoutInflater.inflate(R.layout.highwayappselectionitem,parent, false);

        viewHolder.appImageIcon=(ImageView) view.findViewById(R.id.highwayGridViewItemIcon);
        viewHolder.appNameLabel=(TextView) view.findViewById(R.id.highwayGridViewItemName);
        viewHolder.appInfoButton = (ImageView) view.findViewById(R.id.highwayGridViewItemInfoButton);
        viewHolder.linearLayout = (LinearLayout) view.findViewById(R.id.HighwayGridViewItem);

    }else{
        view= convertView;
        viewHolder = (ViewHolder) convertView.getTag();
    }


    viewHolder.appNameLabel.setText(appsList.get(position).getAppName());
    viewHolder.appImageIcon.setImageDrawable(appsList.get(position).getImageIcon());


    view.setTag(viewHolder);

    viewHolder.linearLayout.setOnClickListener(new MyOnClickListener(position,0));

    return view;
}

and here is the listener for the linearlayout (that represents each item):

public void onClick(View v) {

        if(selectedView==0 || selectedView==1){

            appName=appsList.get(position).getAppName();
            filePath=appsList.get(position).getFilePath();
            appLogo=appsList.get(position).getImageIcon();

           v.setBackgroundColor(activity.getResources().getColor(R.color.light_grey));           

            System.out.println("Just pressed the app ... "+appName);
            System.out.println("His filePath is ... "+filePath);

            HighwayGridViewAppItem tmpSelectedItem= new HighwayGridViewAppItem(appLogo, appName, filePath);
            selectedAppsList.add(tmpSelectedItem);

            System.out.println("AppSelectionCUstomAdapter : Added a new application to the list of selected apps .!!!!!!!!!!!!!!!.........");
            System.out.println("AppselectioncustomAdaptaer  :   The size of the selectedAppsList is now  : "+selectedAppsList.size());

        }else{
            //pressed the info button
            System.out.println("This will be implemented later, need access to the webservices");
        }
    }
}

ignore the if(selected view ==0 or ==1 it is not being used anymore, i can delete it... ).

Also, appsLIst is a list my custom class of items. There i have a boolean attribute to represent if it is selected or not.

WHat is happening is , imagine this gridview:

A B C

D E F


G H I

J K L

The grey line represents the scroll... if scroll those new elements show up. If i press B, it selects B, but it also selects H.

I tried to do research but the problems that I found weren't the same.

Something that might help.. the gridview used to add repeated elemetns, so when i add the elements to the gridview i check if the list of elements already has a similar element, if so i dont add it.


回答1:


You need to add this line when you want to keep selected some item of gridview after selection.

((BaseAdapter) gridView.getAdapter()).notifyDataSetChanged();

Edit I think there is problem in getView method that's why you are getting duplicate values:

public View getView(int position, View convertView, ViewGroup parent) {
   View view = convertView;
    if (view== null) {  // if it's not recycled, initialize some attributes
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(     Context.LAYOUT_INFLATER_SERVICE );
        view = inflater.inflate(R.layout.highwayappselectionitem, parent, false);
    } 

}



来源:https://stackoverflow.com/questions/39224237/trying-to-highlight-a-selected-item-from-a-gridview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!