RecyclerView使用入门

RcyclerView是Android 5.0推出的,是support-v7包中的新组件

1.首先通过Andriod Studio导入包

implementation 'com.android.support:recyclerview-v7:28.0.0-rc01'

2.初始化RecyclerView

package com.example.administrator.recycleviewdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

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

public class MainActivity extends AppCompatActivity {

    private RecyclerView mRecyclerView ;
    DemosAdapter mDemosAdapter;
    private List mData;

    // 初始化测试数据
    private void initData() {
        mData = new ArrayList<>();
        for (int i=0;i<10;i++) {
            mData.add(i + " line ");
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initData();
        setContentView(R.layout.activity_main);
        mRecyclerView = findViewById(R.id.recyclerView);
        //RecyclerView的样式设置,纵向横向等
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);

        mRecyclerView.setLayoutManager(linearLayoutManager);
        mDemosAdapter = new DemosAdapter(this ,mData);
        mRecyclerView.setAdapter(mDemosAdapter);
    }
}

3. 写RecyclerView的Adapter

package com.example.administrator.recycleviewdemo;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

public class DemosAdapter extends RecyclerView.Adapter {

    Context mContext;
    List mLists;

    public DemosAdapter(Context context, List lists) {
        mContext = context;
        mLists = lists;
    }

    public void update(List list) {
        mLists = list;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public DemosHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.main_item,viewGroup,false);
        DemosHolder demosHolder = new DemosHolder(view);
        return demosHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull DemosHolder demosHolder, int position) {
        String data = mLists.get(position);
        demosHolder.textView.setText(data);
        demosHolder.textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.i("TAG",view.toString());
            }
        });
    }

    @Override
    public int getItemCount() {
        return mLists.size();
    }

    class DemosHolder extends RecyclerView.ViewHolder {
        TextView textView;
        public DemosHolder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.textView);
        }
    }

}

用到的xml文件:

activity_main.xml





    

    

main_item.xml




    

 

你可能感兴趣的:(Android)