美文网首页
Scrollview+gridview替代方案

Scrollview+gridview替代方案

作者: 程序猿的小生活 | 来源:发表于2021-10-11 15:23 被阅读0次

    1.布局文件

     <android.support.v4.widget.NestedScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent">
            <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
            <android.support.v7.widget.RecyclerView
                android:id="@+id/gv"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:numColumns="3"
                android:paddingLeft="15dp"
                android:paddingRight="15dp" />
            </LinearLayout>
            </android.support.v4.widget.NestedScrollView>
    

    2.activity代码

             gv = findViewById(R.id.gv);
             gv.setNestedScrollingEnabled(false);
             GridLayoutManager layoutManage = new 
             GridLayoutManager(wylzgzryactivity.this, 3);//3代表一行3列
             gv.setLayoutManager(layoutManage);
             Adapter adapter = new Adapter(this,hysyyMenuBeanList);//点击事件
            gv.setAdapter(adapter);
            adapter.notifyDataSetChanged();
            adapter.setOnItemClickListener(new Adapter.OnItemClickListener() {
                @Override
                public void setOnclickItem(View view, int position) {
         }
                }
            });
    

    3.adapter 代码

    public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {
        Context mcontext;
        List<hysyyMenuBean> pllist;
        private int num, service;
        private OnItemClickListener onItemOnclick;;
        public OldWYLVZHIAdapter(Context context, List<hysyyMenuBean> pllist) {
                this.mcontext = context;
                this.pllist = pllist;
    
        }
    
        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
           View view = View.inflate(mcontext, R.layout.item_hysyymenu, null);
           ViewHolder holder = new ViewHolder(view);
            return holder;
        }
    
        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder,int position) {
            hysyyMenuBean hysyyMenuBean = pllist.get(position);
    
            holder.name.setText(hysyyMenuBean.getName());
            String code = hysyyMenuBean.getCode();
      holder.all.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (onItemOnclick!=null){
                        onItemOnclick.setOnclickItem(view,holder.getAdapterPosition());
                    }
                }
            });
        }
    
        @Override
        public int getItemCount() {
            return pllist.size();
        }
    
    
        static class ViewHolder extends RecyclerView.ViewHolder{
            TextView name;
            TextView shenhesum_view;
            RelativeLayout all;
            public ViewHolder (View rootView)
            {
                super(rootView);
                name = rootView.findViewById(R.id.name);
                shenhesum_view = rootView.findViewById(R.id.shenhesum_view);
                all = rootView.findViewById(R.id.all);
            }
    
        }
    
        public  interface OnItemClickListener{
            void setOnclickItem(View view, int position);
        }
        public void setOnItemClickListener(OnItemClickListener onItemOnclick) {
            this.onItemOnclick = onItemOnclick;
        }
    

    4.item_hysyymenu布局

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        android:id="@+id/all"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
       >
    
            <TextView
                android:drawablePadding="3dp"
                android:layout_marginTop="35dp"
                android:id="@+id/name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentStart="true"
                android:layout_alignParentTop="true"
                android:layout_gravity="center"
                android:drawableTop="@drawable/hysyy_yysh1"
                android:gravity="center_horizontal"
                android:src="@drawable/nodata"
                android:textColor="#000"
                android:textSize="14sp" />
    
        <TextView
            android:layout_marginTop="35dp"
            android:id="@+id/shenhesum_view"
            android:layout_width="23.5dp"
            android:layout_height="21dp"
            android:layout_alignParentEnd="true"
            android:layout_marginEnd="17dp"
            android:layout_alignParentTop="true"
            android:background="@drawable/jb"
            android:gravity="center"
            android:text="0"
            android:textColor="@color/color_white"
            android:textSize="10sp" />
    </RelativeLayout>
    

    相关文章

      网友评论

          本文标题:Scrollview+gridview替代方案

          本文链接:https://www.haomeiwen.com/subject/lciloltx.html