Hey guys I'm newbie at Android development, and I would like to build a sub item for each listview item.
Just like this:
ListView 1
sub item 1
ListView 1
sub item 1
I though that it would be esier to create a new relative layout for each listview item, so my XML file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/wrap_layout"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:orientation="horizontal" >
<RelativeLayout
android:id="#+id/first_col"
android:layout_width="fill_parent"
android:layout_height="200dp" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="200dp"
android:layout_alignParentBottom="true"
android:orientation="vertical" >
<ImageView
android:id="#+id/bg_image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:adjustViewBounds="true"
android:scaleType="centerCrop" />
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="#drawable/overlay_bg" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="9dp" >
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/current_event"
android:layout_alignParentLeft="true"
android:layout_marginBottom="6dp"
android:letterSpacing="1.2"
android:shadowColor="#color/dark_grey"
android:shadowDx="-2"
android:shadowDy="2"
android:shadowRadius="0.01"
android:textColor="#color/white"
android:textSize="22sp" />
<ImageView
android:layout_width="17dp"
android:layout_height="17dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/persons" />
<TextView
android:id="#+id/current_day"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/name"
android:layout_alignParentBottom="true"
android:text="TextView"
android:textColor="#color/grey"
android:textSize="13sp" />
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:id="#+id/second_col"
android:layout_width="fill_parent"
android:layout_marginTop="200dp"
android:layout_height="200dp" >
<TextView
android:id="#+id/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="15dp"
android:layout_marginTop="24dp"
android:text="TextView" />
</RelativeLayout>
</RelativeLayout>
Each item is divided by two columns, the second column should be hidden column, thats why I changed the layout height to 200dp.
And to show the full layout, I'm doing that:
public View getView(final int position, View convertView, ViewGroup parent) {
// here you let SimpleAdapter built the view normally.
final View v = super.getView(position, convertView, parent);
final RelativeLayout lebg = (RelativeLayout) v.findViewById(R.id.wrap_layout);
lebg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View b) {
lebg.getLayoutParams().height = 400;
}
});
return v;
}
I just need to change the height from 200 to 400 to show the full Listview item.
The issue:
Is not working that well because when I click at the listview it only changes the height when I scroll...
Is there any better way to do what I am trying?
Thanks.
When you change LayoutParams you have to call requestLayout to see changes:
lebg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View b) {
lebg.getLayoutParams().height = 400;
requestLayout();
}
});
Related
I need to detect the onclick event on a imageview, and get the position to get the row from a cursor, and I don't know how.
I'm trying to modify https://github.com/Odoo-mobile/framework to and shopping cart app.
The code of the layout view where I need to detect the click event is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:orientation="horizontal"
android:padding="10dp" >
<!-- ListRow Left sied Thumbnail image -->
<LinearLayout
android:id="#+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginRight="7dip"
android:padding="2dip" >
<odoo.controls.BezelImageView
android:id="#+id/image_small"
android:layout_width="40dp"
android:layout_margin="#dimen/default_8dp"
app:maskDrawable="#drawable/circle_mask"
android:layout_height="40dp" />
</LinearLayout>
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/thumbnail"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="#color/body_text_1"/>
<LinearLayout
android:id="#+id/cart_plus_minus_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/name"
android:orientation="horizontal" >
<TextView
android:id="#+id/list_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_marginLeft="60dip"
android:layout_weight="0.23"
android:paddingRight="5dip"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="#color/android_red" />
<ImageView
android:id="#+id/cart_minus_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_action_minuscircle" />
<TextView
android:id="#+id/cart_product_quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:paddingLeft="15dip"
android:paddingRight="15dip"
android:text="0"
android:textSize="18dip"
android:textStyle="bold" />
<ImageView
android:id="#+id/cart_plus_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_action_addcircle" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
the images I need to detect the the click are: #+id/cart_plus_img #+id/cart_minus_img
When the row is click the function that capture the event is:
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ODataRow row = OCursorUtils.toDatarow((Cursor) mAdapter.getItem(position));
//AddToCart(row);
loadActivity(row);
}
What I need is to call loadActivity(row) when the user click the row but when click #+id/cart_plus_img #+id/cart_minus_img call another function but with the row parameter
Any help is thanksfull
Use the Tag property of the View
ImageView imgMinus = (ImageView)view.findViewById(R.id.cart_minus_img);
ImageView imgPlus = (ImageView)view.findViewById(R.id.cart_plus_img);
imgMinus.setTag(position);
imgPlus.setTag(position);
imgMinus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Integer position = (Integer) v.getTag();
...
}
});
imgPlus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Integer position = (Integer) v.getTag();
...
}
});
i am using below xml for my ListView item 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:gravity="center_vertical"
android:orientation="horizontal"
android:minHeight="?android:attr/listPreferredItemHeight"
android:padding="16dp">
<ImageView
android:id="#+id/contact_item_icon"
android:layout_width="50dp"
android:layout_height="50dp"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:paddingLeft="16dp">
<TextView
android:id="#+id/contact_item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Feel The Light" />
<TextView
android:id="#+id/contact_item_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Home" />
</LinearLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_user_audio_call_dark"
android:id="#+id/btn_audio_call"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_user_video_call_dark"
android:id="#+id/btn_video_call"/>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="30dp"
android:text="Invite"
android:background="#drawable/selector_button_green_oval"
android:textColor="#color/white"
android:id="#+id/btn_invite"/>
</RelativeLayout>
</LinearLayout>
i know in this xml some views are overlapping each other but in my custom array adapter i make visibility to gone for some of them based on certain condition, but what this xml do is to disable my ListView onItemClickListener but if i exclude this from xml then onItemClickListener works fine but i want to make onItemClickListener work without removing xml
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_user_audio_call_dark"
android:id="#+id/btn_audio_call"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_user_video_call_dark"
android:id="#+id/btn_video_call"/>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="30dp"
android:text="Invite"
android:background="#drawable/selector_button_green_oval"
android:textColor="#color/white"
android:id="#+id/btn_invite"/>
</RelativeLayout>
In your adapter getView(), set onClickListener to each button.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Do something
}
}
If you want to handle click event for entire list item then you need to set onClickListener to root LinearLayout of your listViewItem XML:
ListViewItem XML: Give an id to LinearLayout
<?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:gravity="center_vertical"
android:orientation="horizontal"
android:minHeight="?android:attr/listPreferredItemHeight"
android:padding="16dp"
android:id="#+id/container">
<ImageView
android:id="#+id/contact_item_icon"
android:layout_width="50dp"
android:layout_height="50dp"/>
.................
...................
Adapter: Set OnClickListener to LinearLayout
#Override
public View getView(int position, View convertView, ViewGroup parent) {
.............
.................
LinearLayout container = (LinearLayout) convertView.findViewById(R.id.container);
container.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Do something
}
}
.................
...................
}
Hope this will help!
I have created a custom ListView with 2 ImageView and a TextView.
In the Adapter, I'm doing setText in TextView, but it is not visible in the app.
Here is my List:
<?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"
android:background="#ffffff">
<View
android:layout_width="fill_parent"
android:layout_height="5dp"
android:background="#289FA5"/>
<RelativeLayout
android:layout_marginTop="7dp"
android:layout_marginLeft="7dp"
android:layout_marginBottom="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#289FA5">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Popular products"
android:textSize="23sp"
android:padding="7dp"/>
</RelativeLayout>
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="120dp">
<LinearLayout
android:id="#+id/pop_pro_men"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
</LinearLayout>
</HorizontalScrollView>
<RelativeLayout
android:layout_marginTop="15dp"
android:layout_marginLeft="7dp"
android:layout_marginBottom="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#289FA5">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Catagories"
android:textSize="23sp"
android:padding="7dp"/>
</RelativeLayout>
<ListView
android:id="#+id/cata_men"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></ListView>
Here is my Custom List:
<LinearLayout
android:id="#+id/custom_list"
android:layout_width="match_parent"
android:layout_height="130dp"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:background="#ffffff">
<ImageView
android:layout_gravity="left"
android:id="#+id/imageView2"
android:layout_height="130dp"
android:layout_marginLeft="7dp"
android:layout_width="120dp"
android:src="#drawable/clothing_men"/>
<TextView
android:id="#+id/cata_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Clothing"
android:layout_marginTop="35dp"
android:textStyle="bold"
android:textSize="27sp"
android:padding="10dp"
android:layout_weight="1"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/forward_arrow"
android:layout_marginTop="42dp"/>
And Here is the getView method of my Adapter:
public View getView(final int position, View convertView, ViewGroup parent) {
final Holder holder = new Holder();
View rowView;
rowView = inflater.inflate(R.layout.custom_subcatagory_list, null,true);
TextView sub_catagory= (TextView) rowView.findViewById(R.id.cata_name);
ImageView pic = (ImageView) rowView.findViewById(R.id.imageView2);
sub_catagory.setText(catagory[position]);
pic.setImageResource(image[position]);
Log.d("Strings========", sub_catagory.getText().toString());
rowView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context, "You Clicked " + catagory[position], Toast.LENGTH_LONG).show();
}
});
return rowView;
}
Please Help as i am unable to find what is wrong in this.
Remove your layout_weight attribute from your TextView. If you remove sub_catagory.setText(catagory[position]); does "Clothing" appear when you run your app?
I got the answer now. I don't know how my textColor was set to White, because i didn't mentioned it. So, change the color to something else and it worked.And it wasted my whole day.
Sorry to disturb you all.
I have a list view with an image view and text view for each list element, as per the screenshot below. The image and text views together make the clickable element so if you tap to the right of any text nothing happens, you have to tap on the text or image. How do I make the link span the width of the whole list element, basically to the right hand edge of the screen?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/icon"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="10dp"
android:layout_marginTop="4dp"
android:layout_gravity="center|right"
android:gravity="center|right"
android:src="#drawable/image5" >
</ImageView>
<TextView
android:id="#+id/label"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="20sp"
android:layout_marginTop="4dp"
android:layout_gravity="center|right"
android:gravity="center|right" >
</TextView>
</LinearLayout>
Make your Parent Layout width and height as match_parent/fill_parent
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/icon"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="10dp"
android:layout_marginTop="4dp"
android:layout_gravity="center|right"
android:gravity="center|right"
android:src="#drawable/image5" >
</ImageView>
<TextView
android:id="#+id/label"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="20sp"
android:layout_marginTop="4dp"
android:layout_gravity="center|right"
android:gravity="center|right" >
</TextView>
</LinearLayout>
In your listview item layout, set the width of textview to 0dp and add android:layout_weight="1" attribute to textview.
You can make something like this inside your Adapters getView method:
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if(convertView==null) { ... }
...
vi.setOnClickListener(new OnListItemClickListener(position));
return vi;
}
private class OnListItemClickListener implements OnClickListener {
private int mPosition;
OnListItemClickListener(int pos) {
this.mPosition = pos;
}
#Override
public void onClick(View arg0) {
//DO WHATEVER YOU WANT WITH YOUR "CLICK". THE POSITION CLICKED IS STORED INSIDE mPosition.
}
}
I have a problem with my GridView when it receives incomplete row item number, it doesn't scroll anymore. My items are dynamic which are given by the server, I have 3 columns and if the number of GridView items are more than enough to enable scrolling and if the row item is 3, it works fine. For example (9 items):
But if the third row has only 1 or 2 item/s, it doesn't scroll anymore (7 or 8 items):
This has caused me headache more than excitement, my GridView in layout is:
<GridView
android:id="#+id/gridView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#color/white"
android:listSelector="#android:color/transparent"
android:numColumns="3"
android:stretchMode="columnWidth" >
</GridView>
In my activity nothing much, just declaration and setting its adapter. Did I miss anything? I tried adding verticalSpacing and padding separately but nothing's changed.
EDIT:
This is the setup in the whole layout:
Black is a RelativeLayout, FrameLayout and the last ImageView are aligned top and bottom respectively. Top TextView is below FrameLayout, bottom TextView is below top TextView. Now GridView is below bottom TextView and above ImageView, it should expand its height basing to the space left consumed between the first 3 elements and the bottom element. This is how it's setup in my layout.
I believe it's a layout problem more than in my code since I've done dozens of ListView and this has similar implementation.
EDIT2:
The layout:
<RelativeLayout 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"
tools:context=".SixthActivity" >
<FrameLayout
android:id="#+id/cover_frame"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/cover_image"
android:layout_width="match_parent"
android:layout_height="180dp"
android:background="#color/gray"
android:scaleType="centerCrop" />
<ProgressBar
android:id="#+id/cover_image_progressbar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="180dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="55dp"
android:layout_alignParentBottom="true"
android:orientation="horizontal" >
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center_vertical"
android:layout_margin="10dp" />
<TextView
android:id="#+id/school_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginBottom="10dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:ellipsize="end"
android:maxLines="2"
android:textColor="#color/white"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:id="#+id/container_sns"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_margin="10dp"
android:gravity="right"
android:orientation="horizontal" >
<ImageView
android:id="#+id/btn_telephone"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:visibility="gone" />
<ImageView
android:id="#+id/btn_email"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:visibility="gone" />
<ImageView
android:id="#+id/btn_facebook"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:visibility="gone" />
<ImageView
android:id="#+id/btn_twitter"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:visibility="gone" />
<ImageView
android:id="#+id/btn_line"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:visibility="gone" />
</LinearLayout>
</RelativeLayout>
</FrameLayout>
<TextView
android:id="#+id/student_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/cover_frame"
android:background="#f8f7f3"
android:padding="10dp"
android:textColor="#color/brown"
android:textStyle="bold" />
<RelativeLayout
android:id="#+id/container_news"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/student_name"
android:background="#drawable/bg_news"
android:clickable="true"
android:orientation="horizontal"
android:visibility="gone" >
<TextView
android:id="#+id/news"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_gravity="center_vertical"
android:layout_marginLeft="85dp"
android:layout_marginRight="10dp"
android:clickable="true"
android:ellipsize="end"
android:maxLines="2"
android:textColor="#color/brown"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/container_next_plan"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/container_news"
android:background="#drawable/bg_next"
android:clickable="true"
android:orientation="horizontal"
android:visibility="gone" >
<TextView
android:id="#+id/next_plan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_gravity="center_vertical"
android:layout_marginLeft="85dp"
android:layout_marginRight="10dp"
android:clickable="true"
android:ellipsize="end"
android:maxLines="2"
android:textColor="#color/brown"
android:textStyle="bold" />
</RelativeLayout>
<GridView
android:id="#+id/grid_view"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_above="#+id/mypage_footer"
android:layout_below="#id/container_next_plan"
android:background="#ACA899"
android:gravity="center"
android:numColumns="3" >
</GridView>
<ImageView
android:id="#+id/mypage_footer"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:scaleType="fitXY" />
</RelativeLayout>
Don't mind the ImageViews, I just took out their source. I needed to retain the setup from my actual layout. The code:
public class SixthActivity extends Activity {
private Integer[] gridViewItem = new Integer[] { 1, 2, 3, 4, 5, 6, 7, 8 };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sixth);
GridView gridView = (GridView) findViewById(R.id.grid_view);
CustomAdapter adapter = new CustomAdapter(this,
R.layout.item_module, gridViewItem);
gridView.setAdapter(adapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.sixth, menu);
return true;
}
}
The adapter:
public class CustomAdapter extends ArrayAdapter<Integer> {
private Context context;
private int resource;
private Integer[] moduleList;
public CustomAdapter(Context context, int resource,
Integer[] moduleList) {
super(context, resource, moduleList);
// TODO Auto-generated constructor stub
this.context = context;
this.resource = resource;
this.moduleList = moduleList;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(resource, parent, false);
holder = new ViewHolder();
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
holder.tvModuleName = (TextView) row.findViewById(R.id.module_name);
holder.tvModuleImage = (ImageView) row.findViewById(R.id.module_image);
holder.tvModuleName.setText("SAMPLESAMPLE");
return row;
}
public class ViewHolder {
public ImageView tvModuleImage;
public TextView tvModuleName;
}
}
The item layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/item_module"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="#+id/module_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/module_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:ellipsize="end"
android:singleLine="true" />
</LinearLayout>
Try to add numbers to gridViewItem to add items enough to trigger the scrolling just like in my screenshots, and try taking out 1 or 2 items at the bottom such that that row would be incomplete to replicate my problem. Thanks.