当前位置: 首页 > 工具软件 > iosched > 使用案例 >

iosched_Android UI:看一看iosched的开源应用程序

楚嘉玉
2023-12-01

iosched

因此,是的,开源是正确的,非常有用的,它可以帮助每个人学习如何做正确的事情(甚至只是学习更多有关某些框架的知识)。 本周,我需要查看两个适用于Android的开源应用程序的源代码: ioschedubuntu一个适用于android的应用程序。 它们都非常出色,值得关注并仔细查看其源代码。 现在,我将向您介绍在IOSched应​​用程序上发现的一个不错的实现。 SinglePane模式。 第一次我真的没有发现它很有用,但是当您开始使用它时,您会发现它真的很酷! 它提供了一种简单的方法来定义仅包含一个内容片段的“活动”。 很简单,嗯?

/**
 * Copyright 2012 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.android.apps.iosched.ui;

import com.google.android.apps.iosched.R;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;

/**
 * A {@link BaseActivity} that simply contains a single fragment. The intent used to invoke this
 * activity is forwarded to the fragment as arguments during fragment instantiation. Derived
 * activities should only need to implement {@link SimpleSinglePaneActivity#onCreatePane()}.
 */
public abstract class SimpleSinglePaneActivity extends BaseActivity {
    private Fragment mFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_singlepane_empty);

        if (getIntent().hasExtra(Intent.EXTRA_TITLE)) {
            setTitle(getIntent().getStringExtra(Intent.EXTRA_TITLE));
        }

        final String customTitle = getIntent().getStringExtra(Intent.EXTRA_TITLE);
        setTitle(customTitle != null ? customTitle : getTitle());

        if (savedInstanceState == null) {
            mFragment = onCreatePane();
            mFragment.setArguments(intentToFragmentArguments(getIntent()));
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.root_container, mFragment, "single_pane")
                    .commit();
        } else {
            mFragment = getSupportFragmentManager().findFragmentByTag("single_pane");
        }
    }

    /**
     * Called in <code>onCreate</code> when the fragment constituting this activity is needed.
     * The returned fragment's arguments will be set to the intent used to invoke this activity.
     */
    protected abstract Fragment onCreatePane();

    public Fragment getFragment() {
        return mFragment;
    }
}

尽管这次是从BaseActivity扩展的,但您可以从Activity,SherlockFragmentActivity,RoboSherlockFragmentActivity或适合您项目的任何内容扩展。 仅考虑到它必须能够使用片段。

如您所见,它非常简单,您只需扩展此类并在子活动上覆盖onCreatePane()。

public class ExampleOneFragmentActivity extends SinglePaneActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected Fragment onCreatePane() {
        return new ExampleOneFragment();
    }

}

这将是片段:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="@dimen/content_padding_normal"
        android:paddingRight="@dimen/content_padding_normal"
        android:paddingTop="@dimen/content_padding_normal"
        android:paddingBottom="@dimen/content_padding_normal">

        <TextView android:id="@+id/vendor_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            style="@style/TextHeader" />

        <TextView android:id="@+id/vendor_url"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:autoLink="web"
            android:paddingBottom="@dimen/element_spacing_normal"
            style="@style/TextBody" />

        <com.google.android.apps.iosched.ui.widget.BezelImageView android:id="@+id/vendor_logo"
            android:scaleType="centerCrop"
            android:layout_width="@dimen/vendor_image_size"
            android:layout_height="@dimen/vendor_image_size"
            android:layout_marginTop="@dimen/element_spacing_normal"
            android:src="@drawable/sandbox_logo_empty"/>

        <TextView android:id="@+id/vendor_desc"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/element_spacing_normal"
            android:paddingTop="@dimen/element_spacing_normal"
            style="@style/TextBody" />
    </LinearLayout>
</ScrollView>

*那是从iosched挑选的

别忘了,这种简单的模式还提供了传递额外内容来设置新标题的功能! 或者,您可以使用BaseActivity的这两种方法将参数从活动传递到片段。

/**
     * Converts an intent into a {@link Bundle} suitable for use as fragment arguments.
     */
    public static Bundle intentToFragmentArguments(Intent intent) {
        Bundle arguments = new Bundle();
        if (intent == null) {
            return arguments;
        }

        final Uri data = intent.getData();
        if (data != null) {
            arguments.putParcelable("_uri", data);
        }

        final Bundle extras = intent.getExtras();
        if (extras != null) {
            arguments.putAll(intent.getExtras());
        }

        return arguments;
    }

    /**
     * Converts a fragment arguments bundle into an intent.
     */
    public static Intent fragmentArgumentsToIntent(Bundle arguments) {
        Intent intent = new Intent();
        if (arguments == null) {
            return intent;
        }

        final Uri data = arguments.getParcelable("_uri");
        if (data != null) {
            intent.setData(data);
        }

        intent.putExtras(arguments);
        intent.removeExtra("_uri");
        return intent;
    }

参考: Android UI:Javier Manzano的Blog博客中查看我们JCG合作伙伴 Javier Manzano 编写的iosched开源应用程序

翻译自: https://www.javacodegeeks.com/2013/01/android-ui-taking-a-look-at-iosched-open-source-app.html

iosched

 类似资料: