GridView将继续突出显示所选项目

时间:2012-10-16 12:25:58

标签: android gridview

我的布局有一个GridView。与Google的API演示类似,每个网格都有一个图像。此外,我定制了一个“可绘制图像”,用于突出显示当前所选项目。触摸网格图像时,可以显示“可绘制图像”。不幸的是,当我没有触摸网格时它会消失。

我希望GridView能够突出显示所选项目。甚至用户也在滚动屏幕。

从这里,我发现了一些类似的帖子。

1)Keep image in GridView selected

2)Android gridview keep item selected

但我无法理解他们的方法,他们没有使用自定义图像。 谁能给我一些帮助?请。

以下是我的代码:

    gridAdapter  adapter =  new   gridAdapter  (this, images);
    gv = (GridView) findViewById (R.id.gridView1);
    gv.setAdapter(adapter);
    gv.setSelector(R.drawable.circle);
    gv.setDrawSelectorOnTop(true);

以下是我更新的代码:

public class GridViewBasic extends Activity {
    Integer[] imageIDs = {
            R.drawable.ic_launcher,
            R.drawable.ic_launcher,
            R.drawable.ic_launcher,
            R.drawable.ic_launcher,
            R.drawable.ic_launcher,
            R.drawable.ic_launcher,
            R.drawable.ic_launcher,
            R.drawable.ic_launcher,
            R.drawable.ic_launcher,
            R.drawable.ic_launcher,
            R.drawable.ic_launcher,
            R.drawable.ic_launcher,
            R.drawable.ic_launcher,
            R.drawable.ic_launcher,
            R.drawable.ic_launcher    };
    GridView gridView;
    public int lastPos;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
            gridView = (GridView) findViewById(R.id.gridView1);
            gridView.setAdapter(new ImageAdapter(this));
            gridView.setSelected(true);
            gridView.setSelector(R.drawable.circle);
            gridView.setDrawSelectorOnTop(true);
            gridView.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);

            gridView.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent,  View v, int position, long id) {   
                    lastPos = position;
                    gridView.setSelection((int)(gridView.getAdapter()).getItemId(lastPos));
                    System.out.println ("getItemId ="+(gridView.getAdapter()).getItemId(lastPos));

                }
            });        
        }

        public class ImageAdapter extends BaseAdapter 
        {
            private Context context;

            public ImageAdapter(Context c)  {
                context = c;
            }

            //---returns the number of images---
            public int getCount() {
                return imageIDs.length;
            }

            //---returns the ID of an item--- 
            public Object getItem(int position) {
                return position;
            }

            public long getItemId(int position) {
                return position;
            }

            //---returns an ImageView view---
            public View getView(int position, View convertView, ViewGroup parent) {
                ImageView imageView;

                if (convertView == null) {
                    imageView = new ImageView(context);
                    imageView.setLayoutParams(new GridView.LayoutParams(185, 185));
                    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                    imageView.setPadding(5, 5, 5, 5);
                } else {
                    imageView = (ImageView) convertView;
                }
                imageView.setImageResource(imageIDs[position]);

                return imageView;
            }
        }    


}

5 个答案:

答案 0 :(得分:2)

好了,现在需要一个全新的答案......

这就是事情:setSelected()会将调用视图设置为选中状态,从而随后给予关注....

调用

 GridView gv; gv.setSelected(true);

将选择GridView gv,而不是您想要的gridview适配器中的View。

因此,您需要致电gv.setSelection(pos)以选择特定的孩子。

你要保存的pos可能不是真正的位置,而是来自“可见”偏移的位置。这就是你必须打电话的原因

(gridView.getAdapter()).getPosition(viewThatYouWant);

(gridView.getAdapter()).getItemAt(lastPos).requestFocus();

请注意你应该看一下如何看待它的焦点与儿童的焦点 该方法称为

setDescendantFocusability

很自然地你想要

GridView gv; gv.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);

答案 1 :(得分:0)

public class StaffSelectionAdapter extends BaseAdapter {

private Context context;

private final String[] staffMemberList;
private int previousPosition = -1;
private static boolean isFirstTime;
boolean[] isChecked;
private TextView textView = null;
public long staffId;
private LayoutInflater inflater;


public StaffSelectionAdapter(Context context, String[] staffMemberList) {
    this.context = context;
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    this.staffMemberList = staffMemberList;

    isFirstTime = true;
    isChecked = new boolean[staffMemberList.length];
    for (int i = 0; i < staffMemberList.length; i++) {
        isChecked[i] = false;
    }
}

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

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.row_for_staff_selection, null);
    }
    textView = (TextView) convertView.findViewById(R.id.row_for_staff_selection_grid_item_label);
    textView.setText(staffMemberList[position]);

    if (isChecked[position]) {
        SpannableString content = new SpannableString(staffMemberList[position]);
        content.setSpan(new UnderlineSpan(), 0, staffMemberList[position].length(), 0);
        textView.setText(content);
    } else {
        textView.setBackgroundColor(context.getResources().getColor(R.drawable.dialog_bg));
    }

    convertView.setTag(position);
    convertView.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            if (!isChecked[position]) {
                staffId = 

                isChecked[position] = true;
                if (!isFirstTime) {
                    isChecked[previousPosition] = false;
                } else {
                    isFirstTime = false;
                }
                textView.setBackgroundColor(context.getResources().getColor(R.drawable.dialog_bg));
            }
            previousPosition = (Integer) v.getTag();
            notifyDataSetChanged();
        }
    });
    return convertView;
}

public int getCount() 
    {
    return staffMemberList.length;
}

public Object getItem(int position) 
    {
    return staffMemberList[position];
}

public long getItemId(int position) {
    return 0;
}

public long getStaffId() {
    return staffId;
}

public void setStaffId(long staffId) {
    this.staffId = staffId;
}

}

答案 2 :(得分:0)

在gridview上设置onItemClickListener。然后,您可以获取onItemClick的视图位置。将选定的位置传递给适配器,并在getView方法内的适配器内检查if(position == selected_position)。如果你选择了真实的背景。

public ColorGridAdapter(Context context,ArrayList<String> colorArray,int position) {
                super(context, resource,colorArray);            
                this.context = context;
                this.colors = colorArray;
                this.selected_position = position;  // position of your selected item

            }

    public View getView(final int position, View convertView, ViewGroup parent){
                View v = convertView;
                if (v == null) {

    LayoutInflater inflater = (LayoutInflater)  getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                        v = inflater.inflate(R.layout.color_pelatte, null);
                        ColorView = (View)v.findViewById(R.id.color_view);          
                    }

                ColorView.setBackgroundColor(Color.parseColor(colors.get(position)));
                if(position == selected_position){
                    v.setBackground(context.getResources().getDrawable(R.drawable.edittext_background));
                }

答案 3 :(得分:0)

这就是我实现这个目标的方式

 <GridView
    android:id="@+id/gv_calender"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:numColumns="7"
    android:listSelector="@color/colorAccent"
    android:stretchMode="columnWidth"></GridView>

并在适配器中添加一行额外的

  GridView gvCalender = (GridView)findViewById(R.id.gv_calender);
  CalendarAdapter adapter = new CalendarAdapter(getActivity(), lsCalender);
    gvCalender.setDrawSelectorOnTop(false);
    gvCalender.setAdapter(adapter);

那就是你完成了 关键行是

<强>机器人:listSelector =&#34; @颜色/ colorAccent&#34;
    和
 的 gvCalender.setDrawSelectorOnTop(假);

答案 4 :(得分:-1)

你有答案吗?如果不是这是间接的方式: (这不是编码的好方法..但这是一个成功的选择)

1)在gridview的焦点发生变化之前,你应该把所选的项目带到一个数组中。(使用下面的代码)

 public void selected_items()
     {
         checkedItems = gridView.getCheckedItemPositions();
         selected=new String[checkedItems.size()];
         if (checkedItems != null) {
             for (int i=0; i<checkedItems.size(); i++) {
                 if (checkedItems.valueAt(i)) {
                     String x = gridView.getAdapter().getItem(   checkedItems.keyAt(i)).toString();
                     selected[i]=x;
                 }
             }
         }
     }

2)然后在gridview获得焦点时再次检查这些项目。