layout - Android - Custom GridView with separated XML -
my goal display chessboard. have activity displays menu , load instance of "chessview" extends gridview. i've created custom gridview because want have class dedicated display. class have own layout.xml etc ...
gameactivity.java :
protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_game); //init game cchess.getinstance().init(); //creating chessboard chessview lchessview = (chessview) findviewbyid(r.id.chessview); } activity_game.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:background="@color/background" tools:context=".gameactivity" > <project.chess.view.chessview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/chessview" > </project.chess.view.chessview> </relativelayout> chessview.java :
public class chessview extends gridview { public chessview(context pcontext, attributeset pattrs) { super(pcontext, pattrs); gridview.inflate(pcontext, r.layout.view_chess, null); this.setadapter(new chessadapter(pcontext)); } } view_chess.xml :
<?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" > <gridview android:id="@+id/gridview" android:layout_height="wrap_content" android:layout_width="wrap_content" android:numcolumns="8" android:gravity="center" > </gridview> </linearlayout> chessadapter.java :
public class chessadapter extends baseadapter { private context mcontext; public chessadapter (context pcontext) { this.mcontext = pcontext; } @override public int getcount() { return 64; } @override public view getview(int position, view convertview, viewgroup parent) { imageview limageview; if (convertview == null) { limageview = new imageview(mcontext); int size = parent.getwidth()/8; limageview.setlayoutparams(new gridview.layoutparams(size,size)); //background black or white depending of position int col = position/8 %2; if (col == 0) { if (position%2 == 0) limageview.setbackgroundcolor(color.white); else limageview.setbackgroundcolor(color.black); } else { if (position%2 == 0) limageview.setbackgroundcolor(color.black); else limageview.setbackgroundcolor(color.white); } //load images cpiece p = cchess.getinstance().getpiece(position/8, position%8); if( p != null) limageview.setimageresource(mcontext.getresources().getidentifier(p.tostring(), "drawable", mcontext.getpackagename())); } else { limageview = (imageview) convertview; } return limageview; } } the result : have 1 column of 64 squares image (my init() in cchess ok). clear, want 8x8 grid standards chessboards.
few days ago, had stuff in 1 activity without custom view simple gridview , working correctly. have broken since i've decided separate code in different classes
i'm sure forgot inflater somewhere don't understand how works , does. i've parsed google hours couldn't find answer.
can me please ? lot reading me , sorry english
i think problem layoutinflator in chessview.java
remove line class
no need inflate gridview in class
gridview.inflate(pcontext, r.layout.view_chess, null); and set column count , gravity in chess view in activity_game.xml file.
android:layout_width="wrap_content" android:numcolumns="8" i think works you.
Comments
Post a Comment