我有一个目录,我在其中有许多文件夹,我想在活动中GridView
显示。
我已经提取了文件夹和标题的路径,但是如何在GridView
中实现它的绘制空白。
我的代码是String[] albumNameOnly=thisIntent.getStringArrayExtra("AlbumString");
gridView = (GridView) findViewById(R.id.grid_view);
columnWidth = getScreenWidth();
InitilizeGridLayout(columnWidth);
//The initialize grid is shown below.
MyAlbumAdapter myAlbumAdapter;
Log.e("Starting the Adapter","+++");
myAlbumAdapter = new MyAlbumAdapter(getApplicationContext(), columnWidth,albumNameOnly);
gridView.setAdapter(myAlbumAdapter);
}
public int getScreenWidth() {
int columnWidth;
WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
final Point point = new Point();
try {
display.getSize(point);
} catch (NoSuchMethodError ignore) { // Older device
point.x = display.getWidth();
point.y = display.getHeight();
}
columnWidth = point.x;
return columnWidth;
}
private void InitilizeGridLayout(int screenWidth) {
Resources r = getResources();
float padding = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
GRID_PADDING, r.getDisplayMetrics());
columnWidth = (int) ((screenWidth - ((NUM_OF_COLUMNS + 1) * padding)) / NUM_OF_COLUMNS);
gridView.setNumColumns(NUM_OF_COLUMNS);
gridView.setColumnWidth(columnWidth);
gridView.setStretchMode(GridView.NO_STRETCH);
gridView.setPadding((int) padding, (int) padding, (int) padding,
(int) padding);
gridView.setHorizontalSpacing((int) padding);
gridView.setVerticalSpacing((int) padding);
}
实现的。
AlbumAdapter
我的GridView
在下面。
现在我只想在setResource
中找到它们,这就是为什么我将ImageView
public MyAlbumAdapter(Context context, int columnWidth, String[] albumNameOnly) {
this.context = context;
Log.e("My Adapter Called", "++++");
this.columnWidth = columnWidth;
this.albumNameOnly = albumNameOnly;
}
@Override
public int getCount() {
Log.e("Size of Directory", "++ " + albumNameOnly.length);
return MainActivity.directoryPath.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View myGrid;
final ImageView imageView;
final TextView textView;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
myGrid = new View(context);
myGrid = inflater.inflate(R.layout.album_grid_activity_layout, null);
imageView = (ImageView) myGrid.findViewById(R.id.no_image_placeholder);
textView = (TextView) myGrid.findViewById(R.id.album_title_main);
// myImageView.setLayoutParams(new GridView.LayoutParams(200, 200));
// myImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
// myImageView.setPadding(1, 1, 1, 1);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(columnWidth, columnWidth));
imageView.setImageResource(R.drawable.no_image);
Log.e("++++", "AlbuM Name to be displayed" + albumNameOnly[position]);
textView.setText(albumNameOnly[position]);
} else {
myGrid = (View) convertView.getTag();
}
return myGrid;
}
设置为可绘制的原因。
GridView
这是我的<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/grid_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#fffff">
</GridView>
布局。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/no_image_placeholder"/>
<TextView
android:layout_width="match_parent"
android:id="@+id/album_title_main"
android:layout_height="wrap_content"
android:padding="12dip"
android:background="@android:color/tertiary_text_light"
android:textColor="@android:color/background_dark"
android:gravity="center"
android:layout_alignRight="@+id/no_image_placeholder"
android:layout_alignBottom="@+id/no_image_placeholder"
/>
</RelativeLayout>
我正在为其设置图像和标题的布局。
{{1}}
请帮我解决这个问题。 我将非常感谢你的帮助。
答案 0 :(得分:0)
您应该使用ViewHolder模式在适配器上获得更好的内存消耗和性能。
static class ViewHolder {
TextView textView;
ImageView imageView;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if(convertView==null){
// inflate the layout
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
convertView = inflater.inflate(layoutResourceId, parent, false);
// well set up the ViewHolder
viewHolder = new ViewHolder();
viewHolder.textView = (TextView) convertView.findViewById(R.id.textView);
viewHolder.textView = (ImageView) convertView.findViewById(R.id.imageView);
//just need to set the image once, because all images will be same
//you can also set the image directly from XML
viewHolder.imageView.setImageResource(R.drawable.no_image);
// store the holder with the view.
convertView.setTag(viewHolder);
}else{
// we will avoid call findViewById() on resource everytime
// just use the viewHolder
viewHolder = (ViewHolder) convertView.getTag();
}
// object item based on the position
ObjectItem objectItem = data[position];
// assign values if the object is not null
if(objectItem != null) {
viewHolder.textView.setText(objectItem.itemName);
}
return convertView;
}
你的问题是你打电话
else {
myGrid = (View) convertView.getTag();
}
虽然您没有使用ViewHolder模式将标记设置为convertView
答案 1 :(得分:0)
我实现了RecyclerView,以便在Activity中将文件夹显示为Grid。
public class NewAlbumAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
String[] allAlbumName;
File[] filePathToAllAlbums;
ArrayList<File> firstImageOfAllFile;
Context context;
private static final int TYPE_HEADER = 2;
private static final int TYPE_ITEM = 1;
int countOfAlbums;
RecyclerViewClickListener recyclerViewClickListener;
public interface RecyclerViewClickListener {
void onClick(View view, int position);
void onLongClick(View view, int position);
}
public NewAlbumAdapter(Context context, File[] filePathToAllAlbums, String[] allAlbumName, int countOfAlbums, ArrayList<File> firstImageOfAllFile) {
this.context = context;
this.filePathToAllAlbums = filePathToAllAlbums;
this.allAlbumName = allAlbumName;
this.countOfAlbums = countOfAlbums;
this.firstImageOfAllFile = firstImageOfAllFile;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
// view= LayoutInflater.from(context).inflate(R.layout.no_photo_album,parent,false);
// MyViewHolder myViewHolder=new MyViewHolder(view);
// return myViewHolder;
// if (viewType == TYPE_ITEM) {
view = LayoutInflater.from(context).inflate(R.layout.no_photo_album, parent, false);
return RecyclerItemViewHolder.newInstance(view);
// } else if (viewType == TYPE_HEADER) {
// view = LayoutInflater.from(context).inflate(R.layout.recycler_header, parent, false);
// return new RecyclerHeaderViewHolder(view);
// }
// throw new RuntimeException("There is no type that matches the type " + viewType + " + make sure your using types correctly");
}
public int getBasicItemCount() {
return countOfAlbums == 0 ? 0 : allAlbumName.length;
}
@Override
public int getItemCount() {
return getBasicItemCount();
// return getBasicItemCount()+1; //Use this for header
}
public boolean isPositionHeader(int position) {
return position == 0;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
View view;
RecyclerItemViewHolder itemHolder = (RecyclerItemViewHolder) holder;
if (firstImageOfAllFile.get(position) != null) {
Bitmap bitmap = BitmapFactory.decodeFile(firstImageOfAllFile.get(position).toString());
itemHolder.setImageView(bitmap);
} else {
itemHolder.setImageView(R.drawable.no_photo_icon);
}
itemHolder.setAlbumTitle(allAlbumName[position]);
itemHolder.setNumberOfPic(String.valueOf(filePathToAllAlbums[position].list().length));
}
}
Right now I am not showing any images which are currently there in the folders, so I am showing it with the drawable icon.
And the items are being feeded into it using a item feeder.
public class RecyclerItemViewHolder extends RecyclerView.ViewHolder {
private TextView albumTitle, numberOfPic;
private ImageView imageView;
public RecyclerItemViewHolder(View itemView, TextView albumTitle, TextView numberOfPic, ImageView imageView) {
super(itemView);
this.albumTitle = albumTitle;
this.numberOfPic = numberOfPic;
this.imageView = imageView;
}
public static RecyclerItemViewHolder newInstance(View parent){
TextView albumTitle=(TextView)parent.findViewById(R.id.album_main_title_textview);
TextView numberOfPic=(TextView)parent.findViewById(R.id.number_of_pic_textview);
ImageView imageView=(ImageView)parent.findViewById(R.id.no_image_placeholder);
return new RecyclerItemViewHolder(parent,albumTitle,numberOfPic,imageView);
}
public void setAlbumTitle(String albumStringArray){ albumTitle.setText(albumStringArray); }
public void setNumberOfPic(String numberOfPicSetter){
numberOfPic.setText(numberOfPicSetter);
}
public void setImageView(int imageId){
imageView.setImageResource(imageId);
}
public void setImageView(Bitmap bitmap){
imageView.setImageBitmap(bitmap);
}
}
使用物品输送器类将物品送入RecyclerView。
public class RecyclerItemViewHolder extends RecyclerView.ViewHolder {
private TextView albumTitle, numberOfPic;
private ImageView imageView;
public RecyclerItemViewHolder(View itemView, TextView albumTitle, TextView numberOfPic, ImageView imageView) {
super(itemView);
this.albumTitle = albumTitle;
this.numberOfPic = numberOfPic;
this.imageView = imageView;
}
public static RecyclerItemViewHolder newInstance(View parent){
TextView albumTitle=(TextView)parent.findViewById(R.id.album_main_title_textview);
TextView numberOfPic=(TextView)parent.findViewById(R.id.number_of_pic_textview);
ImageView imageView=(ImageView)parent.findViewById(R.id.no_image_placeholder);
return new RecyclerItemViewHolder(parent,albumTitle,numberOfPic,imageView);
}
public void setAlbumTitle(String albumStringArray){ albumTitle.setText(albumStringArray); }
public void setNumberOfPic(String numberOfPicSetter){
numberOfPic.setText(numberOfPicSetter);
}
public void setImageView(int imageId){
imageView.setImageResource(imageId);
}
public void setImageView(Bitmap bitmap){
imageView.setImageBitmap(bitmap);
}
}
这就是我在RecyclerView中显示文件夹并使用CardView作为布局的方法。
很抱歉迟到的回复。 我很感谢@Hendra和@Sandeep的帮助。