1

I have made a custom gridView and a item witch I want to implement in the gridView, but how can I set these custom gridView onCreate inside my HomeActivity class file?

HomeActivity.java

  public class HomeActivity extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_home);

            // how can I say here that I want to set the gridView from list_holder.xml

        }
    }

list_holder.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <GridView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/view_item_block_holder"
        android:layout_alignParentEnd="false"
        android:numColumns="auto_fit"
        tools:listitem="@layout/item_block" />
</RelativeLayout>

item_block.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/item_block"
    android:padding="5dp">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:background="@color/secondaryBackgroundColor">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/secondaryBackgroundColor"
            android:paddingLeft="10dp"
            android:paddingTop="10dp"
            android:paddingRight="10dp">

            <ImageView
                android:layout_width="150dp"
                android:layout_height="150dp"
                android:id="@+id/imageView2"
            android:background="@drawable/no_image_available" />
        </LinearLayout>

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/secondaryBackgroundColor"
            android:padding="10dp"
            android:layout_margin="5dp">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="video title"
                android:id="@+id/item_block_title"
                android:textColor="@color/textTitleColor"
                android:textStyle="bold"
                android:textSize="15dp" />

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="5dp">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="New Text"
                    android:id="@+id/item_block_description"
                    android:textColor="@color/textContentColor"
                    android:textSize="10dp"
                    android:layout_weight="1" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="12-09-16"
                    android:id="@+id/item_date"
                    android:textColor="@color/textContentColor"
                    android:textSize="10dp"
                    android:layout_weight="2" />
            </LinearLayout>

        </LinearLayout>
    </LinearLayout>

</RelativeLayout>

1 Answer 1

1

HomeActivity.java

public class HomeActivity extends AppCompatActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_home);

            //Here Adapter like
            AdapterDemoGrid adapterDemoGrid  = new AdapterDemoGrid(this);
            gridView.setAdapter(adapterDemoGrid);

        }
    }

Here, Adapter for Custom GridView Make this class :

public class AdapterDemoGrid extends BaseAdapter {

        private final Context context;

        private LayoutInflater mLayoutInflater;


        public AdapterDemoGrid(Context context) {

            mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            this.context = context;


        }


        @Override
        public void notifyDataSetChanged() {
            super.notifyDataSetChanged();
            //lastPosition = -1;
        }

        @Override
        public int getCount() {
            return 20;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View view, ViewGroup viewGroup) {
            ViewHolder holder = null;

            if (view == null) {

                //The view is not a recycled one: we have to inflate

                view = mLayoutInflater.inflate(R.layout.item_block.xml, viewGroup, false);

                holder = new ViewHolder();


                view.setTag(holder);

            } else {

                holder = (ViewHolder) view.getTag();
            }

            return view;
        }


        class ViewHolder {

            /// Item Block views goes here...

        }
    }

Hopefully it will help you !!

Sign up to request clarification or add additional context in comments.

1 Comment

that's exactly what I needed!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.