我想设置gridview的自定义背景,但是当网格加载时,网格背景的第一项未加载并且正确加载所有项目。 以下是我的代码,请帮助。
public class PicassoExample extends Activity {
private GridView gvFirst;
private ArrayList<MyModel> arrayList;
private PicassoImageAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_picasso_example);
gvFirst = (GridView)findViewById(R.id.gvFirst);
arrayList = new ArrayList<MyModel>();
loadArraylist();
adapter = new PicassoImageAdapter(getApplicationContext(),arrayList);
gvFirst.setAdapter(adapter);
}
private void loadArraylist() {
arrayList.add(new MyModel("http://i.imgur.com/rFLNqWI.jpg"));
arrayList.add(new MyModel("http://i.imgur.com/C9pBVt7.jpg"));
arrayList.add(new MyModel("http://i.imgur.com/rT5vXE1.jpg"));
}
}
这是我的Adapter类。
public class PicassoImageAdapter extends BaseAdapter {
private Context context;
private LayoutInflater inflater;
private ArrayList<MyModel> arrayList;
public PicassoImageAdapter(Context applicationContext, ArrayList<MyModel> arrayList) {
context = applicationContext;
this.arrayList = arrayList;
}
@Override
public int getCount() {
return arrayList.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
class Holder {
ImageView imageView;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View myView = null;
try {
Holder holder;
myView = convertView;
if (myView == null) {
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
myView = inflater.inflate(R.layout.adapter_picasso, null);
holder = new Holder();
// here I am setting custom backgroud of each item of gride.
myView.setBackgroundResource(R.drawable.corner_gride_layout);
holder.imageView = (ImageView) myView.findViewById(R.id.imageView);
myView.setTag(holder);
}
else {
holder = (Holder) myView.getTag();
}
MyModel model = arrayList.get(position);
Picasso.with(this.context)
.load(model.getStrUrl())
.placeholder(R.drawable.ic_launcher)
.into(holder.imageView);
} catch (Exception e) {
e.printStackTrace();
}
return myView;
}
}
这里是适配器的xml。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView"
android:layout_width="100dp"
android:layout_height="100dp"
android:scaleType="fitXY"
/>
</LinearLayout>
实际上我使用Picasso
库从互联网上加载照片。我为gridview的每个项目创建了自定义背景,但是当gridview首先加载时,网格的第一个项目(网格的零位置)不起作用,并且gridview的所有项目的背景设置正确。
答案 0 :(得分:0)
首次调用getView方法时,父视图的宽度和高度为零。也许您可以获取屏幕内容的宽度和高度来设置它们的值。 在myView = inflater.inflate之后添加这些代码行(R.layout.adapter_picasso,null);
myView.setLayoutParams(new
AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT,
AbsListView.LayoutParams.MATCH_PARENT));
答案 1 :(得分:0)
我使用下面的代码解决了我的问题。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View myView = null;
try {
Holder holder;
myView = convertView;
if (myView == null) {
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
myView = inflater.inflate(R.layout.adapter_picasso, null);
holder = new Holder();
holder.imageView = (ImageView) myView.findViewById(R.id.imageView);
myView.setTag(holder);
}
else {
holder = (Holder) myView.getTag();
}
if(Build.VERSION.SDK_INT>15)
{
myView.setBackground(ContextCompat.getDrawable(context, R.drawable.corner_gride_layout));
}
else {
myView.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.corner_gride_layout));
}
MyModel model = arrayList.get(position);
Picasso.with(this.context)
.load(model.getStrUrl())
.placeholder(R.drawable.ic_launcher)
.into(holder.imageView);
} catch (Exception e) {
e.printStackTrace();
}
return myView;
}
答案 2 :(得分:0)
当 getView() 中的 convertView 不为 null 时,您可以只返回它。
if(convertView != null){
return convertView
}
*Here is what you want to let view do*