Android - Optimize GridView Scrolling - android

I have a fragment that contains a GridView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<GridView
android:padding="20dp"
android:id="#+id/sub_category_gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:horizontalSpacing="20dp"
android:numColumns="4"
android:verticalSpacing="10dp"
android:scrollingCache="true"
android:smoothScrollbar="true"
android:fastScrollEnabled="true"
tools:listitem="#layout/sub_category_view" />
</LinearLayout>
Every child of this GridView contains one TextView and one ImageView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:id="#+id/sub_category_view">
<ir.motorel.carbuyinstruduction.SquareLinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/button">
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="#drawable/picture1"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:layout_margin="10dp"
android:id="#+id/sub_category_img"/>
</ir.motorel.carbuyinstruduction.SquareLinearLayout>
<TextView
android:layout_marginTop="5dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello"
android:gravity="center"
android:textColor="#C36518"
android:id="#+id/sub_category_name"/>
</LinearLayout>
When I fill GridView with Texts and Images, it's become very Laggy and slow when I scrolling. This is My adapter Class:
public class CustomGrid extends BaseAdapter{
private Context mContext;
public CustomGrid(Context c) {
mContext = c;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return subCatTitle.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder viewHolder;
if(convertView == null){
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.sub_category_view, parent, false);
viewHolder = new ViewHolder();
viewHolder.subCatView = (LinearLayout) convertView.findViewById(R.id.sub_category_view);
viewHolder.subCatImg = (ImageView) convertView.findViewById(R.id.sub_category_img);
viewHolder.subCatTitle = (TextView) convertView.findViewById(R.id.sub_category_name);
//subCatImg.setImageResource(imgIDs.getResourceId(position, -1));
convertView.setTag(viewHolder);
}
else{
//imgIDs.recycle();
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.subCatTitle.setText(subCatTitle[position]);
return convertView;
}
}
public class ViewHolder{
public TextView subCatTitle;
public ImageView subCatImg;
public LinearLayout subCatView;
}
How can i optimize it to scroll more smoothly?
PS. My images are in drawable folder and their format is PNG.

I had a ImageView in my Activity's Header that I set a really big image to it. It was killing performance. There is no problem in GridView.

Related

Glide messes up gridlayout

I'm trying to make a grid view to dynamically load images and data from a web server, but if i try to use Glide to load the image it messes up my layout, im a newbie on android programming so please be patient
the correct layout is this:
but if i enable Glide it shows like this:
As you can see there is a wide gap at the bottom that shouldn't be there
Here is my Grid Adapter:
public class GridViewAdapter extends BaseAdapter {
private Context mContext;
ArrayList<HashMap<String, String>> hotelsList;
TextView hotelNameTv;
TextView hotelDistanceTv;
ImageView hotelImage;
public GridViewAdapter(Context c, ArrayList<HashMap<String, String>> hotelsList ) {
mContext = c;
this.hotelsList = hotelsList;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return hotelsList.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return hotelsList.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View grid;
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
grid = new View(mContext);
grid = inflater.inflate(R.layout.hotel_item, null);
hotelNameTv = (TextView) grid.findViewById(R.id.hotelName);
hotelDistanceTv = (TextView) grid.findViewById(R.id.hotelDistance);
hotelImage = (ImageView) grid.findViewById(R.id.hotelImage);
HashMap<String, String> hotel=hotelsList.get(position);
hotelNameTv.setText(hotel.get("name"));
hotelDistanceTv.setText(hotel.get("distancemsg"));
/*
Glide.with(mContext)
.load("http://192.168.0.6/hoteles360ws/img/kron02.jpg")
.into(hotelImage);
*/
} else {
grid = (View) convertView;
}
return grid;
}
}
Here is my Activity Main resource File:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="#color/windowBackground">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:orientation="vertical">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar" />
<GridView
android:id="#+id/hotelsGrid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:verticalSpacing="#dimen/small_margin"
android:horizontalSpacing="#dimen/small_margin"
android:stretchMode="columnWidth"
android:numColumns="2"
android:padding="10dp" />
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
And my item resource file
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:elevation="30dp"
>
<TextView
android:text="Hotel Name"
android:layout_width="match_parent"
android:layout_height="56sp"
android:gravity="center_vertical"
android:id="#+id/hotelName"
android:textAppearance="#android:style/TextAppearance.Material.Large"
android:textColor="#color/colorPrimary"
android:paddingLeft="5sp" />
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="#drawable/hotel"
android:id="#+id/hotelImage"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:cropToPadding="false"
android:layout_below="#+id/hotelName"
android:layout_alignParentStart="true" />
<TextView
android:text="Hotel Distance"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/hotelDistance"
android:background="#222222"
android:textAppearance="#android:style/TextAppearance.Material.Inverse"
android:layout_below="#+id/hotelImage"
android:layout_alignParentStart="true"
android:padding="5sp"
android:textColor="#color/gold" />
</RelativeLayout>
</RelativeLayout>
Thanks for your help!!!
Not sure what i did... first i changed my get view to this:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if(convertView == null){
final LayoutInflater layoutInflater = LayoutInflater.from(mContext);
convertView = layoutInflater.inflate(R.layout.hotel_item, null);
}
final TextView hotelName = (TextView)convertView.findViewById(R.id.hotelName);
final TextView hotelDistance = (TextView)convertView.findViewById(R.id.hotelDistance);
final ImageView hotelImage = (ImageView)convertView.findViewById(R.id.hotelImage);
HashMap<String, String> hotel=hotelsList.get(position);
hotelName.setText(hotel.get("name"));
hotelDistance.setText(hotel.get("distancemsg"));
Glide.with(mContext)
.load("http://192.168.0.6/hoteles360ws/img/kron02.jpg")
.into(hotelImage);
return convertView;
}
And it didn't worked...
But then i changed from glide to picasso, it worked perfectly...
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if(convertView == null){
final LayoutInflater layoutInflater = LayoutInflater.from(mContext);
convertView = layoutInflater.inflate(R.layout.hotel_item, null);
}
final TextView hotelName = (TextView)convertView.findViewById(R.id.hotelName);
final TextView hotelDistance = (TextView)convertView.findViewById(R.id.hotelDistance);
final ImageView hotelImage = (ImageView)convertView.findViewById(R.id.hotelImage);
HashMap<String, String> hotel=hotelsList.get(position);
hotelName.setText(hotel.get("name"));
hotelDistance.setText(hotel.get("distancemsg"));
Picasso.with(mContext)
.load("http://192.168.0.6/hoteles360ws/img/kron02.jpg")
.into(hotelImage);
return convertView;
}
I'll be very grateful if someone explains me what happened...
You can check this link below. Gridview in Android is quite studpid. (I suggest you use Recyclerview with GridLayoutManager)
How to have a GridView that adapts its height when items are added

Scrolling GridView's items make GridView scrolls up down and will be gone

I request you just see this video you will know what is the issue
https://www.youtube.com/watch?v=gsZSMRPEZSI&feature=youtu.be
Gridview scrolls and Gone when I scroll it's item
This issue is not occurring when there is limited no. of items in GridView or when we scroll slowly
I have taken Custom GridView, in that I am loading images and text from server
below is the code
GridView's XML file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/background" >
<TextView
android:id="#+id/edtTxtProfileQuotes"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/small_margin"
android:background="#drawable/blue_notification_shape"
android:drawableLeft="#drawable/blue_notification_icon"
android:drawablePadding="#dimen/small_margin"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/light_violet" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:adjustViewBounds="true"
android:src="#drawable/teambg" />
<GridView
android:id="#+id/grdGrupOfUnite"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/bottomBar"
android:layout_below="#id/edtTxtProfileQuotes"
android:numColumns="3" >
</GridView>
</RelativeLayout>
Adapter's xml file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:orientation="vertical" >
<com.example.achessapp.widget.CircularImageView
android:id="#+id/ivUniteGrpMemIcon"
android:layout_width="#dimen/icon_size"
android:layout_height="#dimen/icon_size"
android:layout_centerHorizontal="true"
android:src="#android:color/white" />
<TextView
android:id="#+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/ivUniteGrpMemIcon"
android:layout_centerHorizontal="true"
android:layout_marginTop="#dimen/extra_small_margin"
android:layout_marginBottom="#dimen/small_margin"
android:gravity="center_horizontal"
android:textColor="#android:color/black" />
</RelativeLayout>
in Activity File
adapter = new BondMemberAdapter(mActivity, list_members_bean);
mGridMembers.setAdapter(adapter);
In Adapter file
public class BondMemberAdapter extends BaseAdapter {
LayoutInflater mInflator;
Activity mActivity;
private ArrayList<MembersBean> list_members_bean;
Typeface tf = null;
public BondMemberAdapter(Activity activity, ArrayList<MembersBean> list) {
mActivity = activity;
mInflator = LayoutInflater.from(mActivity);
list_members_bean = list;
tf = Typeface.createFromAsset(
mActivity.getAssets(), Constants.FONT_TYPE);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return list_members_bean.size();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
MembersBean bean = list_members_bean.get(position);
if (convertView == null) {
convertView = mInflator.inflate(R.layout.unite_grup_member_item,
null);
viewHolder = new ViewHolder();
viewHolder.ivUser = (CircularImageView) convertView
.findViewById(R.id.ivUniteGrpMemIcon);
viewHolder.tvName = (TextView) convertView
.findViewById(R.id.tvName);
viewHolder.tvName.setTypeface(tf);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
String text = "";
text = bean.getScreen_name();
//viewHolder.ivUser.setImageResource(R.drawable.unite_item);
if (bean.getThumbnail_url() != null) {
if (bean.getThumbnail_url().equals("null")) {
ImageLoader.getInstance().displayImage(
"drawable://" + R.drawable.profile_placeholder,
viewHolder.ivUser);
} else {
ImageLoader.getInstance().displayImage(bean.getThumbnail_url(),
viewHolder.ivUser);
}
}
else {
ImageLoader.getInstance().displayImage(
"drawable://" + R.drawable.profile_placeholder,
viewHolder.ivUser);
}
viewHolder.tvName.setText(text);
return convertView;
}
static class ViewHolder {
CircularImageView ivUser;
TextView tvName;
int position;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list_members_bean.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
}

Android Adding Dynamic ListView Header

I have a list view and I am trying to set header for it so that it looks like a table header. Here is the XML file of my list view row:
PS: I have removed some codes to shorten the question
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="#+id/txtDisplayDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="#+id/txtDisplayCat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
And my XML file of my header list view:
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="#+id/txtDisplayDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Date" />
<TextView
android:id="#+id/txtDisplayCat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Category" />
And the class where I populate the list view:
private class ListAdapter extends BaseAdapter {
LayoutInflater inflater;
ViewHolder viewHolder;
public ListAdapter(Context context) {
// TODO Auto-generated constructor stub
inflater = LayoutInflater.from(context);
LinearLayout listHeaderView = (LinearLayout)inflater.inflate(
R.layout.trans_header_layout, null);
listview.addHeaderView(listHeaderView);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return _translist.size();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if (convertView == null) {
convertView = inflater.inflate(R.layout.trans_listview_row,
null);
viewHolder = new ViewHolder();
viewHolder.txt_ddate = (TextView) convertView
.findViewById(R.id.txtDisplayDate);
viewHolder.txt_dcat = (TextView) convertView
.findViewById(R.id.txtDisplayCat);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.txt_ddate.setText(_translist.get(position).getDate()
.trim());
viewHolder.txt_dcat.setText(_translist.get(position)
.getCategory().trim());
return convertView;
}
}
However, by using these codes, here is the output that I've gotten:
The header does not match with the column below it. I wonder is there any alternate way to add header to list view so that it looks like a table header.
Thanks in advance.
EDIT
My transaction_rec.XML where the listview is located:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Date" />
<TextView
android:id="#+id/txtDisplayDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Category" />
<TextView
android:id="#+id/txtDisplayCat"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
<ListView
android:id="#+id/listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
</LinearLayout>
And my transactionRec.java:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.transaction_rec);
context = this;
listview = (ListView) findViewById(R.id.listview);
//Code to get data from database
}
private class ListAdapter extends BaseAdapter {
LayoutInflater inflater;
ViewHolder viewHolder;
public ListAdapter(Context context) {
// TODO Auto-generated constructor stub
inflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return _translist.size();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if (convertView == null) {
convertView = inflater.inflate(R.layout.transaction_rec,
null);
viewHolder = new ViewHolder();
viewHolder.txt_ddate = (TextView) convertView
.findViewById(R.id.txtDisplayDate);
viewHolder.txt_dcat = (TextView) convertView
.findViewById(R.id.txtDisplayCat);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.txt_ddate.setText(_translist.get(position).getDate()
.trim());
viewHolder.txt_dcat.setText(_translist.get(position)
.getCategory().trim());
return convertView;
}
}
}
And the output I've gotten now:
try this...
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
with all text views.

Gridview numbering

I have this gridview. is it possible to have numbers on the side of gridview like in excel 1,2,3 or a, b, c ,d
my code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<GridView
android:id="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="4"
android:stretchMode="columnWidth" />
</RelativeLayout>
something i want to achieve
You'll have to add numbering to each of the cells and only display it when it is the leftmost cell.
Cell layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="#+id/row_number"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<!-- The rest of your layout -->
</LinearLayout>
</LinearLayout>
In your Adapter
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = LayoutInflater.from(context).inflate(R.layout.grid_cell);
TextView rowNumber = (TextView) v.findViewById(R.id.row_number);
if (position % colNum == 0) { // colNum: number of columns
int row = position / colNum + 1; // get the row number
rowNumber.setText(String.valueOf(row));
rowNumber.setVisibility(View.VISIBLE);
} else {
rowNumber.setVisibility(View.GONE);
}
return v;
}
Note: For the sake of simplicity I am not using the ViewHolder pattern. But you should!
// try this way hope this will help you...
grid_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center">
<TextView
android:id="#+id/txtGridItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"/>
<ImageView
android:id="#+id/imgGridItem"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher"
android:adjustViewBounds="true"
android:scaleType="fitXY"/>
</LinearLayout>
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<GridView
android:id="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="4"
android:stretchMode="columnWidth" />
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity {
private GridView gridview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridview = (GridView) findViewById(R.id.gridview);
ArrayList<String> images = new ArrayList<String>();
images.add("image1path");
images.add("image2path");
images.add("image3path");
images.add("image4path");
images.add("image5path");
images.add("image6path");
images.add("image7path");
images.add("image8path");
images.add("image9path");
images.add("image10path");
images.add("image11path");
images.add("image12path");
gridview.setAdapter(new GridAdapter(images,this));
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this,String.valueOf(position+1),Toast.LENGTH_SHORT).show();
}
});
}
class GridAdapter extends BaseAdapter{
private Context context;
private ArrayList<String> images;
public GridAdapter(ArrayList<String> images,Context context){
this.context = context;
this.images = images;
}
#Override
public int getCount() {
return images.size();
}
#Override
public Object getItem(int position) {
return images.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null){
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.grid_item,null,false);
holder.imgGridItem = (ImageView) convertView.findViewById(R.id.imgGridItem);
holder.txtGridItem = (TextView) convertView.findViewById(R.id.txtGridItem);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
if(position%4 == 0){
holder.txtGridItem.setVisibility(View.VISIBLE);
holder.txtGridItem.setText(String.valueOf((position / 4)+1));
}else{
holder.txtGridItem.setVisibility(View.GONE);
}
holder.imgGridItem.setImageResource(R.drawable.ic_launcher);
return convertView;
}
class ViewHolder{
ImageView imgGridItem;
TextView txtGridItem;
}
}
}

Display data vertically in ListView

I have created an android application in that I want to display some Image data vertically in ListView.
How to make it possible ?
I tried using this code-
<LinearLayout
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="360dp"
android:layout_above="#+id/relativeLayout2"
android:layout_below="#+id/relativeLayout1"
android:gravity="center"
android:orientation="vertical" >
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="left|top"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="15dp"
app:layout_gravity="fill_vertical"
android:entries="#array/country_image_list" >
</ListView>
</LinearLayout>
public class ItemImagesAdapter extends BaseAdapter {
private Context context;
private ArrayList<String> paths;
private ImageLoader iml;
public ItemImagesAdapter(FragmentActivity activity, ArrayList<String> image_paths) {
context = activity;
paths = image_paths;
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(activity).build();
ImageLoader.getInstance().init(config);
iml = ImageLoader.getInstance();
}
#Override
public int getCount() {
return paths.size();
}
#Override
public Object getItem(int position) {
return paths.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item_image, parent, false);
}
String path = paths.get(position);
ImageView image = (ImageView) convertView.findViewById(R.id.image1);
iml.displayImage(path, image);
return convertView;
}
}
take one more layout for list item as R.layout.list_item_image.
here i am taking image loader to load the images from server.
if you are getting images by means local...then set them directly
hope this may helps you
You can try to manage your requirement using TwoWayView for Horizontal scrolling of ListView items with Vertical scrolling. You can aslo check this demo for the same on github.
XML for first activity
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".FirstActivity"
android:orientation="vertical">
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
</LinearLayout>
XML for row layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<ImageView
android:id="#+id/myImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
Create this layouts and then using custom adapter class you can fill the images for each row in image view...Something like this
Custom Adapter class
public class customAdapter extends BaseAdapter {
ArrayList<HashMap<String, String>> al = new ArrayList<HashMap<String,String>>();
Activity activity;
private static LayoutInflater inflater = null;
public customAdapter(Activity a, ArrayList<HashMap<String, String>> al) {
Log.e("check", "");
activity = a;
this.al = al;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return al.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
imageLoader=new ImageLoader(activity.getApplicationContext());
View vi=convertView;
if(convertView==null)
{
vi = inflater.inflate(R.layout.row_layout, null);
}
ImageView myImage = (ImageView) vi.findViewById(R.id.myImage);
imageLoader.DisplayImage(//your image from resource here),myImage);
return vi;
}
}
Any problem in this you can follow the link of the tutorial I have mentioned in the comments...

Categories

Resources