如何在Android中加载带有动画的cardview GridView?

问题描述 投票:0回答:3

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

我的应用程序的功能工作正常,图像显示在片段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);

        }
    }
}

以下是我的要求示例:

enter image description here

我的gridview看起来像这样:

My gridview looks like this

android android-layout gridview cardview
3个回答
0
投票

在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;
        }
    }
}

0
投票

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

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


0
投票

将此Recyclerview与Load动画一起使用

Recyclerview Load Animation

© www.soinside.com 2019 - 2024. All rights reserved.