Android:ListView和RecyclerView

Android:ListView

    • ListView
    • RecyclerView

ListView

Android:ListView和RecyclerView_第1张图片

<ListView
        android:id="@+id/hai"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    <TextView
        android:id="@+id/s"
        android:textSize="30sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
package com.example.mylinearlayout;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

public class MyAdapter extends BaseAdapter {

    private List<Bean> beanList;
    private Context context;

    public MyAdapter() {
    }

    public MyAdapter(List<Bean> beanList, Context context) {
        this.beanList = beanList;
        this.context = context;
    }

    @Override
    public int getCount() {
        return beanList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null)
        convertView = LayoutInflater.from(context).inflate(R.layout.item, parent, false);
        TextView textView = convertView.findViewById(R.id.s);
        textView.setText(beanList.get(position).getName());
        return convertView;
    }
}

package com.example.mylinearlayout;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    private List<Bean> beanList;

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

        for (int i = 0; i < 100; i++) {
            beanList.add(new Bean("helloworld"+i));
        }

        ListView listView = findViewById(R.id.hai);
        listView.setAdapter(new MyAdapter());
    }
}

RecyclerView

dependencies {
    implementation 'androidx.recyclerview:recyclerview:1.0.0'
}

在这里插入图片描述
Android:ListView和RecyclerView_第2张图片

你可能感兴趣的:(Android,android,android,studio,java)