Collapsing ToolBar Layout with viewpager

前端 未结 5 1120
旧时难觅i
旧时难觅i 2021-02-02 12:43

I am using CollapsingBarLayout with viewpager and the fragments of viewpager are having listview, gridview.

Here is my code:

    

        
5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-02 13:02

    I had this issue as well. You'll need to used RecyclerView. Now I understand you must use ListView and GridGiew, but you can use that within a RecyclerView.

    For example, I'll show you how to implement a GridLayout within in a RecyclerView.

    In your fragment layout (that you'd want to implement GridView in), add the following XML

    
    

    Then on your fragment java,

    on your fragment onCreateView, define the RecyclerView,

    RecyclerView rv = (RecyclerView) v.findViewById(R.id.recyclerviewGRID);
    setupRecyclerView(rv);
    

    Create the method setupRecyclerView,

     private void setupRecyclerView(RecyclerView recyclerView) {
            recyclerView.setHasFixedSize(true);
    
                recyclerView.setLayoutManager(new GridLayoutManager(recyclerView.getContext(), 2));
                mAdapter = new AdapterGridView(getActivity().getApplicationContext(), mItems);
                recyclerView.setAdapter(mAdapter);
    
        }
    

    You must define mAdapter as RecyclerView.Adapter first. Then create your the adapter for the RecyclerView. Note that, mItems is an ArrayList that will contain contents of your list.

    AdapterGridView.java

    public class AdapterGridView extends RecyclerView.Adapter {
    
        ArrayList mItems;
        Context context, contxt;
    
        public AdapterGridView(Context context, ArrayList mItems) {
            this.context = context;
            this.mItems = mItems;
        }
    
        @Override
        public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    
    //attach your list item layout here, mine in this case is called list_item_grid_item
            View v = LayoutInflater.from(viewGroup.getContext())
                    .inflate(R.layout.list_item_grid_item, viewGroup, false);
            ViewHolder viewHolder = new ViewHolder(v);
            return viewHolder;
        }
    
        @Override
        public void onBindViewHolder(ViewHolder viewHolder, int i) {
    AdItem singleItem = mItems.get(i);
    
    //attach data to your layout as the following manner
    viewHolder.tvspecies.setText(singleItem.getName());
    
        }
    
        @Override
        public int getItemCount() {
    
    //return the number of list items
            return mItems.size();
        }
    
        class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
    
            public ImageView imgThumbnail, avatar;
            public TextView tvspecies, adIDLBL;
    
    
            public ViewHolder(View itemView) {
                super(itemView);
                itemView.setClickable(true);
                itemView.setOnClickListener(this);
    
    //define your list item views, basically the stuff in list_item_grid_item
                imgThumbnail = (ImageView)itemView.findViewById(R.id.img_thumbnail);
                tvspecies = (TextView)itemView.findViewById(R.id.tv_species);
    
            }
    
            @Override
            public void onClick(View v) {
                //handle item click events
            }
        }
    }
    

提交回复
热议问题