📜  如何在 Android App 中使用 Android 滑动活动库?

📅  最后修改于: 2022-05-13 01:55:00.030000             🧑  作者: Mango

如何在 Android App 中使用 Android 滑动活动库?

滑动活动允许您轻松地将标题内容、菜单和数据设置到可滑动屏幕上。轻松创建可以在屏幕上垂直滑动并非常适合材料设计时代的活动。我们可以为我们的滑动活动设置标题图像。我们还可以自定义会影响标题和状态栏的颜色。我们还可以禁用标题并仅显示可读的内容。下面给出了一个示例 GIF,以了解我们将在本文中做什么。请注意,我们将使用Java语言来实现这个项目。

在 Android 应用示例 GIF 中使用 Android 滑动活动库

分步实施

第 1 步:创建一个新项目

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

第二步:添加依赖



导航到Gradle Scripts > build.gradle(Module:app)并在依赖项部分添加以下依赖项。

第 3 步:为滑块活动创建 XML。这将在滑块活动中显示为内容

XML


XML

  
    
  
    
  
        
  
            


Java
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
  
import com.klinker.android.sliding.SlidingActivity;
  
public class ImageActivity extends SlidingActivity {
    @Override
    public void init(Bundle savedInstanceState) {
        setTitle(R.string.image_activity);
        setContent(R.layout.activity_content);
  
        // no need to set a color here, palette
          // will generate colors for us to be set
        setImage(R.drawable.profile_picture);
  
         // if we wanted to set some manually instead,
           // do this after setting the image
         setPrimaryColors(
                 getResources().getColor(R.color.image_activity_primary),
                 getResources().getColor(R.color.image_activity_primary_dark)
         );
  
         // if we want the image to animate in, then set it after
           // the activity has been created
         // NOTE: this will not change the activity's colors using palette,
           // so make sure you call
         // setPrimaryColors() first
         new Handler().postDelayed(new Runnable() {
             @Override
             public void run() {
                 setImage(R.drawable.profile_picture);
             }
         }, 500);
  
        Intent intent = getIntent();
        if (intent.getBooleanExtra(MainActivity.ARG_USE_EXPANSION, false)) {
            expandFromPoints(
                    intent.getIntExtra(MainActivity.ARG_EXPANSION_LEFT_OFFSET, 0),
                    intent.getIntExtra(MainActivity.ARG_EXPANSION_TOP_OFFSET, 0),
                    intent.getIntExtra(MainActivity.ARG_EXPANSION_VIEW_WIDTH, 0),
                    intent.getIntExtra(MainActivity.ARG_EXPANSION_VIEW_HEIGHT, 0)
            );
        }
    }
}


Java
import androidx.appcompat.app.AppCompatActivity;
  
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
  
public class MainActivity extends AppCompatActivity {
  
    public static final String ARG_USE_EXPANSION = "arg_use_expansion";
    public static final String ARG_EXPANSION_LEFT_OFFSET = "arg_left_offset";
    public static final String ARG_EXPANSION_TOP_OFFSET = "arg_top_offset";
    public static final String ARG_EXPANSION_VIEW_WIDTH = "arg_view_width";
    public static final String ARG_EXPANSION_VIEW_HEIGHT = "arg_view_height";
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
  
        setContentView(R.layout.activity_main);
  
        // registering click event
        findViewById(R.id.show_image).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getApplicationContext(), ImageActivity.class));
            }
        });
    }
}


第 4 步:为 MainActivity 创建 XML。这包含一个按钮。单击它后滑块活动将打开

XML


  
    
  
    
  
        
  
            

第 5 步:我们将使用 init() 方法,而不是使用onCreate() 方法。

A)创建一个名为ImageActivity的活动。使用setImage(R.drawable.profile_picture)配置图像

B) expandFromPoints(Arg1,Arg2,Arg3,Arg4)方法用于在活动打开时创建动画。

Java

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
  
import com.klinker.android.sliding.SlidingActivity;
  
public class ImageActivity extends SlidingActivity {
    @Override
    public void init(Bundle savedInstanceState) {
        setTitle(R.string.image_activity);
        setContent(R.layout.activity_content);
  
        // no need to set a color here, palette
          // will generate colors for us to be set
        setImage(R.drawable.profile_picture);
  
         // if we wanted to set some manually instead,
           // do this after setting the image
         setPrimaryColors(
                 getResources().getColor(R.color.image_activity_primary),
                 getResources().getColor(R.color.image_activity_primary_dark)
         );
  
         // if we want the image to animate in, then set it after
           // the activity has been created
         // NOTE: this will not change the activity's colors using palette,
           // so make sure you call
         // setPrimaryColors() first
         new Handler().postDelayed(new Runnable() {
             @Override
             public void run() {
                 setImage(R.drawable.profile_picture);
             }
         }, 500);
  
        Intent intent = getIntent();
        if (intent.getBooleanExtra(MainActivity.ARG_USE_EXPANSION, false)) {
            expandFromPoints(
                    intent.getIntExtra(MainActivity.ARG_EXPANSION_LEFT_OFFSET, 0),
                    intent.getIntExtra(MainActivity.ARG_EXPANSION_TOP_OFFSET, 0),
                    intent.getIntExtra(MainActivity.ARG_EXPANSION_VIEW_WIDTH, 0),
                    intent.getIntExtra(MainActivity.ARG_EXPANSION_VIEW_HEIGHT, 0)
            );
        }
    }
}

第 6 步:创建 Button 对象。注册点击监听器。在 onClick() 方法中启动滑块活动。(ImageActivity.class)

Java

import androidx.appcompat.app.AppCompatActivity;
  
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
  
public class MainActivity extends AppCompatActivity {
  
    public static final String ARG_USE_EXPANSION = "arg_use_expansion";
    public static final String ARG_EXPANSION_LEFT_OFFSET = "arg_left_offset";
    public static final String ARG_EXPANSION_TOP_OFFSET = "arg_top_offset";
    public static final String ARG_EXPANSION_VIEW_WIDTH = "arg_view_width";
    public static final String ARG_EXPANSION_VIEW_HEIGHT = "arg_view_height";
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
  
        setContentView(R.layout.activity_main);
  
        // registering click event
        findViewById(R.id.show_image).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getApplicationContext(), ImageActivity.class));
            }
        });
    }
}

输出:

项目链接:点此