gpt4 book ai didi

android - 如何在选择时突出显示 GridView 项目?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:54:23 25 4
gpt4 key购买 nike

我正在创建一个带有图像和文本的 GridView 布局。我想要多选项目,但我想突出显示所选的网格项目。

这是我的代码:

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"};


}
}

我应该在代码中添加什么来突出显示将被选中的网格项目。

请建议我一些方法来做到这一点。提前致谢。

最佳答案

  1. 创建 ImageAdapter 类
  2. 将这个参数添加到你的类中 private int selectedPosition = -1
  3. 将此方法添加到您的 ImageAdapter 类中

    public void setSelectedPosition(int position) {
    selectedPosition = position;
    }
  4. 将这些行添加到 ImageAdapter 类的 getView 方法的末尾

    if (position == selectedPosition) {
    gridView.setBackgroundColor(Color.BLACK);
    } else {
    gridView.setBackgroundColor(Color.TRANSPARENT);
    }
  5. 在您的 Activity (或 fragment )中从 ImageAdapter 类创建对象
  6. 然后只需添加这些行进行测试

    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();
    }
    });

关于android - 如何在选择时突出显示 GridView 项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19292369/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com