Android Studio application basics, teach you from entry to mastery (Xiaobai learning) Summary 2 common interface layout and ListView

Summary 1 link:

(156 messages) Android Studio application basics, teach you from entry to mastery (Xiaobai learning) summary 1

Learning video link: 

(Must be learned after learning) Android studio basics, from entry to proficiency, even beginners can learn after learning_哔哩哔哩_bilibili


Table of contents

Five, common interface layout

1. LinearLayout layout

1.1 orientation attribute: determines the arrangement of controls 

1.2 layout_gravity: Determines the alignment of controls in the layout

1.3 gravity: determines the alignment of text in the control

1.4 weight weight

 2. RelativeLayout relative layout

 2.1 Location attributes positioned according to the layout

2.2 Location properties

3. ConstraintLayout layout

4. Custom layout

4.1 Writing a custom layout

4.2 Reference the custom layout in the main layout

4.3 Hide the system's own title bar

5. TableLayout row and column layout 

6. Horizontal and vertical scroll bars

1. HorizentalScrollView horizontal scroll bar

2. Vertical scroll bar ScrollView

Seven, ListView scrolling menu

1. Simple application of scrolling menu ListView

2. Customize ListView interface

3. Implement the click event of ListView


Five, common interface layout

1. LinearLayout layout

The syntax format of the LinearLayout layout is

<LinearLayout 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=".MainActivity"
    android:orientation="horizontal"
    >

……

</LinearLayout>

1.1 orientation attribute: determines the arrangement of controls 

 android:orientation="horizontal" or "vertical"

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".MainActivity"
    android:orientation="horizontal"
    >

                   

            horizontal horizontal arrangement vertical vertical arrangement

If you do not specify the value of orientation, the default arrangement is horizontal.

  • If the arrangement mode is horizontal, you cannot specify the width as match-parent, because the width of a button will fill the entire screen, as shown in Figure 1 below
  • If the arrangement is vertical, the height cannot be specified as match-parent, because the height of a button will fill the entire screen, as shown in Figure 2 below

                                                                 1                                                                            2                   

1.2 layout_gravity: Determines the alignment of controls in the layout

<Button
        android:id="@+id/b1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:text="按钮1"
        />

    <Button
        android:id="@+id/b2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:text="按钮2"
        />
    <Button
        android:id="@+id/b3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:text="按钮3"
        />

Effect:

1.3 gravity: determines the alignment of text in the control

  •  Change the text alignment of button2 to bottom
  • android:gravity="right|bottom" combined use
 <Button
        android:id="@+id/b2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:gravity="bottom"
        android:text="按钮2"
        />

Effect:        

1.4 weight weight

(1) Function : It can make the controls in the layout display the size according to the weight ratio, which plays an important role in screen adaptation

Because the weight attribute is used, the width of the control can be set to 0dp, and the width at this time is determined by the weight value

(2) Application:

  •  Set the weights of both controls to 1, then the screen will allocate the size of the controls in a 1:1 ratio
<EditText
        android:id="@+id/input1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:hint="请输入信息"></EditText>

    <Button
        android:id="@+id/send"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="发送"
        />

The effect is shown in Figure 1 below.

  • Set the weight of the input box to 1, and set the width of the button to wrap-content, as shown in Figure 2, which is a commonly used mobile phone layout setting.

    <EditText
        android:id="@+id/input1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:hint="请输入信息"></EditText>

    <Button
        android:id="@+id/send"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="发送"
        />

                                                                           1                                                                      2

 2. RelativeLayout relative layout

The syntax is as follows: 

<?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=".MainActivity"
    >

</RelativeLayout>

 2.1 Location attributes positioned according to the layout

Set different page effects for the 5 buttons respectively.

  • android:layout_alignParentLeft="true" Set the current control to be left aligned with the parent layout
  • android:layout_alignParentTop="true" Set the current control to align with the top of the parent layout
  • android:layout_centerInParent="true" sets the current control to be in the center of the parent layout 
<Button
        android:id="@+id/b1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="按钮1"></Button>

    <Button
        android:id="@+id/b2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="按钮2"></Button>

    <Button
        android:id="@+id/b3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="按钮3"></Button>

    <Button
        android:id="@+id/b4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:text="按钮4"></Button>

    <Button
        android:id="@+id/b5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:text="按钮5"></Button>

Effect:

2.2 Location properties based on other controls

Let buttons 1, 2, 4, and 5 be arranged according to button 3 

  •  android:layout_above="@+id/b3" This control is above b3
  •  android:layout_toLeftOf="@+id/b3" This control is on the left of b3
  • android:layout_below="@+id/b3" This control is below b3
  • android:layout_toRightOf="@+id/b3" This control is on the right of b3
<Button
        android:id="@+id/b1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/b3"
        android:layout_toLeftOf="@+id/b3"
        android:text="按钮1"></Button>

    <Button
        android:id="@+id/b2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/b3"
        android:layout_toRightOf="@+id/b3"
        android:text="按钮2"></Button>

    <Button
        android:id="@+id/b3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="按钮3"></Button>

    <Button
        android:id="@+id/b4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/b3"
        android:layout_toLeftOf="@+id/b3"
        android:text="按钮4"></Button>

    <Button
        android:id="@+id/b5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/b3"
        android:layout_toRightOf="@+id/b3"
        android:text="按钮5"></Button>

Effect:

3. ConstraintLayout layout

The syntax format is as follows: 

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity"
    >

    

</androidx.constraintlayout.widget.ConstraintLayout>

4. Custom layout

4.1 Writing a custom layout

  • Create a new layout file title.xml, import the required buttons and background images, and write the custom title bar
<?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="wrap_content"
    android:orientation="horizontal"
    android:background="@drawable/bb3">

    <Button
        android:id="@+id/title_back"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:layout_weight="1"
        android:background="@drawable/bb1"
        android:gravity="center"
        android:text="Back"
        android:textColor="#ffff"></Button>

    <TextView
        android:id="@+id/title_text"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="title text"
        android:textColor="#fff"
        android:textSize="24sp"></TextView>

    <Button
        android:id="@+id/title_edit"
        android:layout_width="0"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_margin="5dp"
        android:background="@drawable/bb2"
        android:gravity="center"
        android:text="edit"
        android:textColor="#ffff"></Button>

</LinearLayout>
  • In the process of writing the title bar, in order to prevent the control text from not being displayed, change the themes.xml file in the values ​​folder to the following statement.
<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.Layout" parent="Theme.MaterialComponents.DayNight.DarkActionBar.Bridge">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/purple_500</item>
        <item name="colorPrimaryVariant">@color/purple_700</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_700</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
        <!-- Customize your theme here. -->
    </style>
</resources>

The effect is as follows (a bit ugly)

4.2 Reference the custom layout in the main layout

Insert the following statement in the main_activity.xml file:

<!-- Import custom layout -->
    <include layout="@layout/title" ></include>

Indicates that a custom layout is introduced into this layout. 

The effect is as follows:

4.3 Hide the system's own title bar

Use ActionBar in main_activity.java to hide the system title bar. The specific usage method will be explained later~ 

package cn.edu.sdut.layout;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Notification;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ActionBar actionBar = getSupportActionBar();
        if(actionBar!=null){
            actionBar.hide();
        }
    }
}

Successfully hide the system title bar:

5. TableLayout row and column layout 

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

    <Button
        android:id="@+id/login"
        android:gravity="center"
        android:layout_centerInParent="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/loginTxt"/>

    <TableLayout
        android:layout_above="@+id/login"
        android:shrinkColumns="0"
        android:stretchColumns="1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:layout_marginLeft="10dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/name">

            </TextView>

            <EditText
                android:id="@+id/txt_user"
                android:layout_marginRight="10dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>

        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:layout_marginLeft="10dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/psw">

            </TextView>

            <EditText
                android:id="@+id/txt_psw"
                android:layout_marginRight="10dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>

        </TableRow>

    </TableLayout>




</RelativeLayout>

6. Horizontal and vertical scroll bars

1. HorizentalScrollView horizontal scroll bar

The horizontal scroll bar can only contain one view , so you can’t put multiple buttons in it directly, so you need to create a view with a linear layout first, put five buttons in it, and then put the linear layout in the horizontal scroll bar.

  • structure:
<?xml version="1.0" encoding="utf-8"?>

1. 第一层:线性布局大框架

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    2.第二层:水平滚动条部件

    <HorizontalScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

          3. 第三层:线性布局容器,用来装按钮

          <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

    
                      4. 放置多个按钮


           </LinearLayout>


    </HorizontalScrollView>

</LinearLayout>

  • code:
<?xml version="1.0" encoding="utf-8"?>

<!--
权重:控制宽度

-->

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

<!--    水平滚动条只能包含一个视图,所以需要先新建一个linear的视图,把五个按钮放进去,
再把linear放在水平滚动条内,即可-->

    <HorizontalScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:text="Button" />

    <!--&lt;!&ndash;    使用view给按钮添加间隔&ndash;&gt;-->
    <!--    <View-->
    <!--        android:layout_width="0dp"-->
    <!--        android:layout_weight="1"-->
    <!--        android:layout_height="wrap_content"></View>-->


    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:text="Button" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:text="Button" />

    <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>


    </HorizontalScrollView>

</LinearLayout>

2. Vertical scroll bar ScrollView

ScrollView is at the outermost layer, and there must be this statement of xmlns, because this statement is unique, and it can only and must be added to the header tag of the outermost frame.

xmlns:android="http://schemas.android.com/apk/res/android"

structure:



<?xml version="1.0" encoding="utf-8"?>


<!--xmlns给scrollview,因为这句话只有最外层的容器有-->

1. ScrollView,在最外层 

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    >

      2. 线性布局容器,装按钮

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

        
            3. 放置多个按钮


    </LinearLayout>

</ScrollView>

code:

<?xml version="1.0" encoding="utf-8"?>

<!--
权重:控制宽度

-->

<!--xmlns给scrollview,因为这句话只有最外层的容器有-->
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    >

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="100dp">

        </Button>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="100dp">

        </Button>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="100dp">

        </Button>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="100dp">

        </Button>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="100dp">

        </Button>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="100dp">

        </Button>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="100dp">

        </Button>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="100dp">

        </Button>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="100dp">

        </Button>


    </LinearLayout>

</ScrollView>

Seven, ListView scrolling menu

Create a new project named ListView and complete the follow-up operations.

1. Simple application of scrolling menu ListView

  •  Define a scroll menu in the layout file
<ListView
        android:id="@+id/list_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
  •  Define the display data and adapter in the MainActivity.java file

The fruit array of String type stores the content that the menu wants to display

The array in the array cannot be directly passed to the ListView, so it needs to be completed with the help of an adapter . The ArrayAdapter is used to complete the data transmission to the ListView. The constructor of the adapter needs to pass in three parameters:

  • The activity you are in
  • Android's built-in xml file's text container
  • The content you want to output.

Finally, use the setAdapter method to configure the adapter just defined on the scroll menu

package cn.edu.sdut.listview;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity {

//    数组存取listview想要展示的信息
    private String[] fruit ={"apple","banana","orange","watermelon",
            "pear","grape","strawberry","cherry","mango","Coconut","tomato","lemon",
        "apple","banana","orange","watermelon", "pear","grape","strawberry","cherry",
        "mango","Coconut"};

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

//        配置适配器
        ArrayAdapter<String> adapter = new ArrayAdapter<String>
                (MainActivity.this, android.R.layout.simple_list_item_1,fruit);

        ListView listView = (ListView) findViewById(R.id.list_view);
        listView.setAdapter(adapter);
    }
}

The effect is as follows:

2. Customize ListView interface

Realize that the fruit picture and the corresponding fruit name match are displayed in the scrolling list together.

  • Create a new Fruit.java class to write entity classes for fruit names and fruit picture names
package cn.edu.sdut.listview;

public class Fruit {
    private String name;
    private int imageId;

    public Fruit(String name, int imageId) {
        this.name = name;
        this.imageId = imageId;
    }

    public String getName() {
        return name;
    }

    public int getimageId() {
        return imageId;
    }
}
  •  Define the fruit adapter FruitAdaptor.java
package cn.edu.sdut.listview;

import android.content.Context;
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;

public class FruitAdapter extends ArrayAdapter<Fruit> {
    private int resourceId;

    public FruitAdapter(Context context, int textViewResourceId, List<Fruit> object){
        super(context,textViewResourceId,object);
        resourceId = textViewResourceId;

    }

    public View getView(int position, View coverView, ViewGroup parent){
        Fruit fruit = getItem(position);
        View view = LayoutInflater.from(getContext()).inflate(resourceId,parent,false);
        ImageView fruitImage = (ImageView) view.findViewById(R.id.fruit_image);
        TextView fruitName = (TextView) view.findViewById(R.id.fruit_name);
        fruitImage.setImageResource(fruit.getimageId());
        fruitName.setText(fruit.getName());
        return view;
    }

}
  •  Write the main activity MainActivity.java to write

     1. Create a new fruit list to store the fruit name and fruit picture name

     2. Use the initFruit method to add elements to the list

     3. Put the adapter on the scroll list set

package cn.edu.sdut.listview;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private List<Fruit> fruitList = new ArrayList<>();

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

        initFruits();
        
        FruitAdapter adapter = new FruitAdapter(MainActivity.this,R.layout.fruit_item,fruitList);
        ListView listView = (ListView) findViewById(R.id.list_view);
        listView.setAdapter(adapter);


    }

//把水果名称和对应图片加入信息列表
    private void initFruits(){
        for(int i=0;i<2;i++){
            Fruit apple = new Fruit("apple",R.drawable.apple);
            fruitList.add(apple);
            Fruit banana = new Fruit("banana",R.drawable.banana);
            fruitList.add(banana);
            Fruit orange = new Fruit("orange",R.drawable.orange);
            fruitList.add(orange);
            Fruit watermelon = new Fruit("watermelon",R.drawable.watermelon);
            fruitList.add(watermelon);
            Fruit pear= new Fruit("pear",R.drawable.pear);
            fruitList.add(pear);
            Fruit grape = new Fruit("grape",R.drawable.grape);
            fruitList.add(grape);
            Fruit strawberry = new Fruit("strawberry",R.drawable.stawberry);
            fruitList.add(strawberry);
            Fruit cherry = new Fruit("cherry",R.drawable.cherry);
            fruitList.add(cherry);
            Fruit mango = new Fruit("mango",R.drawable.mango);
            fruitList.add(mango);
            Fruit coconut = new Fruit("coconut",R.drawable.coconut);
            fruitList.add(coconut);
            Fruit tomato = new Fruit("tomato",R.drawable.tomato);
            fruitList.add(tomato);
            Fruit lemon = new Fruit("lemon",R.drawable.lemon);
            fruitList.add(lemon);
        }
    }

}

The effect is as follows:

3. Implement the click event of ListView

  • Use the setOnItemListener method to set the click event for the scroll menu
  • The i in the onItemClick method parameter is the specific element clicked
  • Use the toast method to display the clicked fruit name in a pop-up window

Add the following code in the main activity 

 code:

package cn.edu.sdut.listview;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {


    private List<Fruit> fruitList = new ArrayList<>();

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

//为fruitList列表添加元素
        initFruits();

        FruitAdapter adapter = new FruitAdapter(MainActivity.this,R.layout.fruit_item,fruitList);
        ListView listView = (ListView) findViewById(R.id.list_view);
        listView.setAdapter(adapter);

//       设置点击事件
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Fruit fruit = fruitList.get(i);
                Toast.makeText(MainActivity.this,fruit.getName(), Toast.LENGTH_SHORT).show();
            }
        });


    }

//把水果名称和对应图片加入信息列表
    private void initFruits(){
//        为了让滚动列表中多显示一些元素,所以添加两遍重复的内容
        for(int i=0;i<2;i++){
            Fruit apple = new Fruit("apple",R.drawable.apple);
            fruitList.add(apple);
            Fruit banana = new Fruit("banana",R.drawable.banana);
            fruitList.add(banana);
            Fruit orange = new Fruit("orange",R.drawable.orange);
            fruitList.add(orange);
            Fruit watermelon = new Fruit("watermelon",R.drawable.watermelon);
            fruitList.add(watermelon);
            Fruit pear= new Fruit("pear",R.drawable.pear);
            fruitList.add(pear);
            Fruit grape = new Fruit("grape",R.drawable.grape);
            fruitList.add(grape);
            Fruit strawberry = new Fruit("strawberry",R.drawable.stawberry);
            fruitList.add(strawberry);
            Fruit cherry = new Fruit("cherry",R.drawable.cherry);
            fruitList.add(cherry);
            Fruit mango = new Fruit("mango",R.drawable.mango);
            fruitList.add(mango);
            Fruit coconut = new Fruit("coconut",R.drawable.coconut);
            fruitList.add(coconut);
            Fruit tomato = new Fruit("tomato",R.drawable.tomato);
            fruitList.add(tomato);
            Fruit lemon = new Fruit("lemon",R.drawable.lemon);
            fruitList.add(lemon);
        }
    }

}

Effect:

Guess you like

Origin blog.csdn.net/weixin_45662399/article/details/126593068