我使用来自 sdcard 位置源的图像标题进行了自定义网格视图。我的整个项目都放在下面。如果它对任何人都有帮助,请投票给我以提高声誉。
public class MainActivity extends Activity {
private File file;
LinearLayout linear;
ArrayList<Bitmap> listbitmap;
String[] folderlist ;
ExpandableHeightGridView gridview;
LinearLayout linearhere;
String imageInSD = Environment.getExternalStorageDirectory().getAbsolutePath() +"/Hi";
ArrayList<String> mylist = new ArrayList<String>();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linear = (LinearLayout) findViewById(R.id.linear);
File imagesSource = new File(imageInSD);
File[] dictoryFiles = imagesSource.listFiles();
for(int i =0; i<dictoryFiles.length;i++){
mylist.add(dictoryFiles[i].getName());
}
folderlist = mylist.toArray(new String[mylist.size()]);
linearhere = new LinearLayout(getApplicationContext());
linearhere.setOrientation(LinearLayout.VERTICAL);
for (int k = 0; k < folderlist.length; k++) {
TextView textview = new TextView(getApplicationContext());
textview.setTextColor(Color.BLACK);
textview.setTextSize(20);
textview.setText(folderlist[k]);
gridview = new ExpandableHeightGridView(getApplicationContext());
gridview.setId(Calendar.SECOND);
gridview.setLayoutParams(new GridView.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
gridview.setBackgroundColor(Color.WHITE);
gridview.setNumColumns(3);
gridview.setColumnWidth(GridView.AUTO_FIT);
gridview.setVerticalSpacing(5);
gridview.setHorizontalSpacing(5);
gridview.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
listbitmap = new ArrayList<Bitmap>();
file = new File(imageInSD + "/" + folderlist[k]);
File list[] = file.listFiles();
for (int i = 0; i < list.length; i++) {
String sp=list[i].getName();
//if(sp.startsWith("123")){
//textview.setText(folderlist[k]);
File image = new File(imageInSD + "/"+folderlist[k]+"/" + list[i].getName());
InputStream fis = null;
try {
fis = new FileInputStream(image.getAbsolutePath());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
Bitmap bm = BitmapFactory.decodeStream(fis, null, options);
listbitmap.add(bm);
//}
}
gridview.setAdapter(new CustomGridAdapter(getApplicationContext(),
listbitmap));
gridview.setExpanded(true);
linearhere.addView(textview);
linearhere.addView(gridview);
}
linear.addView(linearhere);
}
}
======================
class2
======================
public class ExpandableHeightGridView extends GridView
{
boolean expanded = false;
public ExpandableHeightGridView(Context context)
{
super(context);
}
public ExpandableHeightGridView(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public ExpandableHeightGridView(Context context, AttributeSet attrs,
int defStyle)
{
super(context, attrs, defStyle);
}
public boolean isExpanded()
{
return expanded;
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
// HACK! TAKE THAT ANDROID!
if (isExpanded())
{
// Calculate entire height by providing a very large height hint.
// View.MEASURED_SIZE_MASK represents the largest height possible.
int expandSpec = MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
}
else
{
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
public void setExpanded(boolean expanded)
{
this.expanded = expanded;
}
}
======================
class 3:
======================
public class CustomGridAdapter extends BaseAdapter {
private Context context;
private final ArrayList<Bitmap> gridValues;
// Constructor to initialize values
public CustomGridAdapter(Context context, ArrayList<Bitmap> gridValues) {
this.context = context;
this.gridValues = gridValues;
}
@Override
public int getCount() {
// Number of times getView method call depends upon gridValues.length
return gridValues.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int id) {
return id;
}
// Number of times getView method call depends upon gridValues.length
public View getView(int position, View convertView, ViewGroup parent) {
// LayoutInflator to call external grid_item.xml file
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
gridView = new View(context);
// get layout from grid_item.xml ( Defined Below )
gridView = inflater.inflate(R.layout.grid_item, null);
ImageView imageView = (ImageView) gridView
.findViewById(R.id.grid_item_image);
imageView.setImageBitmap(gridValues.get(position));
} else {
gridView = (View) convertView;
}
return gridView;
}
}
=========================
activity_main.xml Code:
=========================
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="@+id/linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#D8D8D8"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
==========================
grid_item.xml code:
==========================
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp" >
<ImageView
android:id="@+id/grid_item_image"
android:layout_width="180dp"
android:layout_margin="5dp"
android:layout_height="150dp"
>
</ImageView>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="0dp" >
</LinearLayout>
</LinearLayout>