ListView和RecyclerView的简单使用

这篇博客介绍了Android开发中ListView和RecyclerView的基本使用,包括TabLayout、ViewPager和Fragment的结合。内容涵盖相关布局文件的配置和Java代码实现,如activity_main.xml、fragment_list.xml、FragmentList.java等,并讨论了适配器的使用。
摘要由CSDN通过智能技术生成

ListView和RecyclerView是Android开发中常用的两个模块,本文来简单介绍一下它们的基本使用方法,主要模块:

1. 效果

2. 文件目录

3. 配置

由于使用了Material Design中的TabLayout,需要在app文件夹下build.gradle中的 dependencies声明一些依赖

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    // Material Design
    implementation 'com.google.android.material:material:1.1.0'
    // RecyclerView
    implementation 'androidx.recyclerview:recyclerview:1.1.0'
    testImplementation 'junit:junit:4.13'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

}

4. layout布局

4.1 activity_main.xml

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

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/main_tab"
        android:layout_width="match_parent"
        android:layout_height="36dp" />

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/main_viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

4.2 fragment_list.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"
    android:orientation="vertical">

    <ListView
        android:id="@+id/frag_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

4.3 fragment_recycler.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="wrap_content">

        <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/frag_recyc"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

4.4 item_layout.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="wrap_content"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/item_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/todo"
        android:src="@drawable/test_img" />

    <TextView
        android:id="@+id/item_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/this_is_text"
        android:textSize="36sp" />


</LinearLayout>

5. Java文件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值