I want to set custom background of gridview but when grid load, first item of grid background is not load and remaining all item load properly. Below is my code so please help.
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"));
}
}
Here is my Adapter class.
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;
}
}
here is xml for adapter.
<?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>
actually I have used Picasso
library to load photo from internet. I have created custom background for each item of gridview but when gridview load first then only first item (zero position of grid) of grid not working and all item of gridview's background set properly.
I have solved my problem using below code.
@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;
}