Please refer the following codes.First is my grid_row.xml file. It is grid items layout file
<ProgressBar
android:layout_height="70dp"
android:layout_width="70dp"
android:id="@+id/myprogress"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_below="@+id/title" />
<View
android:layout_width="2dp"
android:layout_height="2dp"/>
<ImageView
android:layout_height="165dp"
android:id="@+id/imageView1"
android:layout_width="125dp"
android:scaleType="fitXY"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"/>
<View
android:layout_width="2dp"
android:layout_height="2dp"/>
<TextView
android:text="TextView"
android:layout_height="wrap_content"
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_below="@+id/imageView1"
android:textStyle="bold"
android:layout_marginTop="2dp"
android:layout_centerHorizontal="true"
android:textSize="20sp"
android:ellipsize="marquee">
</TextView>
<View
android:layout_width="2dp"
android:layout_height="2dp"/>
<TextView
android:text="TextView"
android:layout_height="wrap_content"
android:id="@+id/subTitle"
android:layout_width="wrap_content"
android:layout_below="@+id/title"
android:layout_marginTop="2dp"
android:layout_centerHorizontal="true"
android:textSize="18sp"
android:ellipsize="marquee">
</TextView>
</RelativeLayout>
Then Please go ahead with the adapter class for reference.
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.nostra13.universalimageloader.core.listener.ImageLoadingProgressListener;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
/**
* Created by mpatil on 28/05/15.
*/
public class GridViewAdapter extends BaseAdapter
{
private ArrayList<String> listTitle;
private ArrayList<String> listSubTitle;
private ArrayList<String> imgp;
private Context activity;
ViewHolder view;
Configuration_Parameter m_config=Configuration_Parameter.getInstance();
public GridViewAdapter(Context activity,ArrayList<String> listTitle, ArrayList<String> subTitle,ArrayList<String> img)
{
super();
this.listTitle = listTitle;
this.imgp = img;
this.listSubTitle=subTitle;
this.activity = activity;
}
@Override
public int getCount()
{
// TODO Auto-generated method stub
return listTitle.size();
}
@Override
public String getItem(int position)
{
// TODO Auto-generated method stub
return (String) (String) view.imgViewFlag.getTag();
}
@Override
public long getItemId(int position)
{
// TODO Auto-generated method stub
return 0;
}
public static class ViewHolder
{
public ImageView imgViewFlag;
public TextView txtViewTitle;
public TextView txtViewSubTitle;
public ProgressBar pg;
public ViewHolder(View v)
{
}
public ViewHolder()
{
}
}
@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
// TODO Auto-generated method stub
View participentView=convertView;
if(participentView == null || participentView.getTag() == null)
{
LayoutInflater inflater = null;
inflater=(LayoutInflater) parent.getContext().getSystemService(activity.LAYOUT_INFLATER_SERVICE);
view = new ViewHolder();
participentView = inflater.inflate(R.layout.grid_layout, null);
view.txtViewTitle = (TextView) participentView.findViewById(R.id.title);
view.txtViewSubTitle = (TextView) participentView.findViewById(R.id.subTitle);
view.pg=(ProgressBar)participentView.findViewById(R.id.myprogress);
view.imgViewFlag = (ImageView) participentView.findViewById(R.id.imageView1);
participentView.setTag(view);
}
else
{
view = (ViewHolder) participentView.getTag();
}
//download and display image from url
view.txtViewTitle.setText(listTitle.get(position));
view.txtViewSubTitle.setText(listSubTitle.get(position) + " subitem");
ImageLoader imageLoader = null;
imageLoader= ImageLoader.getInstance();
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showImageForEmptyUri(R.drawable.paceholder) // resource or drawable
.showImageOnFail(R.drawable.error_page_logo) // resource or drawable
.resetViewBeforeLoading(false) // default
.delayBeforeLoading(1000)
.cacheInMemory(true) // default
.cacheOnDisk(true) // default
.build();
m_config.imageLoader.displayImage(imgp.get(position), view.imgViewFlag,options,new SimpleImageLoadingListener()
{
@Override
public void onLoadingStarted(String imageUri, View v)
{
Log.i("Inside onLoadingStarted " + position,"Yes");
view.imgViewFlag.setVisibility(View.INVISIBLE);
view.pg.setVisibility(View.VISIBLE);
view.imgViewFlag.setVisibility(View.INVISIBLE);
}
@Override
public void onLoadingFailed(String imageUri, View v, FailReason failReason)
{
Log.i("Inside onLoadingFailed " + position,"Yes");
view.pg.setVisibility(View.GONE);
}
@Override
public void onLoadingComplete(String imageUri, View v, Bitmap loadedImage)
{
Log.i("Ins onLoadingComplete " + position, "Yes");
view.pg.setVisibility(View.GONE);
view.imgViewFlag.setVisibility(View.VISIBLE);
view.imgViewFlag.invalidate();
}
});
return participentView;
}
}
I am sure that this will help definitely. Thanks NOSTRA for such a great library. Thumbs Up...!!! Happy Coding... :)
myRecyclerView.smoothScrollToPosition(0)
after I updated the dataset but without implementing the necessary methods/classes that smoothScrollToPosition requires. Changing it to scrollToPosition(0) fixed my issues. See #38392369 – Stayathome