我正在关注这个https://github.com/madhu314/sectionedgridview分区的gridview。 在data_item.xml中,我有复选框而不是imagebutton。我想检查多个项目,然后将检查的项目发送到另一个活动。 我面临的问题是,当我向上或向下滚动列表视图时,选中的复选框将被取消选中并选中不同的复选框。 我需要帮助。任何帮助将不胜感激。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = null;
boolean isSectionheader = isSectionHeader(position);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (isSectionheader) {
v = inflater.inflate(R.layout.section_header, null);
} else {
LinearLayout ll = (LinearLayout) inflater.inflate(
R.layout.list_row, null);
v = ll;
ll = (LinearLayout) ll.findViewById(R.id.row_item);
// add childrenCount to this
for (int i = 0; i < numberOfChildrenInRow; i++) {
// add a child
View child = inflater.inflate(R.layout.data_item, null);
ll.addView(child, new LinearLayout.LayoutParams(
gridItemSize, gridItemSize));
if (i < numberOfChildrenInRow - 1) {
// now add space view
View spaceItem = new View(mContext);
ll.addView(spaceItem, new LinearLayout.LayoutParams(
childrenSpacing[i], ll.getHeight()));
}
}
}
} else {
v = convertView;
}
String sectionName = whichSection(position);
if (isSectionheader) {
TextView tv = (TextView) v;
tv.setText(sectionName);
} else {
LinearLayout ll = (LinearLayout) v;
LinearLayout rowPanel = (LinearLayout) ll.findViewById(R.id.row_item);
View divider = ll.findViewById(R.id.row_item_divider);
divider.setVisibility(View.VISIBLE);
// check if this position corresponds to last row
boolean isLastRowInSection = isLastRowInSection(position);
int positionInSection = positionInSection(position);
Cursor c = sectionCursors.get(sectionName);
// --
int cursorStartAt = numberOfChildrenInRow * positionInSection;
// set all children visible first
for (int i = 0; i < 2 * numberOfChildrenInRow - 1; i++) {
// we need to hide grid item and gap
View child = rowPanel.getChildAt(i);
child.setVisibility(View.VISIBLE);
// leave alternate
if (i % 2 == 0) {
// its not gap
if (c.moveToPosition(cursorStartAt)) {
String dataName = c.getString(0);
TextView tv = (TextView) child
.findViewById(R.id.data_item_text);
tv.setText(dataName);
}
// set listener on image button
checkbox = (CheckBox) child
.findViewById(R.id.data_item_image);
ButtonViewHolder holder = new ButtonViewHolder();
holder.sectionName = sectionName;
holder.positionInSection = cursorStartAt;
holder.parent = child;
holder.position = position;
checkbox.setTag(holder);
checkbox.setOnCheckedChangeListener(this);
cursorStartAt++;
}
}
checkbox.setChecked(mSparseBooleanArray.get(position));
if (isLastRowInSection) {
divider.setVisibility(View.INVISIBLE);
// check how many items needs to be hidden in last row
int sectionCount = sectionCursors.get(sectionName).getCount();
int childrenInLastRow = sectionCount % numberOfChildrenInRow;
if (childrenInLastRow > 0) {
int gaps = childrenInLastRow - 1;
for (int i = childrenInLastRow + gaps; i < rowPanel
.getChildCount(); i++) {
// we need to hide grid item and gap
View child = rowPanel.getChildAt(i);
child.setVisibility(View.INVISIBLE);
}
}
}
}
return v;
}
答案 0 :(得分:0)
根据您的问题,您需要选择多个图像,之后您需要执行操作(例如应对所选图像,删除/上传等)。因此,您需要保持选择图像位置以及您需要更改选择颜色..
为此,我只是创建了一个适配器,它具有多个图像选择和各种方法来获取所选图像并选择全部/ de选择所有选项..在此我控制所有可能性这样认为..如果它是同样的意思是,改变你的适配器类..
这是我的适配器类:
public class LocalFilesUploadAdapter extends BaseAdapter
{
private final HashMap<Integer, Boolean> myChecked = new HashMap<Integer, Boolean>();
private final Activity _activity;
private ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>();
ArrayList<String> listPaths;
ArrayList<String> listNames;
private LayoutInflater inflater = null;
public LocalFilesUploadAdapter( Context context,
Activity activity,
ArrayList<String> filePaths,
ArrayList<String> fileNames,
ArrayList<Bitmap> bitmaps )
{
this._activity = activity;
this.listPaths = filePaths;
this.listNames = fileNames;
this.bitmaps = bitmaps;
inflater = LayoutInflater.from( context );
for( int i = 0; i < filePaths.size(); i++ )
{
myChecked.put( i,
false );
}
}
public void toggleChecked( int position )
{
if( myChecked.get( position ) )
{
myChecked.put( position,
false );
}
else
{
myChecked.put( position,
true );
}
notifyDataSetChanged();
}
public void selectAllItems( int position )
{
myChecked.put( position,
true );
notifyDataSetChanged();
}
public void deSelectAllItems( int position )
{
myChecked.put( position,
false );
notifyDataSetChanged();
}
public List<Integer> getCheckedItemPositions()
{
List<Integer> checkedItemPositions = new ArrayList<Integer>();
for( int i = 0; i < myChecked.size(); i++ )
{
if( myChecked.get( i ) )
{
( checkedItemPositions ).add( i );
}
}
return checkedItemPositions;
}
public ArrayList<String> getCheckedItems()
{
ArrayList<String> checkedItems = new ArrayList<String>();
// ArrayList<HashMap<String, String>> checkedItems = new ArrayList<HashMap<String, String>>();
for( int i = 0; i < myChecked.size(); i++ )
{
if( myChecked.get( i ) )
{
String pathName = listPaths.get( i );
( checkedItems ).add( pathName );
}
}
return checkedItems;
}
public ArrayList<String> getCheckedItemNames()
{
ArrayList<String> checkedItems = new ArrayList<String>();
// ArrayList<HashMap<String, String>> checkedItems = new ArrayList<HashMap<String, String>>();
for( int i = 0; i < myChecked.size(); i++ )
{
if( myChecked.get( i ) )
{
String imageName = listNames.get( i );
( checkedItems ).add( imageName );
}
}
return checkedItems;
}
public boolean selectAll()
{
for( int i = 0; i < listPaths.size(); i++ )
{
selectAllItems( i );
}
return false;
}
public boolean deSelectAll()
{
for( int i = 0; i < listPaths.size(); i++ )
{
deSelectAllItems( i );
}
return false;
}
@Override
public int getCount()
{
// TODO Auto-generated method stub
if( 0 == listPaths.size() )
{
return 0;
}
return this.listPaths.size();
}
@Override
public Object getItem( int position )
{
// TODO Auto-generated method stub
return this.listPaths.get( position );
}
@Override
public long getItemId( int position )
{
// TODO Auto-generated method stub
return position;
}
@Override
public View getView( final int position,
View convertView,
ViewGroup parent )
{
View row = convertView;
if( row == null )
{
row = inflater.inflate( R.layout.gridview_item,
null );
}
Log.d( "debug",
"position" + position );
ImageView imageView = (ImageView) row.findViewById( R.id.grid_image );
CheckBox checkbox = (CheckBox) row.findViewById( R.id.img_checkBox );
imageView.setImageBitmap( bitmaps.get( position ) );
checkbox.setOnClickListener( new OnClickListener()
{
@Override
public void onClick( View v )
{
// TODO Auto-generated method stub
toggleChecked( position );
}
} );
Boolean checked = myChecked.get( position );
if( checked != null )
{
checkbox.setChecked( checked );
}
imageView.setOnClickListener( new OnImageClickListener( position ) );
return row;
}
class OnImageClickListener implements OnClickListener
{
int _postion;
// constructor
public OnImageClickListener( int position )
{
this._postion = position;
}
@Override
public void onClick( View v )
{
// on selecting grid view image
// launch full screen activity
toggleChecked( _postion );
}
}
}
希望它能为你提供帮助