涟漪效应不会高于ImageView

时间:2016-02-20 17:13:16

标签: android xml listview imageview rippledrawable

我有CustomListView我正在显示一些文字,并使用image的库显示Picasso。我在drawable文件夹中创建了一个xml文件,为Ripple Effect创建了一个drawable-21文件夹,但由于某种原因,效果不会超过ImageView

这是我的文件:

drawablelistview_item_background.xml

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle" >
            <solid
                android:color="#ffdadada"/>
        </shape>
    </item>
    <item>
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle" >
            <solid
                android:color="#ffffff"/>
        </shape>
    </item>
</selector>

drawable-v21listview_item_background.xml

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="#ffdadada">
    <item android:drawable="@android:color/white"/>
</ripple>

activity_main

<?xml version="1.0"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="150dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:expandedTitleMarginStart="20dp"
            app:expandedTitleMarginEnd="20dp"
            app:contentScrim="@color/colorPrimary"
            android:background="@color/colorPrimary">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="150dp"
                android:gravity="start|bottom"
                app:layout_collapseMode="parallax"
                android:background="@color/colorPrimary"
                android:orientation="vertical">

                ... 

            </LinearLayout>

            <android.support.v7.widget.Toolbar
                android:layout_height="?attr/actionBarSize"
                android:layout_width="match_parent"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:layout_collapseMode="pin"
                app:theme="@style/ToolbarTheme"
                android:background="@android:color/transparent"
                android:id="@+id/main_toolbar">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="start"
                    android:id="@+id/toolbar_title"
                    android:textSize="20sp"
                    android:textColor="#ffffff"/>

            </android.support.v7.widget.Toolbar>

        </android.support.design.widget.CollapsingToolbarLayout>

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/scroll"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="false"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:background="#eeeeee">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                tools:ignore="UselessParent">

                <packagename.NestedListView
                    android:id="@android:id/list"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="4dp"
                    android:divider="#eeeeee"
                    android:drawSelectorOnTop="true"/>

            </LinearLayout>

        </FrameLayout>

    </android.support.v4.widget.NestedScrollView>

</android.support.design.widget.CoordinatorLayout>

NestedListView

public class NestedListView extends ListView implements View.OnTouchListener, AbsListView.OnScrollListener {

private int listViewTouchAction;
private static final int MAXIMUM_LIST_ITEMS_VIEWABLE = 99;

public NestedListView(Context context, AttributeSet attrs) {
    super(context, attrs);
    listViewTouchAction = -1;
    setOnScrollListener(this);
    setOnTouchListener(this);
}

@Override
public void onScroll(AbsListView view, int firstVisibleItem,
                     int visibleItemCount, int totalItemCount) {
    if (getAdapter() != null && getAdapter().getCount() > MAXIMUM_LIST_ITEMS_VIEWABLE) {
        if (listViewTouchAction == MotionEvent.ACTION_MOVE) {
            scrollBy(0, -1);
        }
    }
}

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}

@SuppressLint("DrawAllocation")
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    int newHeight = 0;
    final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
    int heightSize = MeasureSpec.getSize(heightMeasureSpec);
    if (heightMode != MeasureSpec.EXACTLY) {
        ListAdapter listAdapter = getAdapter();
        if (listAdapter != null && !listAdapter.isEmpty()) {
            int listPosition;
            for (listPosition = 0; listPosition < listAdapter.getCount()
                    && listPosition < MAXIMUM_LIST_ITEMS_VIEWABLE; listPosition++) {
                View listItem = listAdapter.getView(listPosition, null, this);
                //now it will not throw a NPE if listItem is a ViewGroup instance
                if (listItem instanceof ViewGroup) {
                    listItem.setLayoutParams(new LayoutParams(
                            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                }
                listItem.measure(widthMeasureSpec, heightMeasureSpec);
                newHeight += listItem.getMeasuredHeight();
            }
            newHeight += getDividerHeight() * listPosition;
        }
        if ((heightMode == View.MeasureSpec.AT_MOST) && (newHeight > heightSize)) {
            if (newHeight > heightSize) {
                newHeight = heightSize;
            }
        }
    } else {
        newHeight = getMeasuredHeight();
    }
    setMeasuredDimension(getMeasuredWidth(), newHeight);
}

@Override
public boolean onTouch(View v, MotionEvent event) {
    if (getAdapter() != null && getAdapter().getCount() > MAXIMUM_LIST_ITEMS_VIEWABLE) {
        if (listViewTouchAction == MotionEvent.ACTION_MOVE) {
            scrollBy(0, 1);
        }
    }
    return false;
}

MainActivity

NestedListView list = (NestedListView)findViewById(android.R.id.list);
            CustomList adapter = new CustomList(MainActivity.this, myRssFeed.getList());
            adapter.addAll();
            list.setAdapter(adapter);

custom_listview_item

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardCornerRadius="2dp"
        app:cardElevation="2dp"
        android:layout_marginBottom="@dimen/cardMarginVertical"
        android:layout_marginLeft="@dimen/cardMarginHorizontal"
        android:layout_marginRight="@dimen/cardMarginHorizontal"
        android:layout_marginTop="@dimen/cardMarginVertical"
        app:cardPreventCornerOverlap="false"
        app:contentPadding="0dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:background="@drawable/listview_item_background"
            android:id="@+id/ln">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="170dp"
                android:background="#d6d6d6"
                android:contentDescription="@null"
                android:id="@+id/image"
                android:scaleType="centerCrop" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingRight="16dp"
                android:paddingLeft="16dp"
                android:layout_marginBottom="8dp"
                android:textSize="18sp"
                android:fontFamily="sans-serif"
                tools:ignore="HardcodedText,UnusedAttribute"
                android:id="@+id/title"/>

            <Button
                android:layout_width="match_parent"
                android:layout_height="36dp"
                android:layout_marginTop="16dp"
                android:paddingEnd="6dp"
                android:paddingRight="6dp"
                android:background="@drawable/listview_item_background"
                android:text="@string/condividi"
                android:textStyle="bold"
                android:textSize="16sp"
                android:textColor="@color/material_text_dialogs"
                android:gravity="center|center_vertical|center_horizontal"
                android:stateListAnimator="@null"
                tools:ignore="RtlHardcoded,RtlSymmetry,UnusedAttribute"
                android:id="@+id/condividi"/>

        </LinearLayout>

    </android.support.v7.widget.CardView>

</FrameLayout>

CustomList上课:

public class CustomList extends ArrayAdapter<RSSItem> {

private static Activity context = null;
private final List<RSSItem> web;

public CustomList(Activity context, List<RSSItem> web) {
    super(context, R.layout.new_listview, web);
    CustomList.context = context;
    this.web = web;
}

@Override
public View getView(final int position, View view, ViewGroup parent) {
    LayoutInflater inflater = context.getLayoutInflater();
    @SuppressLint({"ViewHolder", "InflateParams"}) final View rowView = inflater.inflate(R.layout.new_listview, null, true);

    ImageView imageView = (ImageView)rowView.findViewById(R.id.image);
    Picasso.with(context).load(web.get(position).getImage()).into(imageView);

    TextView textView = (TextView)rowView.findViewById(R.id.title);
    textView.setText(Html.fromHtml(web.get(position).getTitle()));

      LinearLayout linearLayout = (LinearLayout)rowView.findViewById(R.id.notizia);
      linearLayout.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {

              String url = web.get(position).getLink();
              if (!url.startsWith("http://") && !url.startsWith("https://"))
                  url = "http://" + url;
              Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
              String title = "Completa azione usando:";
              Intent chooser = Intent.createChooser(browserIntent, title);
              context.startActivity(chooser);

          }
      });


      Button button = (Button)rowView.findViewById(R.id.condividi);
      button.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
          }
      });

      return rowView;

  }
}

这就是我展示ImageView的方式。

你能帮我吗?

2 个答案:

答案 0 :(得分:3)

几天前我也面临同样的问题。我通过在ImageView中包含FrameLayout并在FrameLayout上应用android:foreground属性来解决了我的问题。您还必须将android:clickable的{​​{1}}属性设置为FrameLayout,或者将true设置为onClickListener

请在以下link中的以下主题中查看我的答案。

如果有帮助,请告诉我。

答案 1 :(得分:3)

我设法在没有额外布局的情况下解决了这个问题。:

private Viewbox getHeader() {
        Grid gr = new Grid();
        var sr = Application.GetResourceStream(new Uri("Propuestas;component/img/log.xaml", UriKind.Relative));

        var img = (Canvas)XamlReader.Load(new XmlTextReader(sr.Stream));

        var logo = new Viewbox {
            Child = img,
            HorizontalAlignment = HorizontalAlignment.Left,
            VerticalAlignment = VerticalAlignment.Center,
            Width = 165
        };
        var detalles = new TextBlock {
            FontSize = 10,
            FontFamily = new FontFamily("Verdana"),
            Padding = new Thickness(logo.Width + 15, 0, 0, 0)
        };
        App.Comando.CommandText = "SELECT RazEmp, DirEmp, CpEmp, PobEmp, ProEmp, TelEmp, CifEmp FROM META4.Empresa";
        using (var reader = App.Comando.ExecuteReader())
            while (reader.Read())
                detalles.Text = "" + reader.GetString(0).Trim() + "\n" + reader.GetString(1).Trim() + "\n" +
                                reader.GetDecimal(2) + " - " + reader.GetString(3).Trim() + "(" +
                                reader.GetString(4).Trim() + ")\n" + "Tlf: " + reader.GetString(5).Trim() +
                                "\nCIF: " + reader.GetString(6).Trim();

        var pd = new TextBox {
            Text = "PEDIDO DE COMPRA " + numPCO,
            TextAlignment = TextAlignment.Left,
            FontSize = 19,
            FontFamily = new FontFamily("Verdana"),
            FontWeight = FontWeights.Bold,
            Background = new SolidColorBrush(Color.FromRgb(192, 192, 192)),
            Margin = new Thickness(logo.Width + 15, 10, 0, 20),
            BorderThickness = new Thickness(0)
        };
        gr.ColumnDefinitions.Add(new ColumnDefinition());
        gr.ColumnDefinitions.Add(new ColumnDefinition());
        gr.RowDefinitions.Add(new RowDefinition());
        gr.RowDefinitions.Add(new RowDefinition());
        Grid.SetRow(logo, 0);
        Grid.SetRow(detalles, 0);
        Grid.SetRow(pd, 1);
        Grid.SetColumn(pd, 0);
        Grid.SetColumnSpan(pd, 2);
        Grid.SetColumnSpan(detalles, 2);

        gr.Children.Add(logo);
        gr.Children.Add(detalles);
        gr.Children.Add(pd);
        Viewbox vb = new Viewbox();
        vb.Child = gr;
        vb.Measure(new System.Windows.Size(headerWidth, headerHeight));
        vb.Arrange(new Rect(15,15,headerWidth,headerHeight));
        vb.UpdateLayout();
        return vb;
    }