【问题标题】:How to load cardview GridView with Animations in Android?如何在 Android 中加载带有动画的 cardview GridView?
【发布时间】:2019-01-15 15:52:29
【问题描述】:

我已经创建了类似画廊的 android 应用程序,其中有一些使用 gridview 片段的图像。

我的应用程序的功能工作正常,图像显示在片段网格视图中,当我单击项目时我的第二个活动打开,但我想显示带有动画的 cardview GridView。

此外,如果使用默认图像代替加载图像而不是进度条更容易。

这是我的应用程序片段代码:

public class CallFragment extends Fragment {

    GridView grid;
        String[] plan = {
                "Student Bundle",
                "SMS Plus Bundle",
                "Instagram",
                "Facebook",
                "Flickr"

        } ;
        String[] web = {
                "*3000",
                "*106*1",
                "Instagram",
                "Facebook",
                "Flickr"

        } ;
        int[] imageId = {
                R.drawable.calla,
                R.drawable.smsb,
                R.drawable.person7,
                R.drawable.person7,
                R.drawable.person7

        };
        int[] fullimg = {
                R.drawable.callaa,
                R.drawable.smsbb,
                R.drawable.person7,
                R.drawable.person7,
                R.drawable.person7

        };

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            setHasOptionsMenu(true);

            View view = inflater.inflate(R.layout.fragment_call,container,false);

            CustomGrid adapter = new CustomGrid(getActivity(), plan, web, imageId, fullimg);

            grid=(GridView) view.findViewById(R.id.grid);
            grid.setAdapter(adapter);
            grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                    String value=(String)grid.getItemAtPosition(i);
                    Intent intent=new Intent(getActivity(),Book.class);
                    intent.putExtra("web", web[i]);
                    intent.putExtra("plan", plan[i]);
                    intent.putExtra("imageId", imageId[i]);
                    intent.putExtra("fullimg", fullimg[i]);
                    startActivity(intent);

                }
            });

            return view;
        }

这是我的应用程序 CustomAdapter 代码:

public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MyViewHolder> {

    ArrayList<String> personNames;
    ArrayList<String> personCode;
    ArrayList<Integer> personImages;
    ArrayList<Integer> personImages1;
    Context context;

    public CustomAdapter(Context context, ArrayList<String> personNames, ArrayList<String> personCode, ArrayList<Integer> personImages, ArrayList<Integer> personImages1) {
        this.context = context;
        this.personCode = personCode;
        this.personNames = personNames;
        this.personImages = personImages;
        this.personImages1 = personImages1;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // infalte the item Layout
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.rowlayout, parent, false);
        // set the view's size, margins, paddings and layout parameters
        MyViewHolder vh = new MyViewHolder(v); // pass the view to View Holder
        return vh;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        // set the data in items
        //holder.name.setText(personNames.get(position));
        holder.image.setImageResource(personImages.get(position));
        //holder.image.setImageResource(personImages1.get(position));
        // implement setOnClickListener event on item view.
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // open another activity on item click
                Intent intent = new Intent(context, SecondActivity.class);
                //intent.putExtra("image", personImages.get(position)); // put image data in Intent
                intent.putExtra("name", personNames.get(position)); // put image data in Intent
                intent.putExtra("code", personCode.get(position)); // put image data in Intent
                intent.putExtra("image", personImages1.get(position)); // put image data in Intent
                context.startActivity(intent); // start Intent
            }
        });

    }


    @Override
    public int getItemCount() {
        return personNames.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        // init the item view's
        TextView name;
        TextView code;
        ImageView image;

        public MyViewHolder(View itemView) {
            super(itemView);

            // get the reference of item view's
            name = (TextView) itemView.findViewById(R.id.name);
            code = (TextView) itemView.findViewById(R.id.code);
            image = (ImageView) itemView.findViewById(R.id.image);

        }
    }
}

这是我的要求示例:

我的网格视图看起来像这样:

【问题讨论】:

    标签: android android-layout gridview cardview


    【解决方案1】:

    在anim下创建一个单独的xml文件,应用在onBindViewHolder中。

     @Override
        public void onBindViewHolder(ViewHolder holder, int position)
        {
    
    
            // Here you apply the animation when the view is bound
            setAnimation(holder.itemView, position);
        }
    
        /**
         * Here is the key method to apply the animation
         */
        private void setAnimation(View viewToAnimate, int position)
        {.             int lastPosition=0;
    
            // If the bound view wasn't previously displayed on screen, it's animated
            if (position > lastPosition)
            {
                Animation animation = AnimationUtils.loadAnimation(context, android.R.anim.slide_in_left);
                viewToAnimate.startAnimation(animation);
                lastPosition = position;
            }
        }
    }
    

    【讨论】:

    • 我可以像这样使用吗:schemas.android.com/apk/res/android" >
    • 你能告诉我你在代码中使用的xml名称吗
    • slide_in_left xml 名称
    • 如果这有帮助,请检查这个作为答案。我会对别人有用
    • 创建了一个单独的 XML 名称:animation_style 和您的代码添加到我的 CustomAdapter 然后显示错误无法解析符号 'lastposition'
    【解决方案2】:

    您可以使用this tutorial 在 RecyclerView 上应用动画。

    对于默认图像,只需在 R.layout.rowlayout XML 文件中的 imageview 中添加一个 src 属性

    【讨论】:

      【解决方案3】:

      将此 Recyclerview 与加载动画一起使用

      Recyclerview Load Animation

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-29
        • 2015-10-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多