如何在 recyclerview 中使用 cardview?
本示例演示了如何在 recyclerview 中使用 cardview
步骤 1 − 在 Android Studio 中创建一个新项目,转到文件 ⇒ 新建项目,并填写所有必需的信息以创建一个新项目。
步骤 2 − 将以下代码添加到 res/layout/activity_main.xml 中。
<?xml version = "1.0" encoding = "utf-8"?> <android.support.design.widget.CoordinatorLayout android:layout_width = "match_parent" android:layout_height = "match_parent" xmlns:android = "http://schemas.android.com/apk/res/android" xmlns:app = "http://schemas.android.com/apk/res-auto"> <android.support.design.widget.AppBarLayout android:layout_width = "match_parent" android:layout_height = "wrap_content"> <android.support.v7.widget.Toolbar android:id = "@+id/appbarlayout_tool_bar" android:background = "@color/colorPrimary" android:layout_width = "match_parent" android:layout_height = "?attr/actionBarSize" app:layout_scrollFlags = "scroll|snap|enterAlways" app:theme = "@style/ThemeOverlay.AppCompat.Dark.ActionBar" app:popupTheme = "@style/ThemeOverlay.AppCompat.Light" /> </android.support.design.widget.AppBarLayout> <android.support.v7.widget.RecyclerView android:id = "@+id/recycler_view" android:layout_width = "match_parent" android:layout_height = "match_parent" app:layout_behavior = "@string/appbar_scrolling_view_behavior"/> </android.support.design.widget.CoordinatorLayout>
在上面的代码中,我们采用了应用程序栏布局和回收视图。
步骤 3 − 将以下代码添加到 src/MainActivity.java 中
<?xml version = "1.0" encoding = "utf-8"?> import android.annotation.TargetApi; import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences; import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.support.v4.content.pm.ShortcutInfoCompat; import android.support.v4.content.pm.ShortcutManagerCompat; import android.support.v4.graphics.drawable.IconCompat; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.DefaultItemAnimator; import android.support.v7.widget.DividerItemDecoration; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.View; import android.widget.TextView; import android.widget.Toast; import android.support.v7.widget.Toolbar; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { private RecyclerView recyclerView; private customAdapter mAdapter; TextView text; ArrayList<String> list = new ArrayList<>(); @TargetApi(Build.VERSION_CODES.LOLLIPOP) @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (android.support.v7.widget.Toolbar)findViewById(R.id.appbarlayout_tool_bar); toolbar.setTitle("This is toolbar."); setSupportActionBar(toolbar); recyclerView = (RecyclerView) findViewById(R.id.recycler_view); RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext()); recyclerView.setLayoutManager(mLayoutManager); recyclerView.setItemAnimator(new DefaultItemAnimator()); mAdapter = new customAdapter(this,list); recyclerView.setAdapter(mAdapter); recyclerView.addItemDecoration(new DividerItemDecoration(recyclerView.getContext(), DividerItemDecoration.VERTICAL)); list.add("sairamm"); list.add("Krishna"); list.add("prasad"); list.add("sairamm"); list.add("Krishna"); list.add("prasad"); list.add("sairamm"); list.add("Krishna"); list.add("prasad"); list.add("sairamm"); list.add("Krishna"); list.add("prasad"); list.add("Krishna"); list.add("prasad"); list.add("sairamm"); list.add("Krishna"); list.add("prasad"); list.add("sairamm"); list.add("Krishna"); list.add("prasad"); } }
步骤 4 − 将以下代码添加到 Manifest.xml 中
<?xml version = "1.0" encoding = "utf-8"?> <manifest xmlns:android = "http://schemas.android.com/apk/res/android" package = "com.example.myapplication"> <uses-permission android:name = "android.permission.INTERNET" /> <uses-permission android:name = "com.android.launcher.permission.INSTALL_SHORTCUT" /> <application android:allowBackup = "true" android:theme = "@style/AppTheme.NoActionBar" android:icon = "@mipmap/ic_launcher" android:label = "@string/app_name" android:roundIcon = "@mipmap/ic_launcher_round" android:supportsRtl = "true"> <activity android:name = ".MainActivity" android:configChanges = "keyboardHidden|orientation|screenSize"> <intent-filter> <action android:name = "android.intent.action.MAIN" /> <action android:name = "android.intent.action.CREATE_SHORTCUT" /> <category android:name = "android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
步骤 5 − 将以下代码添加到 customAdapter.java 中
<?xml version = "1.0" encoding = "utf-8"?> import android.content.Context; import android.support.annotation.NonNull; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import java.util.ArrayList; public class customAdapter extends RecyclerView.Adapter<customAdapter.MyViewHolder> { Context context; ArrayList<String> list; public class MyViewHolder extends RecyclerView.ViewHolder { public TextView title; public MyViewHolder(View view) { super(view); title = (TextView) view.findViewById(R.id.title); } } public customAdapter(Context context, ArrayList<String> list) { this.context = context; this.list = list; } @NonNull @Override public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_row, viewGroup, false); return new MyViewHolder(itemView); } @Override public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) { myViewHolder.title.setText(list.get(i)); } @Override public int getItemCount() { return list.size(); } }
步骤 6 − 将以下代码添加到 list_row.xml 中
<?xml version = "1.0" encoding = "utf-8"?> <android.support.v7.widget.CardView 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 = "wrap_content" app:cardBackgroundColor = "@color/colorAccent" app:cardElevation = "10dp" app:cardCornerRadius = "20dp" tools:context = ".MainActivity"> <LinearLayout android:layout_width = "match_parent" android:layout_height = "wrap_content" android:gravity = "center" android:orientation = "vertical"> <ImageView android:id = "@+id/imageView2" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:src = "@drawable/logo" /> <TextView android:id = "@+id/title" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:gravity = "center" android:textSize = "30sp" /> <TextView android:id = "@+id/textview2" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:gravity = "center" android:text = "Sairamkrishan" android:textSize = "30sp" /> </LinearLayout> </android.support.v7.widget.CardView>
让我们尝试运行你的应用程序。我假设你已将你的实际安卓移动设备与你的计算机连接。要通过 Android Studio 运行应用程序,请打开一个项目活动文件,然后单击工具栏上的运行 图标。选择移动设备作为选项,然后检查移动设备,该设备会显示你的默认屏幕 -
广告