android - GridView issue with large data while scrolling -
i having problem "gridview".if use 6 items not creating problem while scrolling increase no. of items behavior unpredictable. please check following video link better understanding problem: gridview issues video link
i attaching codes here:
activity_achievements.xml
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <relativelayout android:id="@+id/header" android:layout_width="match_parent" android:layout_height="60dp" android:layout_alignparenttop="true" android:background="@color/green_color"> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centervertical="true" android:layout_marginleft="10dp" android:layout_torightof="@+id/back_iv" android:text="acheivements" android:textcolor="@color/white_color" android:textsize="24sp" android:textstyle="bold" /> <imageview android:id="@+id/back_iv" android:layout_width="25dp" android:layout_height="25dp" android:layout_alignparentleft="true" android:layout_centervertical="true" android:layout_marginleft="10dp" android:layout_marginright="10dp" android:src="@drawable/back" /> <textview android:id="@+id/total_achievements_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentright="true" android:layout_centervertical="true" android:layout_marginright="10dp" android:text="0/15" android:textcolor="@color/white_color" android:textsize="24sp" android:textstyle="bold" /> </relativelayout> <gridview android:id="@+id/acheivements_gv" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/header" android:columnwidth="90dp" android:gravity="center" android:horizontalspacing="5dp" android:numcolumns="2" android:stretchmode="columnwidth" android:verticalspacing="5dp" /> </relativelayout>
achievements_row.xml
<?xml version="1.0" encoding="utf-8"?> <com.xyz.quitsmoking.view.achievementsview xmlns:android="http://schemas.android.com/apk/res/android" xmlns:card_view="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center"> <android.support.v7.widget.cardview android:id="@+id/card_view2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/card_view1" android:layout_gravity="center" android:layout_marginbottom="5dp" android:layout_marginleft="5dp" android:layout_marginright="5dp" android:layout_margintop="5dp" card_view:cardcornerradius="2dp" android:background="@color/white_color" card_view:contentpadding="2dp"> <linearlayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <imageview android:id="@+id/image_iv" android:layout_width="100dp" android:layout_height="100dp" android:layout_gravity="center" android:layout_margin="10dp" android:src="@drawable/achievement" /> <textview android:id="@+id/title_tv" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" android:gravity="center" android:textsize="20sp" android:padding="10dp" android:text="himalyi" android:textcolor="@color/green_color" /> <textview android:id="@+id/desc_tv" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" android:gravity="center" android:textsize="16sp" android:padding="10dp" android:text="5 cigarettes non-smoked" android:textcolor="@color/teal_color" /> </linearlayout> </android.support.v7.widget.cardview> </pairroxz.com.quitsmoking.view.achievementsview>
achievementsadapter.java
public class achievementsadapter extends baseadapter { private context context; private arraylist<achievements> list; private layoutinflater inflater; public achievementsadapter(context context,arraylist<achievements> list){ this.context = context; this.list = list; initialize(); } private void initialize() { inflater = layoutinflater.from(context); } @override public int getcount() { return list.size(); } @override public object getitem(int position) { return position; } @override public long getitemid(int position) { return position; } @override public view getview(int position, view convertview, viewgroup parent) { achievementsview view = null; if(view==null) { view = (achievementsview) inflater.inflate(r.layout.achievements_row,null); }else{ view = (achievementsview) convertview; } view.setcontents(list.get(position)); notifydatasetchanged(); return view; } }
achievementsview.java
public class achievementsview extends relativelayout { private textview title_tv,desc_tv; private imageview image_iv; public achievementsview(context context) { super(context); } public achievementsview(context context, attributeset attrs) { super(context, attrs); } public achievementsview(context context, attributeset attrs, int defstyleattr) { super(context, attrs, defstyleattr); } @targetapi(build.version_codes.lollipop) public achievementsview(context context, attributeset attrs, int defstyleattr, int defstyleres) { super(context, attrs, defstyleattr, defstyleres); } @override protected void onfinishinflate() { super.onfinishinflate(); initialize(); } private void initialize() { image_iv = (imageview)findviewbyid(r.id.image_iv); title_tv = (textview)findviewbyid(r.id.title_tv); desc_tv = (textview) findviewbyid(r.id.desc_tv); } public void setcontents(achievements achievements){ image_iv.setimagedrawable(getcontext().getresources().getdrawable(achievements.getimg())); title_tv.settext(achievements.gettitle()); desc_tv.settext(achievements.getdesc()); } }
achievementsactivity.java
public class achievementsactivity extends appcompatactivity implements view.onclicklistener { private gridview achievements_gv; private achievementsadapter adapter; private arraylist<achievements> list; databasehelper helper; private imageview back_iv; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); this.getwindow().setflags(windowmanager.layoutparams.flag_fullscreen, windowmanager.layoutparams.flag_fullscreen); setcontentview(r.layout.activity_achievements); initialize(); } private void initialize() { helper = databasehelper.getinstance(this); back_iv = (imageview) findviewbyid(r.id.back_iv); achievements_gv = (gridview) findviewbyid(r.id.acheivements_gv); list = new arraylist<achievements>(); getdata(); setlistener(); } private void setlistener() { back_iv.setonclicklistener(this); } private void getdata() { list = helper.getachievementslist(); //list = adapter = new achievementsadapter(this, list); achievements_gv.setadapter(adapter); } @override public void onclick(view v) { switch (v.getid()) { case r.id.back_iv: onbackpressed(); break; } } }
if(view == null) view.settag(viewholder); viewholder = (viewholder) view.gettag();
don't use this. or send code
Comments
Post a Comment