对listView项的Android -secondary Action

时间:2013-10-04 14:12:55

标签: android android-listview

Google如何在其某些观看次数上实施辅助操作。辅助操作的整个父视图响应触摸。下面是一个示例,我将其称为辅助操作,并突出显示辅助操作视图:

Secondary Action image example:

在这里,我们可以通过单击左侧拨打电话作为主要操作,也可以在分隔符执行辅助操作后单击。我想知道API级别上是否有某些内容可以实现此功能,如果没有,您是否可以推荐第三方。我想我也可以自己设计它。

2 个答案:

答案 0 :(得分:2)

请参阅Android源代码:

以下是代码(ContactDetailFragment.java):

首先,他们定义了一个名为DetailViewCache的自定义类,如下所示:

...

/**
 * Cache of the children views of a contact detail entry represented by a
 * {@link DetailViewEntry}
 */
private static class DetailViewCache {
    public final TextView type;
    public final TextView data;
    public final ImageView presenceIcon;
    public final ImageView secondaryActionButton;
    public final View actionsViewContainer;
    public final View primaryActionView;
    public final View secondaryActionViewContainer;
    public final View secondaryActionDivider;
    public final View primaryIndicator;

    public DetailViewCache(View view,
            OnClickListener primaryActionClickListener,
            OnClickListener secondaryActionClickListener) {
        type = (TextView) view.findViewById(R.id.type);
        data = (TextView) view.findViewById(R.id.data);
        primaryIndicator = view.findViewById(R.id.primary_indicator);
        presenceIcon = (ImageView) view.findViewById(R.id.presence_icon);

        actionsViewContainer = view.findViewById(R.id.actions_view_container);
        actionsViewContainer.setOnClickListener(primaryActionClickListener);
        primaryActionView = view.findViewById(R.id.primary_action_view);

        secondaryActionViewContainer = view.findViewById(
                R.id.secondary_action_view_container);
        secondaryActionViewContainer.setOnClickListener(
                secondaryActionClickListener);
        secondaryActionButton = (ImageView) view.findViewById(
                R.id.secondary_action_button);

        secondaryActionDivider = view.findViewById(R.id.vertical_divider);
    }
}

...

然后,膨胀ListView的项目:

...
private View getDetailEntryView(int position, View convertView, ViewGroup parent) {
        final DetailViewEntry entry = (DetailViewEntry) getItem(position);
        final View v;
        final DetailViewCache viewCache;

        // Check to see if we can reuse convertView
        if (convertView != null) {
            v = convertView;
            viewCache = (DetailViewCache) v.getTag();
        } else {
            // Create a new view if needed
            v = mInflater.inflate(R.layout.contact_detail_list_item, parent, false);

            // Cache the children
            viewCache = new DetailViewCache(v,
                    mPrimaryActionClickListener, mSecondaryActionClickListener);
            v.setTag(viewCache);
        }

        bindDetailView(position, v, entry);
        return v;
    }
...

然后你可以找到他们定义onClickListener的地方:

...

      private final OnClickListener mSecondaryActionClickListener = new OnClickListener() {
        @Override
        public void onClick(View view) {
            if (mListener == null) return;
            if (view == null) return;
            final ViewEntry entry = (ViewEntry) view.getTag();
            if (entry == null || !(entry instanceof DetailViewEntry)) return;
            final DetailViewEntry detailViewEntry = (DetailViewEntry) entry;
            final Intent intent = detailViewEntry.secondaryIntent;
            if (intent == null) return;
            mListener.onItemClicked(intent);
        }
    };

...

Android正在使用的XML布局(contact_detail_list_item),如下所示(contact_detail_list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<!--
/*
 * Copyright 2009, The Android Open Source Project
 *
 * 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.
 */
-->

<!-- Note: padding might be controlled programatically -->
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingStart="16dip">

    <com.android.contacts.detail.ActionsViewContainer
        android:id="@+id/actions_view_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:focusable="true"
        android:background="?android:attr/selectableItemBackground"
        android:nextFocusRight="@+id/secondary_action_view_container"
        android:minHeight="@dimen/detail_min_line_item_height">

        <!-- Note: padding might be controlled programatically -->
        <LinearLayout
            android:id="@+id/primary_action_view"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical"
            android:paddingStart="8dip" >

            <TextView
                android:id="@+id/data"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <ImageView
                    android:id="@+id/presence_icon"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="1dip"
                    android:layout_marginRight="4dip"
                    android:layout_marginEnd="4dip"
                    android:layout_gravity="center_vertical"
                    android:gravity="center"
                    android:scaleType="centerInside" />

                <TextView
                    android:id="@+id/type"
                    style="@style/ContactDetailItemType"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

                <View
                    android:id="@+id/primary_indicator"
                    android:layout_width="16dip"
                    android:layout_height="16dip"
                    android:visibility="gone"
                    android:layout_gravity="center_vertical"
                    android:background="@drawable/ic_list_default_mime_holo_dark" />

            </LinearLayout>

        </LinearLayout>

        <View
            android:id="@+id/vertical_divider"
            android:layout_width="1dip"
            android:layout_height="match_parent"
            android:layout_marginTop="@dimen/detail_vertical_divider_vertical_margin"
            android:layout_marginBottom="@dimen/detail_vertical_divider_vertical_margin"
            android:background="?android:attr/dividerVertical" />

        <!-- Note: padding might be controlled programatically -->

从这部分开始,您正在寻找:

        <FrameLayout
            android:id="@+id/secondary_action_view_container"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:paddingStart="@dimen/detail_item_icon_margin"
            android:paddingEnd="@dimen/detail_item_icon_margin"
            android:focusable="true"
            android:background="?android:attr/selectableItemBackground"
            android:nextFocusLeft="@id/actions_view_container">
            <ImageView
                android:id="@+id/secondary_action_button"
                android:layout_width="32dip"
                android:layout_height="32dip"
                android:layout_gravity="center_vertical"
                android:duplicateParentState="false" />
        </FrameLayout>
    </com.android.contacts.detail.ActionsViewContainer>
</FrameLayout>

答案 1 :(得分:1)

没有可自动生成此布局的内置API,视图或其他功能。

然而,自己创建这个相对简单,因为这个布局没有什么特别之处。

在您的示例中,该行的布局可能如下所示:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="48dp" />

    <Button
        android:id="@+id/callButton"
        android:layout_width="500dp"
        android:layout_height="match_parent"
        style="@style/transparentButtonStyle" />

    <View
        android:layout_width="1dp"
        android:layout_height="match_parent"
        android:background="@drawable/dotted_line" />

    <ImageButton
        android:id="@+id/smsButton"
        android:layout_width="48dp"
        android:layout_height="match_parent"
        android:src="@drawable/sms_button"
        style="@style/transparentButtonStyle" />

</LinearLayout>

您当然需要提供transparentButtonStyle,dotted_line drawable和smsButton drawable。您可能还想调整主按钮的宽度(也许RelativeLayout会更好地工作)。