i have followed example @ https://developer.android.com/guide/topics/ui/layout/gridview.html , used other resources on make @ least not crash given i'm using grid view in fragment.
i'm trying list of images show in grid view (for simplicity i've put same image twice in mthumbids
in imageadapter
).
but when run app, no gridview @ showing in fragment. other views in fragment load fine, it's gridview isn't there. i'm not sure how debug this.
any appreciated, thanks.
imageadapter.java:
import android.content.context; import android.view.view; import android.view.viewgroup; import android.widget.baseadapter; import android.widget.gridview; import android.widget.imageview; public class imageadapter extends baseadapter { private context mcontext; public imageadapter(context c) { mcontext = c; } public int getcount() { return mthumbids.length; } public object getitem(int position) { return null; } public long getitemid(int position) { return 0; } // create new imageview each item referenced adapter public view getview(int position, view convertview, viewgroup parent) { imageview imageview; if (convertview == null) { // if it's not recycled, initialize attributes imageview = new imageview(mcontext); imageview.setlayoutparams(new gridview.layoutparams(85, 85)); imageview.setscaletype(imageview.scaletype.center_crop); imageview.setpadding(8, 8, 8, 8); } else { imageview = (imageview) convertview; } imageview.setimageresource(mthumbids[position]); return imageview; } // references our images private integer[] mthumbids = { r.drawable.placeholder, r.drawable.placeholder }; }
placeholderfragment:
public static class placeholderfragment extends fragment { ... @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_items, container, false); textview textview = (textview) rootview.findviewbyid(r.id.items_subheading); textview.settext(getstring(r.string.section_format, getarguments().getint(arg_section_number))); gridview allimages = (gridview) rootview.findviewbyid(r.id.items_all_images); allimages.setadapter(new imageadapter(rootview.getcontext())); allimages.setonitemclicklistener(new adapterview.onitemclicklistener() { public void onitemclick(adapterview<?> parent, view v, int position, long id) { // empty } }); return rootview; }
fragment_items.xml
<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: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="com.example.myapp.itemsactivity$placeholderfragment"> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <linearlayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2" android:gravity="top|left" android:orientation="vertical"> <imageview android:id="@+id/items_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/placeholder" android:scaletype="fitstart" style="@style/itemsimage" /> <gridview android:id="@+id/items_all_images" android:layout_width="wrap_content" android:layout_height="wrap_content" android:columnwidth="20dp" android:numcolumns="auto_fit" android:verticalspacing="10dp" android:horizontalspacing="10dp" android:gravity="center" android:background="#000000" /> </linearlayout> <linearlayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="3" android:orientation="vertical" android:background="@drawable/items_border"> <linearlayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!-- 10 text views --> </linearlayout> </linearlayout> </linearlayout>
edit
aiming for:
edit 2
solution: figured out! needed android:adjustviewbounds="true"
on imageview
. without attribute seemed taking unlimited space below position.
i think have issue linearlayout wraps gridview linearlayout vertical linear layout if use weight height of gridview should 0dp , in code height wrap_content , width 0dp please change height 0dp , width wrap_content or match_parent
<linearlayout android:layout_width="wrap_content" android:layout_height="0dp" android:layout_weight="2" android:gravity="top|left" android:orientation="vertical"> <gridview android:id="@+id/items_all_images" android:layout_width="match_parent" android:layout_height="match_parent" android:columnwidth="20dp" android:numcolumns="auto_fit" android:verticalspacing="10dp" android:horizontalspacing="10dp" android:gravity="center" />
update
according mock think do:
<?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:padding="5dp" android:orientation="vertical"> <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <textview android:layout_width="wrap_content" android:text="text1" android:layout_height="wrap_content" /> <textview android:layout_width="wrap_content" android:text="text1" android:layout_height="wrap_content" /> <textview android:layout_width="wrap_content" android:text="text1" android:layout_height="wrap_content" /> <!-- if number of textviews dynamic , not fixed it's better use listview adapter --> <!--<listview--> <!--android:id="@android:id/list"--> <!--android:layout_width="match_parent"--> <!--android:layout_height="wrap_content" />--> </linearlayout> <gridview android:id="@+id/items_all_images" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:color/holo_green_dark" android:layout_margintop="5dp" android:columnwidth="20dp" android:gravity="center" android:horizontalspacing="10dp" android:numcolumns="auto_fit" android:verticalspacing="10dp" /> </linearlayout>
please notice if textviews not fixed (can 3 or more ...) need use listview adapter. each line item in listview textview
Comments
Post a Comment