RecyclerView全套方案系列二

目录

1、MainAdapter

2、item_layout

3、MainAdapterClickListener

4、MainActivity

5、activity_main

6、strings.xml


1、MainAdapter

public class MainAdapter extends RecyclerView.Adapter {

    private List mList;

    private OnItemClickListener onItemClickListener;

    public MainAdapter(List list) {
        mList = list;
    }

    public void setItemListener(OnItemClickListener listener) {
        this.onItemClickListener = listener;
    }

    @NonNull
    @Override
    public MainViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_name, parent, false);
        return new MainAdapter.MainViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull final MainViewHolder holder, int position) {
        String name = mList.get(position);
        TextView tv = holder.itemView.findViewById(R.id.rv_item_name_tv);
        tv.setText(name);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    int index = holder.getAdapterPosition();
                    onItemClickListener.onItemClick(v, holder, index);
                }
            }
        });
    }

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

    public interface OnItemClickListener {
        void onItemClick(View view, RecyclerView.ViewHolder holder, int position);
    }

    static class MainViewHolder extends RecyclerView.ViewHolder {
        MainViewHolder(@NonNull View itemView) {
            super(itemView);
        }
    }


}

2、item_layout




3、MainAdapterClickListener

public class MainAdapterClickListener implements MainAdapter.OnItemClickListener {
    @Override
    public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
        processClickView(view.getContext(), position);
    }

    private void processClickView(Context context, int position) {
        switch (position) {
            case 0:
                Toast.makeText(context, "111", Toast.LENGTH_SHORT).show();
                break;
            case 1:
                Toast.makeText(context, "222", Toast.LENGTH_SHORT).show();
                break;
            case 2:
                Toast.makeText(context, "333", Toast.LENGTH_SHORT).show();
                break;
            case 3:
                Toast.makeText(context, "444", Toast.LENGTH_SHORT).show();
                break;
        }
    }
}

4、MainActivity

public class MainActivity extends AppCompatActivity {

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

    private void initView() {
        RecyclerView rvMain = findViewById(R.id.rvMain);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        rvMain.setLayoutManager(layoutManager);
        DividerItemDecoration decoration = new DividerItemDecoration(this, DividerItemDecoration.HORIZONTAL);
        rvMain.addItemDecoration(decoration);
        MainAdapter adapter = new MainAdapter(getData());
        rvMain.setAdapter(adapter);
        adapter.setItemListener(new MainAdapterClickListener());
    }

    private List getData() {
        String[] name = getResources().getStringArray(R.array.rv_item_name);
        if (name == null || name.length == 0) {
            return Collections.emptyList();
        }
        return Arrays.asList(name);
    }
}

5、activity_main




    

6、strings.xml


    UserCenterModel

    
        111111111
        222222222
        333333333
        444444444
    

 

 

 

 

 

 

你可能感兴趣的:(RecyclerView)