Android RecyclerView使用

先添加远程依赖

compile 'com.android.support:recyclerview-v7:27.1.1'

MainActivity

public class MainActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private RecyclerAdapter adapter;
    private List<Person> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerView=findViewById(R.id.recyclerview);

        //设置横向
        LinearLayoutManager layoutManager= new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView.setLayoutManager(layoutManager);

        getData();
        adapter = new RecyclerAdapter(this,list);
        recyclerView.setAdapter(adapter);
    }

    public List<Person> getData(){
        list = new ArrayList<Person>();
        for(int i=1;i<=15;i++){
            Person p = new Person(R.drawable.ic_launcher,i+"","我是第"+i);
            list.add(p);
        }
        return list;
    }
}

RecyclerAdapter

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder>{
    private List<Person> list;
    private Context context;


    static class ViewHolder extends RecyclerView.ViewHolder{
        ImageView imageView;
        TextView title;
        TextView content;
        View view;

        public ViewHolder(View itemView) {
            super(itemView);
            view=itemView;
            imageView=itemView.findViewById(R.id.imageview);
            title=itemView.findViewById(R.id.title1);
            content=itemView.findViewById(R.id.content);
        }
    }

    public RecyclerAdapter(Context context,List<Person> list){
        this.context=context;
        this.list=list;
    }


    @NonNull
    @Override
    public RecyclerAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.item,parent,false);
        View view= LinearLayout.inflate(parent.getContext(),R.layout.item,null);
        final ViewHolder holder = new ViewHolder(view);
        holder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getAdapterPosition();
                Person person = list.get(position);
                Toast.makeText(v.getContext(),person.mContent,Toast.LENGTH_SHORT).show();
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerAdapter.ViewHolder holder, int position) {
        //控件设置值
        Person person = list.get(position);
        holder.imageView.setBackgroundResource(person.mPicture);
        holder.title.setText(person.mTitle);
        holder.content.setText(person.mContent);
    }

    @Override
    public int getItemCount() {
        if(list==null){
            return 0;
        }
        return list.size();
    }
}

你可能感兴趣的:(安卓)