How to build a Horizontal ListView with RecyclerView?

前端 未结 13 1026
小鲜肉
小鲜肉 2020-11-22 04:53

I need to implement a horizontal listview in my Android application. I did a bit of research and came across How can I make a horizontal ListView in Android? and Horizontal

13条回答
  •  不思量自难忘°
    2020-11-22 05:41

    Recycler View in Horizontal Dynamic.

    Recycler View Implementation

    RecyclerView musicList = findViewById(R.id.MusicList);
    
    // RecyclerView musiclist = findViewById(R.id.MusicList1);
    // RecyclerView musicLIST = findViewById(R.id.MusicList2);
    LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
    musicList.setLayoutManager(layoutManager);
    
    String[] names = {"RAP", "CH SHB", "Faheem", "Anum", "Shoaib", "Laiba", "Zoki", "Komal", "Sultan","Mansoob Gull"};
    musicList.setAdapter(new ProgrammingAdapter(names));'
    

    Adapter class for recycler view, in which there is is a view holder for holding view of that recycler

    public class ProgrammingAdapter 
         extendsRecyclerView.Adapter {
    
    private String[] data;
    
    public ProgrammingAdapter(String[] data)
    {
        this.data = data;
    }
    
    @Override
    public programmingViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.list_item_layout, parent, false);
    
        return new programmingViewHolder(view);
    }
    
    @Override
    public void onBindViewHolder(@NonNull programmingViewHolder holder, int position) {
        String title = data[position];
        holder.textV.setText(title);
    }
    
    @Override
    public int getItemCount() {
        return data.length;
    }
    
    public class programmingViewHolder extends RecyclerView.ViewHolder{
        ImageView img;
        TextView textV;
        public programmingViewHolder(View itemView) {
            super(itemView);
            img =  itemView.findViewById(R.id.img);
            textV =  itemView.findViewById(R.id.textt);
        }
    }
    

提交回复
热议问题