Reputation: 323
My layout has a GridView. Similar to Google’s API demo, each grid has an image. Also, I customized a “Drawable image” which used to highlight the currently selected item. When touch the grid’s image, the “Drawable image” can be show. Unfortunately, it will disappear when I not touch the grid.
I hope the GridView will keep to highlight the selected item. Even the user is scrolling the screen.
From here, I found some similar post.
1) Keep image in GridView selected
2) Android gridview keep item selected
But I can’t understand their method and they are not using customized image. Can anyone give me some help? Please.
Following is my code:
gridAdapter adapter = new gridAdapter (this, images);
gv = (GridView) findViewById (R.id.gridView1);
gv.setAdapter(adapter);
gv.setSelector(R.drawable.circle);
gv.setDrawSelectorOnTop(true);
Following is my updated code:
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;
}
}
}
Upvotes: 2
Views: 14592
Reputation: 10586
This is how I achieved this
<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>
and in Adapter add one line extra
GridView gvCalender = (GridView)findViewById(R.id.gv_calender);
CalendarAdapter adapter = new CalendarAdapter(getActivity(), lsCalender);
gvCalender.setDrawSelectorOnTop(false);
gvCalender.setAdapter(adapter);
That's It you are done
Key lines are
android:listSelector="@color/colorAccent"
AND
gvCalender.setDrawSelectorOnTop(false);
Upvotes: 0
Reputation: 11
Set onItemClickListener on gridview .You can then get position of view onItemClick . pass the selected postion to adapter and within adapter inside getView method check if(position == selected_position) . if true set background of your choice .
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));
}
Upvotes: 0
Reputation: 272
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;
}
}
Upvotes: 0
Reputation: 6618
Ok now it needs a whole new answer...
here's the thing: setSelected()
will set the calling view as selected, thus subsequently giving focus....
calling
GridView gv; gv.setSelected(true);
will select the GridView gv, not the View in gridview's adapter which you want.
So you need to call gv.setSelection(pos)
to select a specific child.
the pos
you are saving might not be the real position but rather the position from 'visible' offset. that's why you have to call
(gridView.getAdapter()).getPosition(viewThatYouWant);
or
(gridView.getAdapter()).getItemAt(lastPos).requestFocus();
Do note that you should look into HOW A VIEW HANDLES IT'S FOCUS VS CHILD FOCUS the method is called
setDescendantFocusability
so naturally you would want
GridView gv; gv.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
Upvotes: 2