Other control of Android studio (option selected, number of columns changed)

Keywords: Mobile Android xml

GridView and Spinner

(follow up update of renderings)
Use GridView to display the grid. In the grid, there are pictures with text. In addition, a Spinner can control the number of GridView columns.
Case: the interface consists of a group of rows and columns with pictures and text. The Spinner controls the number of columns displayed.

Requirement: use GridView to display pictures and text. When the item of Spinner is selected, the number of GridView columns changes.
Idea: 1.Spinner setting options {1.Adapter; 2. Properties}.
2.GridView - layout of items
Use Adapter to set the content up {Adapter - {1. Custom inherits BaseAdapter; 2. Custom inherits arrayaadapter}
3. Onitem selectedlistener listening (note Item)

xml part code of main layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    
    <Spinner
        android:id="@+id/spinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        //Set options on the Spinner by setting properties
        android:entries="@array/num"/>
        
    <GridView
        android:id="@+id/dynamic"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
        
</LinearLayout>

Set string array: num array is used to display on the Spinner and title array is used to display on the sub layout

<resources>
    <string name="app_name">Unit3_1</string>
    <string-array name="num">
        <item>1</item>
        <item>2</item>
        <item>3</item>
        <item>4</item>
        <item>5</item>
    </string-array>
    
    <string-array name="title">
        <item>Boss</item>
        <item>penis</item>
        <item>Old three</item>
        <item>Old four</item>
        <item>Old five</item>
        <item>Old Sixth</item>
        <item>Seven</item>
        <item>Old eight</item>
        <item>intellectuals</item>
    </string-array>
</resources>

Be patient with this part, especially the public class adapter extends ArrayAdapter { }This part,
if (row ==null) {… }And the clever addition of wrapper improves the running efficiency.

public class GridSpinner extends Activity {
    String[] title;//Define an array to prepare the title to be displayed
    int[] resIds = {
            R.drawable.a, R.drawable.c, R.drawable.d, R.drawable.e,
            R.drawable.f, R.drawable.g, R.drawable.h, R.drawable.j, R.drawable.k,
    };//Get the picture to be displayed through ID

    GridView gridView;
    Spinner spinner;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
        super.onCreate( savedInstanceState, persistentState );
        setContentView( R.layout.gridspinner );

        spinner=findViewById( R.id.spinner );
        title = getResources().getStringArray( R.array.title );//Get the title to display
        gridView = findViewById( R.id.dynamic );
        gridView.setAdapter( new adapter() );

        //Sets the currently selected location. 1 is the 0 option, and 2 indicates that the number of rows you want to set is 3
        spinner.setSelection( 2 );

        spinner.setOnItemClickListener( new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //Use view to get the value displayed on the current spinner
                String num = "";
                //Directly set the property to display the content (there is a mechanism inside that is made by the adapter, but its layout is inside the system) and take out the value displayed by the spinner
                TextView tv=view.findViewById(R.id.text);
                num=tv.getText()+"";

                //According to the value displayed by spinner, set the column number of gridview, convert the string to integer by type conversion
                gridView.setNumColumns(Integer.parseInt( num ) );
            }
        } );
    }

    public class adapter extends ArrayAdapter<String> {
        //Call the constructor of the parent class. The setting context is applied to GridSpinner, resourceid is not determined so it is 0.
        public adapter() {
            super( GridSpinner.this, 0 );
        }

        //Set the number of data sources and how many times to loop; if you pass in the data of resource in the constructor, you don't need to write this method.
        @Override
        public int getCount() {
            return resIds.length;
        }//Set length (or quantity)

        @Override
        //   A key! Set how resources are displayed on GridView
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            View row=convertView;//Define a content View named row
            Wrapper wrapper = null;
            if (row ==null) {
                //If the content is empty, parse the sub layout of GridSpinner item in GridSpinner
                row = LayoutInflater.from( GridSpinner.this ).inflate( R.layout.gridspinneritem, parent, false );
                wrapper = new Wrapper(row);
                row.setTag( wrapper );//The holder will save it in the control after getting it
            }else {
                wrapper=(Wrapper) row.getTag();//If it's saved, it should be taken out
            }

            //Use these two controls to get the control through the holder
            ImageView imageView = wrapper.getImage();
            TextView textView = wrapper.getTextView();

            imageView.setImageResource( resIds[position] );//Show picture of current location
            textView.setText( title[position] );
            return row;
        }

        //Definition holder
        class Wrapper {
            ImageView image;
            TextView text;
            View row;

            //row is the constructor
            public Wrapper(View row){
                this.row=row;
            }

            public ImageView getImage() {
                if (image == null) {
                    image=row.findViewById(R.id.imageView);//Find in the middle of row, so define row
                }
                return image;
            }
            public TextView getTextView() {
                if(text==null){
                    text=row.findViewById(R.id.textView );
                }
                return text;
            }
        }
    }
}

The xml code of the sub layout adds a picture and a paragraph of text:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        app:srcCompat="@drawable/a" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="TextView" />
</LinearLayout>

Posted by champbronc2 on Fri, 13 Dec 2019 08:08:18 -0800