-
-
Save fariedrahmat/13dc1f6c187579afa38e7ee9f985c9e7 to your computer and use it in GitHub Desktop.
Gridview 2 Column with border
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
tools:context="masxdeveloper.bordergridview.MainActivity"> | |
<GridView | |
android:id="@+id/grid" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:numColumns="2" | |
android:layout_margin="10dp" | |
android:layout_centerInParent="true"/> | |
</RelativeLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package masxdeveloper.bordergridview; | |
import android.content.Context; | |
import android.support.annotation.LayoutRes; | |
import android.support.annotation.NonNull; | |
import android.support.annotation.Nullable; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.ArrayAdapter; | |
import android.widget.ImageView; | |
import android.widget.TextView; | |
import java.util.List; | |
/** | |
* Created by JonesRandom on 04/03/2017. | |
* https://masx-dev-blogspot.co.id | |
*/ | |
public class GridViewAdapter extends ArrayAdapter<Model> { | |
List<Model> data; | |
int row; | |
public GridViewAdapter(@NonNull Context context, @LayoutRes int resource, @NonNull List<Model> objects) { | |
super(context, resource, objects); | |
data = objects; | |
row = resource; | |
} | |
@NonNull | |
@Override | |
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { | |
ViewHolder holder; | |
if (convertView == null) { | |
holder = new ViewHolder(); | |
convertView = LayoutInflater.from(parent.getContext()).inflate(row, parent, false); | |
holder.icon = (ImageView) convertView.findViewById(R.id.row_icon); | |
holder.tvKet = (TextView) convertView.findViewById(R.id.row_ket); | |
holder.viewAtas = convertView.findViewById(R.id.view_atas); | |
holder.viewKiri = convertView.findViewById(R.id.view_kiri); | |
holder.viewKanan = convertView.findViewById(R.id.view_kanan); | |
convertView.setTag(holder); | |
} else { | |
holder = (ViewHolder)convertView.getTag(); | |
} | |
holder.tvKet.setText(data.get(position).getKeterangan()); | |
holder.icon.setImageResource(data.get(position).getIcon()); | |
if (position == 0 || position == 1) { | |
holder.viewAtas.setVisibility(View.GONE); | |
} else { | |
holder.viewAtas.setVisibility(View.VISIBLE); | |
} | |
if (position % 2 == 1){ | |
holder.viewKiri.setVisibility(View.VISIBLE); | |
holder.viewKanan.setVisibility(View.INVISIBLE); | |
} else { | |
holder.viewKiri.setVisibility(View.INVISIBLE); | |
holder.viewKanan.setVisibility(View.VISIBLE); | |
} | |
return convertView; | |
} | |
private class ViewHolder { | |
TextView tvKet; | |
ImageView icon; | |
View viewAtas; | |
View viewKiri; | |
View viewKanan; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package masxdeveloper.bordergridview; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.widget.GridView; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class MainActivity extends AppCompatActivity { | |
GridView gridView; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
List<Model> data = new ArrayList<>(); | |
data.add(new Model(R.mipmap.ic_launcher, "Mantan 1")); | |
data.add(new Model(R.mipmap.ic_launcher, "Mantan 2")); | |
data.add(new Model(R.mipmap.ic_launcher, "Mantan 3")); | |
data.add(new Model(R.mipmap.ic_launcher, "Mantan 4")); | |
data.add(new Model(R.mipmap.ic_launcher, "Mantan 5")); | |
data.add(new Model(R.mipmap.ic_launcher, "Mantan 6")); | |
data.add(new Model(R.mipmap.ic_launcher, "Mantan 7")); | |
data.add(new Model(R.mipmap.ic_launcher, "Mantan 8")); | |
GridViewAdapter adapter = new GridViewAdapter(this, R.layout.row_gridview, data); | |
gridView = (GridView) findViewById(R.id.grid); | |
gridView.setAdapter(adapter); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package masxdeveloper.bordergridview; | |
/** | |
* Created by JonesRandom on 04/03/2017. | |
* https://masx-dev-blogspot.co.id | |
*/ | |
public class Model { | |
int icon; | |
String Keterangan; | |
public Model(int icon, String keterangan) { | |
this.icon = icon; | |
Keterangan = keterangan; | |
} | |
public int getIcon() { | |
return icon; | |
} | |
public String getKeterangan() { | |
return Keterangan; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:orientation="vertical"> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:orientation="vertical"> | |
<View | |
android:id="@+id/view_atas" | |
android:layout_width="match_parent" | |
android:layout_height="0.50dp" | |
android:background="#cc197300" /> | |
<RelativeLayout | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content"> | |
<View | |
android:id="@+id/view_kiri" | |
android:layout_width="0.25dp" | |
android:layout_height="wrap_content" | |
android:background="#cc197300" /> | |
<LinearLayout | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_centerInParent="true" | |
android:orientation="vertical" | |
android:padding="10dp"> | |
<ImageView | |
android:id="@+id/row_icon" | |
android:layout_width="100dp" | |
android:layout_height="100dp" | |
android:layout_gravity="center_horizontal" | |
android:padding="10dp" | |
android:src="@mipmap/ic_launcher" /> | |
<TextView | |
android:id="@+id/row_ket" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:gravity="center" | |
android:text="Mantan 1" | |
android:textStyle="bold" /> | |
</LinearLayout> | |
<View | |
android:id="@+id/view_kanan" | |
android:layout_width="0.25dp" | |
android:layout_height="wrap_content" | |
android:layout_alignParentRight="true" | |
android:background="#cc197300" /> | |
</RelativeLayout> | |
</LinearLayout> | |
</LinearLayout> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment