Dan*_*iel 30 xml layout android gridview
我有一个与Google教程非常相似的GridView,除了我想在运行时添加ImageViews(通过子活动).结果还可以,但View的布局搞砸了:GridView没有填充其父级的内容,我需要做些什么才能正确设计它?
这里添加孩子的代码:
public void initializeWorkbench(GridView gv, Vector<String> items) {
Prototype.workbench.setDimension(screenWidth, divider.height()+workbenchArea.height());
Prototype.workbench.activateWorkbench();
// this measures the workbench correctly
Log.d(Prototype.TAG, "workbench width: "+Prototype.workbench.getMeasuredWidth());
// 320
Log.d(Prototype.TAG, "workbench height: "+Prototype.workbench.getMeasuredHeight());
// 30
ImageAdapter imgAdapter = new ImageAdapter(this.getContext(), items);
gv.setAdapter(imgAdapter);
gv.measure(screenWidth, screenHeight);
gv.requestLayout();
gv.forceLayout();
Log.d(Prototype.TAG, "gv width: "+gv.getMeasuredWidth());
// 22
Log.d(Prototype.TAG, "gv height: "+gv.getMeasuredHeight());
// 119
Prototype.workbench.setDimension(screenWidth, divider.height()+workbenchArea.height());
}
}
Run Code Online (Sandbox Code Playgroud)
工作台中的activateWorkbench,setDimension和measure(GridView上方的LinearLayout):
public void activateWorkbench() {
if(this.equals(Prototype.workbench)) {
this.setOrientation(VERTICAL);
show = true;
measure();
}
}
public void setDimension(int w, int h) {
width = w;
height = h;
this.setLayoutParams(new LinearLayout.LayoutParams(width, height));
this.invalidate();
}
private void measure() {
if (this.getOrientation() == LinearLayout.VERTICAL) {
int h = 0;
int w = 0;
this.measureChildren(0, 0);
for (int i = 0; i < this.getChildCount(); i++) {
View v = this.getChildAt(i);
h += v.getMeasuredHeight();
w = (w < v.getMeasuredWidth()) ? v.getMeasuredWidth() : w;
}
if (this.equals(Prototype.tagarea))
height = (h < height) ? height : h;
if (this.equals(Prototype.tagarea))
width = (w < width) ? width : w;
}
this.setMeasuredDimension(width, height);
}
Run Code Online (Sandbox Code Playgroud)
ImageAdapter构造函数:
public ImageAdapter(Context c, Vector<String> items) {
mContext = c;
boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
// We can read and write the media
mExternalStorageAvailable = mExternalStorageWriteable = true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
// We can only read the media
mExternalStorageAvailable = true;
mExternalStorageWriteable = false;
} else {
// Something else is wrong. It may be one of many other states, but
// all we need
// to know is we can neither read nor write
mExternalStorageAvailable = mExternalStorageWriteable = false;
}
if (mExternalStorageAvailable && mExternalStorageWriteable) {
for (String item : items) {
File f = new File(item);
if (f.exists()) {
try {
FileInputStream fis = new FileInputStream(f);
Bitmap b = BitmapFactory.decodeStream(fis);
bitmaps.add(b);
files.add(f);
} catch (FileNotFoundException e) {
Log.e(Prototype.TAG, "", e);
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
和xml布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="bottom"
android:paddingLeft="0px"
android:paddingTop="0px"
android:paddingRight="0px">
<com.unimelb.pt3.ui.TransparentPanel
android:id="@+id/workbench"
android:layout_width="fill_parent"
android:layout_height="10px"
android:paddingTop="0px"
android:paddingLeft="0px"
android:paddingBottom="0px"
android:paddingRight="0px">
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center" />
</com.unimelb.pt3.ui.TransparentPanel>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
fly*_*erz 44
GridView有一个invalidateViews()方法.
当你调用这个方法时:"所有要重建和重绘的视图." http://developer.android.com/reference/android/widget/GridView.html
我想这就是你需要的:)
小智 34
您必须首先告诉适配器通知数据已更改并再次将适配器设置为网格
adapter.notifyDataChanged();
grid.setAdapter(adapter);
Run Code Online (Sandbox Code Playgroud)
小智 18
这可能会有所帮助.在对项目执行删除后刷新书籍图像缩略图的网格视图.使用adapter.notifyDataChanged(); 如上所述对我不起作用,因为它在我的适配器中调用.
//this is a call that retrieves cached data.
//your constructor can be designed and used without it.
final Object data = getLastNonConfigurationInstance();
Run Code Online (Sandbox Code Playgroud)
我基本上只是重新加载适配器并将其绑定到相同的视图.
//reload the adapter
adapter = new BooksAdapter(MyBooks.this, MyBooks.this, data, show_collection );
grid.invalidateViews();
grid.setAdapter(adapter);
Run Code Online (Sandbox Code Playgroud)
小智 10
@flyerz @snagnever
你们一起得到了它们.它应该是:
adapter.notifyDataChanged();
grid.invalidateViews();
Run Code Online (Sandbox Code Playgroud)
这将标记其数据已更改的适配器,然后在调用invalidateViews()方法后将其传播到网格.
很高兴我发现了这个问题,因为我无法弄清楚如何在渲染后将项目添加到网格中.
这些答案中没有一个对我有用,我不得不将它们全部混合在一起.要实际让GridView更新,您需要这样做:
adapter.notifyDataChanged();
grid.invalidateViews();
grid.setAdapter(adapter);
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助任何无法获得其他解决方案的人.
你不应该调用invalidateViews和setAdapter刷新grid view.保持grid view更新并不是一个好主意,如果以这种方式更新,则会花费大量时间和内存.
如果您有机会查看getView方法,您将看到convertView只创建一次.当您致电时notifyDataChanged,它将更新此视图.而如果您打电话invalidateViews,将重新创建以前创建的视图.这不是一个好的解决方案.
getView调用时会调用您的方法notifyDataChanged.所以你的getView方法看起来应该像下面的代码.
public List list;
public class SimpleItemViewHolder extends Object
{
public TextView textView;
public ImageView imageView;
}
public View getView(int position, View convertView, ViewGroup parent){
View itemView = convertView;
SimpleItemViewHolder viewHolder;
if(convertView==null)
{
viewHolder = (SimpleItemViewHolder)itemView.getTag();
}else{
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
itemView = inflater.inflate(R.layout.layout_name, null);
TextView labelField = (TextView) itemView.findViewById(R.id.label_field);
labelField.setText(list.get(position).Name);
//Typeface boldFont = Typeface.createFromAsset(context.getAssets(), "fonts/Font-Bold.ttf");
//labelField.setTypeface(boldFont);
ImageView imageView = (ImageView) itemView.findViewById(R.id.image_view);
//Bitmap bitmap = init your bitmap here;
//imageView.setImageBitmap(bitmap);
viewHolder = new SimpleItemViewHolder();
viewHolder.imageView = imageView;
viewHolder.textView = labelField;
itemView.setTag(viewHolder);
}
//don't create new views, instead use previous ones and update them.
viewHolder.textView.setText(list.get(position).Name);
//viewHolder.imageView.setImageBitmap(your_bitmap);
return itemView;
}
Run Code Online (Sandbox Code Playgroud)
您正在放置较高的GridView内部。com.unimelb.pt3.ui.TransparentPanel10px
px,你应该使用dp。com.unimelb.pt3.ui.TransparentPanel为android:layout_height="10px"android:layout_height="fill_parent"| 归档时间: |
|
| 查看次数: |
74147 次 |
| 最近记录: |