📜  如何在Android的RecyclerView中添加淡入淡出和收缩动画?

📅  最后修改于: 2021-05-08 20:50:45             🧑  作者: Mango

在本文中,我们将在RecyclerView中显示淡入淡出和收缩动画。当我们向下移动时,顶部的项目将逐渐消失,然后收缩。在输出中,我们可以看到它是如何发生的。我们将实现这种Java编程语言。

分步实施

步骤1:创建一个新项目

要在Android Studio中创建新项目,请参阅如何在Android Studio中创建/启动新项目。请注意,选择Java作为编程语言。

步骤2:将其添加到build.gradle文件中

implementation 'com.stone.vega.library:VegaLayoutManager:1.0.6'

步骤3:使用items.xml文件

转到应用程序> res>布局>新建>布局资源文件,并将文件命名为item 。转到item.xml文件,并参考以下代码。以下是item.xml文件的代码。

XML


  
    


Java
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
  
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
  
public class RecyclerViewAdapter extends RecyclerView.Adapter {
  
    private int mcount;
  
    public RecyclerViewAdapter(int mcount) {
        this.mcount = mcount;
    }
      
    @NonNull
    @Override
    public RecyclerViewAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.items, parent, false);
        return new ViewHolder(view);
    }
  
    @Override
    public void onBindViewHolder(@NonNull final RecyclerViewAdapter.ViewHolder holder, final int position) {
        holder.itemclick.setText("Button" + position);
        holder.itemclick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(holder.itemView.getContext(), "Clicked : " + position, Toast.LENGTH_LONG).show();
            }
        });
    }
  
    @Override
    public int getItemCount() {
        return mcount;
    }
  
    public class ViewHolder extends RecyclerView.ViewHolder {
        Button itemclick;
  
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            itemclick = itemView.findViewById(R.id.itemclick);
        }
    }
}


XML


  
    
  


Java
import android.os.Bundle;
  
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
  
import com.pluscubed.recyclerfastscroll.RecyclerFastScroller;
import com.stone.vega.library.VegaLayoutManager;
  
public class MainActivity extends AppCompatActivity {
  
    RecyclerView recyclerView;
    RecyclerFastScroller fastScroller;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerView = findViewById(R.id.rcv);
          
        // We have used this to add fade and shrink method
        recyclerView.setLayoutManager(new VegaLayoutManager());
        recyclerView.setAdapter(new RecyclerViewAdapter(50));
    }
}


步骤4:使用RecyclerViewAdapter。 Java文件

在android studio中创建一个新的Java类,并将该类命名为RecyclerViewAdapter。转到RecyclerViewAdapter。 Java文件并参考以下代码。下面是RecyclerViewAdapter的代码。 Java文件。

Java

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
  
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
  
public class RecyclerViewAdapter extends RecyclerView.Adapter {
  
    private int mcount;
  
    public RecyclerViewAdapter(int mcount) {
        this.mcount = mcount;
    }
      
    @NonNull
    @Override
    public RecyclerViewAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.items, parent, false);
        return new ViewHolder(view);
    }
  
    @Override
    public void onBindViewHolder(@NonNull final RecyclerViewAdapter.ViewHolder holder, final int position) {
        holder.itemclick.setText("Button" + position);
        holder.itemclick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(holder.itemView.getContext(), "Clicked : " + position, Toast.LENGTH_LONG).show();
            }
        });
    }
  
    @Override
    public int getItemCount() {
        return mcount;
    }
  
    public class ViewHolder extends RecyclerView.ViewHolder {
        Button itemclick;
  
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            itemclick = itemView.findViewById(R.id.itemclick);
        }
    }
}

步骤5:使用activity_main.xml文件

导航到应用程序> res>布局> activity_main.xml,然后将以下代码添加到该文件中。以下是activity_main.xml文件的代码。

XML格式



  
    
  

步骤6:使用MainActivity。 Java文件

转到MainActivity。 Java文件并参考以下代码。下面是MainActivity的代码。 Java文件。在代码内部添加了注释,以更详细地了解代码。

Java

import android.os.Bundle;
  
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
  
import com.pluscubed.recyclerfastscroll.RecyclerFastScroller;
import com.stone.vega.library.VegaLayoutManager;
  
public class MainActivity extends AppCompatActivity {
  
    RecyclerView recyclerView;
    RecyclerFastScroller fastScroller;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerView = findViewById(R.id.rcv);
          
        // We have used this to add fade and shrink method
        recyclerView.setLayoutManager(new VegaLayoutManager());
        recyclerView.setAdapter(new RecyclerViewAdapter(50));
    }
}

输出: