赞
踩
implementation 'androidx.recyclerview:recyclerview:1.2.0'
- <?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">
-
- <androidx.recyclerview.widget.RecyclerView
- android:id="@+id/recycler_view"
- android:layout_width="match_parent"
- android:layout_height="match_parent" />
-
- </RelativeLayout>
- package com.example.recyclerviewapplication
-
- import android.os.Bundle
- import androidx.appcompat.app.AppCompatActivity
- import androidx.recyclerview.widget.GridLayoutManager
- import androidx.recyclerview.widget.RecyclerView
- import java.security.AccessController.getContext
-
-
- class MainActivity : AppCompatActivity() {
-
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_main)
-
- val data: MutableList<String> = ArrayList()
- for (i in 0..200){
- data.add("Item $i")
- }
-
- val adapter = MyAdapter(
- this,
- data
- )
-
- val recyclerView: RecyclerView =
- findViewById(R.id.recycler_view)
-
- recyclerView.layoutManager = GridLayoutManager(
- this,
- 1,
- GridLayoutManager.HORIZONTAL,
- false
- )
-
- recyclerView.adapter = adapter
-
- }
- }
- package com.example.recyclerviewapplication;
-
- import android.content.Context;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.TextView;
-
- import androidx.annotation.NonNull;
- import androidx.recyclerview.widget.RecyclerView;
-
- import java.util.List;
-
- public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
-
- private List<String> mData;
-
- public MyAdapter(Context context, List<String> data) {
- mData = data;
- }
-
- @NonNull
- @Override
- public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
- View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout, parent, false);
- return new ViewHolder(view);
- }
-
- @Override
- public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
- holder.mText.setText(mData.get(position));
- }
-
- @Override
- public int getItemCount() {
- return mData.size();
- }
-
- static class ViewHolder extends RecyclerView.ViewHolder {
-
- TextView mText;
-
- ViewHolder(@NonNull View itemView) {
- super(itemView);
- mText = itemView.findViewById(R.id.textView);
- }
- }
- }
- <?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:padding="25dp"
- android:clipChildren="false">
-
-
- <TextView
- android:id="@+id/textView"
- android:layout_width="300px"
- android:padding="10dp"
- android:textSize="20dp"
- android:gravity="center"
- android:textColor="#fff"
- android:layout_height="wrap_content"/>
-
- <ImageView
- android:id="@+id/imageView"
- android:layout_width="300px"
- android:padding="5dp"
- android:background="#f00"
- android:layout_marginTop="60dp"
- android:layout_height="300px" />
-
-
-
- </RelativeLayout>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。