根据单击的项目操作ListView中的其他列表项

时间:2014-05-01 12:39:24

标签: android android-layout android-fragments android-listview

我有一个包含ListView和自定义适配器的片段。我的目标是操纵列表项的子视图(以显示阴影)。如果单击某个项目,则必须通过更改创建阴影的视图的可见性来操作前后的列表项视图。

我可以成功获取参考并更改可见性,但问题是我无法获得对当前项目上方和下方视图的引用(因为阴影被添加到那些以根据需要创建阴影效果)

我在片段中的mSettingsListView上使用onItemClick侦听器。

片段:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    Log.d("Debugging", "onCreateView");
    View view = inflater.inflate(R.layout.settings_fragment_list, container, false);
    final ViewGroup viewGroup = container;

    Log.d(getTag(), "Finding settingsListView");
    mSettingsListView = (ListView) view.findViewById(R.id.settingsListView);
    mSettingsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Log.d("Debugging", "settingsListView onItemClick position: " + position);

            // Remove right shadow on current item when clicked
            View rightShadow = view.findViewById(R.id.settingsListRowShadowRight);
            rightShadow.setVisibility(View.GONE);

            // Shadow to be shown when active list item is the last
            // Shadow is shown on item above and view below list
            Log.d("Debugging", "settingsListView onItemClick position else clause");

            // Add a shadow at the bottom of the list item ABOVE current
            View aboveView = parent.getAdapter().getView(position -1, view, viewGroup);
            View aboveViewShadow = aboveView.findViewById(R.id.settingsListRowShadowAbove);
            aboveViewShadow.setVisibility(View.VISIBLE);

            // Add a shadow at the top of the list item BELOW current
            View belowView = parent.getAdapter().getView(position +1, view, viewGroup);
            View belowViewShadow = belowView.findViewById(R.id.settingsListRowShadowBelow);
            belowViewShadow.setVisibility(View.VISIBLE);
        }
    });

    return view;
}

现在阴影不会添加到上方和下方的列表项中,而是添加到单击的项目上。另一个怪癖是,当单击一个列表项时,它将使用下面的列表项的文本进行更新。

列表项布局:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/settingsListRowContainer"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:background="@drawable/settings_list_view_listitem_selector">

    <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="horizontal"
            android:paddingTop="@dimen/giraf_settings_list_row_paddingTop"
            android:paddingBottom="@dimen/giraf_settings_list_row_paddingBottom"
            android:paddingLeft="@dimen/giraf_settings_list_row_paddingLeft"
            android:paddingRight="@dimen/giraf_settings_list_row_paddingRight">

        <ImageView
                android:id="@+id/settingsListAppLogo"
                android:layout_width="@dimen/giraf_settings_list_row_imageView_size"
                android:layout_height="@dimen/giraf_settings_list_row_imageView_size"
                android:layout_marginRight="@dimen/giraf_settings_list_row_imageView_marginRight">
        </ImageView>

        <TextView
                android:id="@+id/settingsListAppName"
                android:layout_toRightOf="@id/settingsListAppLogo"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:gravity="center_vertical"
                android:textColor="@android:color/black"
                android:text="@string/giraf_settings_name"
                android:textSize="32dp" >
        </TextView>

    </LinearLayout>

    <!-- Shadow showing ABOVE active list item -->
    <FrameLayout
            android:id="@+id/settingsListRowShadowAbove"
            android:layout_width="fill_parent"
            android:layout_height="@dimen/giraf_settings_list_shadowSize"
            android:layout_gravity="bottom"
            android:background="@drawable/settings_list_view_shadow_top"
            android:visibility="gone"/>

    <!-- Shadow showing BELOW active list item -->
    <FrameLayout
            android:id="@+id/settingsListRowShadowBelow"
            android:layout_width="fill_parent"
            android:layout_height="@dimen/giraf_settings_list_shadowSize"
            android:layout_gravity="top"
            android:background="@drawable/settings_list_view_shadow_bottom"
            android:visibility="gone"/>

    <!-- Right shadow showing on a list item when inactive -->
    <FrameLayout
            android:id="@+id/settingsListRowShadowRight"
            android:layout_width="@dimen/giraf_settings_list_shadowSize"
            android:layout_height="fill_parent"
            android:layout_gravity="right"
            android:background="@drawable/settings_list_view_shadow_right"/>

</FrameLayout>

我不是如何让适配器返回特定位置的视图以及获取引用时是否/如何膨胀?

修改 刚刚添加了自定义适配器的getView()方法。

自定义适配器的getView方法:

public View getView(int position, View convertView, ViewGroup parent) {
    View vi = convertView;

    if(convertView == null)
        vi = mInflater.inflate(R.layout.settings_fragment_list_row, null);

    // Get current item in the list
    SettingsListItem item = mApplicationList.get(position);

    ImageView appIcon = (ImageView)vi.findViewById(R.id.settingsListAppLogo);
    TextView appNameText = (TextView)vi.findViewById(R.id.settingsListAppName);

    // Setting all values in ListView
    appIcon.setBackgroundDrawable(item.appIcon);
    appNameText.setText(item.appName);

    return vi;
}

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。问题是我试图使用适配器来获取对单击列表项上方和下方视图的引用。相反,我可以通过在父视图(listview本身)上使用getChildAt()方法找到适当的视图。

就像写作一样简单:

View aboveView = parent.getChildAt(position -1)
View aboveViewShadow = aboveView.findViewById(R.id.settingsListRowShadowAbove);
aboveViewShadow.setVisibility(View.VISIBLE);