Android ListView - 从右侧滑入子视图?

发布于 2024-09-04 22:02:20 字数 678 浏览 6 评论 0原文

我有一个列表视图。当我在 ListView 中选择一个项目时,我希望有一个子视图从右侧滑入,就像在许多应用程序中那样。

我一直在寻找关于这个主题的教程,但我正在旋转。也许 Android 使用与“Subview”不同的术语?

这就是我最终要做的:

  1. 为子视图创建一个新类

  2. 使用 标签内

  3. 将其添加到主类(“lv”是 ListView)

    <块引用>

    lv.setOnItemClickListener(new OnItemClickListener() {
    公共无效onItemClick(AdapterView父级, 查看视图,int位置,长id){

     Intent myIntent = new Intent(view.getContext(),SubView.class);
    
               startActivityForResult(myIntent, 0);  
    
       } 
    

    });

I have a ListView. When I select an item in the ListView, I would like to have a Subview slide in from the right, the way that it does in many Apps.

I have been searching for tutorials on this topic but am spinning my wheels. Maybe Android uses a term different than "Subview" ?

Here's what I ended up doing:

  1. Create a New Class for the SubView

  2. Add the Subview class to the Manifest with <activity android:name=".SubViewClassName" /> inside the <application> tag

  3. Add this to the main Class ("lv" is the ListView)

    lv.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView parent,
    View view,int position, long id) {

           Intent myIntent = new Intent(view.getContext(),SubView.class);
    
               startActivityForResult(myIntent, 0);  
    
       } 
    

    });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

朱染 2024-09-11 22:02:20

最简单的事情就是让第二个 ListView 位于单独的 Activity 中。如果用户启用了交互动画(这是默认设置),那么您的第二个 ListView 将从右侧滑入。

The simplest thing is to have your second ListView be in a separate Activity. If the user has inter-activity animations enabled (and that is the default), then your second ListView will slide in from the right.

韬韬不绝 2024-09-11 22:02:20

为此,您需要使用 ViewFlipper

以下是设置 main.xml 文件的方法:

<?xml version="1.0" encoding="utf-8"?>
<ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/flipper" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:versionCode="1" android:versionName="1.0">
  <include android:id="@+id/first" layout="@layout/home_screen" />
  <include android:id="@+id/second" layout="@layout/info_screen" />
</ViewFlipper>

在本例中,两个视图的 xml 文件是 home_screen 和 info_screen。您可以随意命名它们。

在您的代码中,您需要将其放在 onCreate() 方法中:

flipper = (ViewFlipper) findViewById(R.id.flipper);

此外,您还需要在 onCreate() 方法下面使用这些方法。

    private Animation inFromRightAnimation() {

    Animation inFromRight = new TranslateAnimation(
            Animation.RELATIVE_TO_PARENT,  +1.0f, Animation.RELATIVE_TO_PARENT,  0.0f,
            Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,   0.0f
    );
    inFromRight.setDuration(800);
    inFromRight.setInterpolator(new AccelerateInterpolator());
    return inFromRight;
}
private Animation outToLeftAnimation() 
{
    Animation outtoLeft = new TranslateAnimation(
            Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,  -1.0f,
            Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,   0.0f
    );
    outtoLeft.setDuration(800);
    outtoLeft.setInterpolator(new AccelerateInterpolator());
    return outtoLeft;
}
private Animation inFromLeftAnimation() {
    Animation inFromLeft = new TranslateAnimation(
            Animation.RELATIVE_TO_PARENT,  -1.0f, Animation.RELATIVE_TO_PARENT,  0.0f,
            Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,   0.0f
    );
    inFromLeft.setDuration(800);
    inFromLeft.setInterpolator(new AccelerateInterpolator());
    return inFromLeft;
}
private Animation outToRightAnimation() {
    Animation outtoRight = new TranslateAnimation(
            Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,  +1.0f,
            Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,   0.0f
    );
    outtoRight.setDuration(800);
    outtoRight.setInterpolator(new AccelerateInterpolator());
    return outtoRight;
}   

当你准备好后,只需使用

flipper.setInAnimation(inFromRightAnimation());
flipper.setOutAnimation(outToRightAnimation());
flipper.showNext();   

For this to work you will need to use a ViewFlipper

Here is how you would set up your main.xml file:

<?xml version="1.0" encoding="utf-8"?>
<ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/flipper" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:versionCode="1" android:versionName="1.0">
  <include android:id="@+id/first" layout="@layout/home_screen" />
  <include android:id="@+id/second" layout="@layout/info_screen" />
</ViewFlipper>

The xml files for the two views are home_screen and info_screen in this case. You can name them anything you'd like.

In your code you will need to place this in your onCreate() method:

flipper = (ViewFlipper) findViewById(R.id.flipper);

Additionally you need these methods below your onCreate() method.

    private Animation inFromRightAnimation() {

    Animation inFromRight = new TranslateAnimation(
            Animation.RELATIVE_TO_PARENT,  +1.0f, Animation.RELATIVE_TO_PARENT,  0.0f,
            Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,   0.0f
    );
    inFromRight.setDuration(800);
    inFromRight.setInterpolator(new AccelerateInterpolator());
    return inFromRight;
}
private Animation outToLeftAnimation() 
{
    Animation outtoLeft = new TranslateAnimation(
            Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,  -1.0f,
            Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,   0.0f
    );
    outtoLeft.setDuration(800);
    outtoLeft.setInterpolator(new AccelerateInterpolator());
    return outtoLeft;
}
private Animation inFromLeftAnimation() {
    Animation inFromLeft = new TranslateAnimation(
            Animation.RELATIVE_TO_PARENT,  -1.0f, Animation.RELATIVE_TO_PARENT,  0.0f,
            Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,   0.0f
    );
    inFromLeft.setDuration(800);
    inFromLeft.setInterpolator(new AccelerateInterpolator());
    return inFromLeft;
}
private Animation outToRightAnimation() {
    Animation outtoRight = new TranslateAnimation(
            Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,  +1.0f,
            Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,   0.0f
    );
    outtoRight.setDuration(800);
    outtoRight.setInterpolator(new AccelerateInterpolator());
    return outtoRight;
}   

And when you're ready simply use

flipper.setInAnimation(inFromRightAnimation());
flipper.setOutAnimation(outToRightAnimation());
flipper.showNext();   
離人涙 2024-09-11 22:02:20

也许我错了,但您可以在此子活动中使用 overridePendingTransition 吗?

在您的主要活动中:

public void onItemSelected(String id) {
    Intent myIntent= new Intent(this, MySubActivity.class);
        myIntent.putExtra("param1", param1); // some parameters
        startActivityForResult(myIntent, 0);
}

或您想要的任何代码。主要的事情是在您的子活动(和子视图)中,如 MySubActivity 中:

protected void onCreate(Bundle savedInstanceState) {
    ...
    // autogenerated code and your code here
    ...
    this.overridePendingTransition(R.anim.enter_from_right, R.anim.exit_to_left);
 }

以下是 res/anim 文件夹中的动画文件

Enter_from_right.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
    <translate
        android:fromXDelta="100%" android:toXDelta="0%"
        android:fromYDelta="0%" android:toYDelta="0%"
        android:duration="700"/>
</set>

exit_to_left.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
    <translate
        android:fromXDelta="0%" android:toXDelta="-100%"
        android:fromYDelta="0%" android:toYDelta="0%"
        android:duration="700"/>
</set>

希望有所帮助。

Maybe I'm wrong, but you can use an overridePendingTransition in this sub activity ?

In your main activity :

public void onItemSelected(String id) {
    Intent myIntent= new Intent(this, MySubActivity.class);
        myIntent.putExtra("param1", param1); // some parameters
        startActivityForResult(myIntent, 0);
}

Or whatever code you want. The main thing is in your subactivity (and subview) like in MySubActivity :

protected void onCreate(Bundle savedInstanceState) {
    ...
    // autogenerated code and your code here
    ...
    this.overridePendingTransition(R.anim.enter_from_right, R.anim.exit_to_left);
 }

Here are the animation files in the res/anim folder

enter_from_right.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
    <translate
        android:fromXDelta="100%" android:toXDelta="0%"
        android:fromYDelta="0%" android:toYDelta="0%"
        android:duration="700"/>
</set>

exit_to_left.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
    <translate
        android:fromXDelta="0%" android:toXDelta="-100%"
        android:fromYDelta="0%" android:toYDelta="0%"
        android:duration="700"/>
</set>

Hope that helps.

薄情伤 2024-09-11 22:02:20

只是对 Ryan 的回应(使用 ViewFlipper)的一小部分补充:

public void onShowRight(View v) {
    flipper.setInAnimation(inFromRightAnimation());
    flipper.setOutAnimation(outToLeftAnimation());
    flipper.showNext();         
}

public void onShowLeft(View v) {
    flipper.setInAnimation(inFromLeftAnimation());
    flipper.setOutAnimation(outToRightAnimation());
    flipper.showPrevious();         
}

Just small addition to Ryan's response (Using ViewFlipper):

public void onShowRight(View v) {
    flipper.setInAnimation(inFromRightAnimation());
    flipper.setOutAnimation(outToLeftAnimation());
    flipper.showNext();         
}

public void onShowLeft(View v) {
    flipper.setInAnimation(inFromLeftAnimation());
    flipper.setOutAnimation(outToRightAnimation());
    flipper.showPrevious();         
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文