グリッドビューを作成し、チェックボックスを追加しました。チェックボックスグリッドビューアイテムをクリックすると、テキストビューを含む画像ビューが選択されます。
しかし、グリッドビューアイテムのタップでチェックボックスをオンにしたいです。
これは私の MainActivity
public class MainActivity extends Activity {
GridView gridview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridview = (GridView) findViewById(R.id.mainGrid);
gridview.setAdapter(new ImageAdapter(MainActivity.this));
gridview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int position,
long id) {
// TODO Auto-generated method stub
//
//
ImageAdapter.holder.ItemCheck.setId(position);
ImageAdapter.holder.ItemCheck.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
//ImageAdapter.holder.ItemCheck.setChecked(true);
Toast.makeText(MainActivity.this, "Itemclicked" ,Toast.LENGTH_LONG).show();
}
});
Toast.makeText(
MainActivity.this, "Itemclicked", Toast.LENGTH_SHORT).show();
}
});
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
これは私の AdapterClass です
public class ImageAdapter extends BaseAdapter{
private Context mContext;
public static ViewHolder holder;
public ImageAdapter(Context c) {
mContext = c;
}
@Override
public int getCount() {
return mThumbIds.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
public boolean areAllItemsEnabled(){
return true;
}
@Override
public boolean isEnabled(int position) {
// TODO Auto-generated method stub
return true;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
//ViewHolder holder;
View myView = convertView;
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
//Inflate the layout
holder = new ViewHolder();
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8,8, 8);
LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
myView = inflater.inflate(R.layout.grid_items_ontap, null);
holder.ItemCheck = (CheckBox) myView.findViewById(R.id.itemCheckBox);
myView.setTag(holder);
}else{
holder = (ViewHolder) myView.getTag();
}
// holder.ItemCheck.setId(position);
// holder.ItemCheck.setOnClickListener(new OnClickListener() {
//
// public void onClick(View v) {
// // TODO Auto-generated method stub
// Toast.makeText(mContext, "Itemclicked" ,Toast.LENGTH_LONG).show();
// }
// });
// 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] );
holder.ItemCheck.setChecked(isEmpty());
holder.id = position;
return myView;
}
public class ViewHolder {
public CheckBox ItemCheck;
int id;
}
private String[] names={"ab","cd","ef","gh","ij","kl","mn","","","","","","",""};
private Integer[] mThumbIds = {
R.drawable.car, R.drawable.car,
R.drawable.car, R.drawable.car,
R.drawable.car,R.drawable.car,R.drawable.car,R.drawable.car, R.drawable.car,
R.drawable.car, R.drawable.car,
R.drawable.car,R.drawable.car,R.drawable.car
};
}
Gridview.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/gridlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<GridView
android:id="@+id/mainGrid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:horizontalSpacing="0dp"
android:numColumns="3"
android:clickable="true"
android:stretchMode="columnWidth"
android:verticalSpacing="0dp" >
</GridView>
</LinearLayout>
griditems.xml ファイル
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/GridItem"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:orientation="vertical" >
<ImageView
android:id="@+id/grid_item_image_OnTap"
android:layout_width="100dp"
android:layout_height="100dp" >
</ImageView>
<TextView
android:id="@+id/grid_item_text_onTap"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center_horizontal"
android:text="TextView"
android:textColor="@android:color/black" >
</TextView>
<CheckBox
android:id="@+id/itemCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:button="@drawable/custom_checkbox"
android:checked="true"
android:visibility="visible"
android:focusable="true" />
</RelativeLayout>
私に提案してください.私はアンドロイドが初めてで、ここで立ち往生しました.
ありがとう