我正在创建一个带有图像和文本的网格视图布局。我想要多项选择工作正常,但我想突出显示所选的网格项。
这是我的代码:
public class FragMent1 extends Fragment{
BaseAdapter MyAdapter;
private Context mContext;
@SuppressLint("ValidFragment")
public FragMent1(Context c) {
mContext = c;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//View view = inflater.inflate(R.layout.g, null);
View view = inflater.inflate(R.layout.gridview,null);
final GridView listView = (GridView) view.findViewById(R.id.mainGrid);
listView.setAdapter(new Adapter());
//listView.setSelection(1);
listView.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE_MODAL);
listView.setMultiChoiceModeListener(new MultiChoiceModeListener() {
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
// TODO Auto-generated method stub
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
mode.setTitle("Select Items");
mode.setSubtitle("One item selected");
return true;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
// TODO Auto-generated method stub
int selectCount = listView.getCheckedItemCount();
switch (selectCount) {
case 1:
mode.setSubtitle("One item selected");
break;
default:
mode.setSubtitle("" + selectCount + " items selected");
break;
}
return true;
}
@Override
public void onItemCheckedStateChanged(ActionMode mode, int position,
long id, boolean checked) {
// TODO Auto-generated method stub
int selectCount = listView.getCheckedItemCount();
switch (selectCount) {
case 1:
mode.setSubtitle("One item selected");
break;
default:
mode.setSubtitle("" + selectCount + " items selected");
break;
}
}
});
return view;
}
private class Adapter extends BaseAdapter {
@Override
public int getCount() {
return mThumbIds.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View myView = convertView;
ImageView image;
if (convertView == null) {
image = new ImageView(FragMent1.this.getActivity());
image.setLayoutParams(new GridView.LayoutParams(85, 85));
image.setScaleType(ImageView.ScaleType.CENTER_CROP);
LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
myView = inflater.inflate(R.layout.grid_items_ontap, null);
// Add The Image!!!
ImageView iv = (ImageView)myView.findViewById(R.id.grid_item_image_OnTap);
iv.setImageResource(mThumbIds[position]);
// Add The Text!!!
TextView tv = (TextView)myView.findViewById(R.id.grid_item_text_onTap);
tv.setText(names[position] );
}
return myView;
}
private Integer[] mThumbIds = {
R.drawable.car, R.drawable.car,
R.drawable.car, R.drawable.car,
R.drawable.car,R.drawable.car,R.drawable.car
};
private String[] names={"ab","cd","ef","gh","ij","kl","mn"};
}
}
我应该在代码中添加什么来突出显示将被选中的网格项目。
请建议我做一些方法。 提前谢谢。
答案 0 :(得分:9)
您可以使用选择器突出显示项目
在drawable文件夹中创建一个xml文件
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/blue" android:state_selected="true"/>
<item android:drawable="@color/transparent"/>
</selector>
并设置gridview的listSelector,如
机器人:listSelector = “@绘制/ list_selector”
答案 1 :(得分:6)
private int selectedPosition = -1
将此方法添加到ImageAdapter类
public void setSelectedPosition(int position) {
selectedPosition = position;
}
将这些行添加到ImageAdapter类的getView方法的末尾
if (position == selectedPosition) {
gridView.setBackgroundColor(Color.BLACK);
} else {
gridView.setBackgroundColor(Color.TRANSPARENT);
}
然后只需将这些行添加到测试
GridView gridView = (GridView) view.findViewById(R.id.gridview);
gridView.setAdapter(adapterImage);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
adapterImage.setSelectedPosition(position);
adapterImage.notifyDataSetChanged();
}
});
答案 2 :(得分:2)
如果你的网格视图在相对布局中,你可以在那里设置触摸监听器,通过使用自定义绘图来突出显示所选项目(这里我使用九个补丁图像来指定选择)
private RelativeLayout.OnTouchListener touchListener = new RelativeLayout.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if(touchEnabled==false)
return false;
try {
if (oldView != null) {
oldView.setBackgroundResource(R.color.transparent);
}
setSelection(((ViewHolder)v.getTag()).pos);
v.setBackgroundResource(R.drawable.list_pressed);
oldView = v;
} catch (Exception e) {
}
return false;
}
};
或者您也可以使用选择器
在drawable文件夹中创建一个xml文件
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/blue" android:state_selected="true"/>
<item android:drawable="@color/transparent"/>
</selector>
并设置gridview的listSelector,如
机器人:listSelector = “@绘制/ list_selector”
答案 3 :(得分:0)
我在setTag
上用childItems
解决了这个问题
这是我的示例代码
yourGridView.setOnItemClickListener((parent1, view, position1, id1) -> {
if (view.getTag()!=null && view.getTag().equals("Examplename")){
view.setBackgroundColor(yourColor);
view.setTag("");
}else{
view.setTag("Examplename");
view.setBackgroundColor(Color.BLUE);
int Nums=gridListItems.getChildCount();
for (int i=0; i<=Nums-1; i++ ){
if (i!=position1){
View child= gridListItems.getChildAt(i);
child.setBackgroundColor(yourColor);
}
}
}
});