cardView 选择上的持久角

persistent corners on cardView selection

我在列表项的背景上设置了一个角部带有半径的 cardView。 问题是当我单击 items/card 选择时,它包含角。起初我尝试使用 drawable/shape 然后使用 9 补丁图像,我得到了相同的结果。 我注意到当我在卡片上添加 clickable true 时,一切看起来都很好,但 intent 不再启动。

我的代码:

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    card_view:cardCornerRadius="15dp"
    card_view:contentPadding="8dp"
    android:background="@android:color/white"
    android:foreground="?attr/selectableItemBackground">
<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent">

 <TextView
     android:id="@+id/section"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignParentStart="true"
     android:textSize="12sp"
     android:textStyle="italic"
     tools:text="Section"
     android:background="@android:color/transparent"/>
 <TextView
     android:id="@+id/time"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignParentEnd="true"
     android:textSize="12sp"
     tools:text="29 May 1981"
     android:background="@android:color/transparent"/>
 <TextView
     android:id="@+id/date"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_toStartOf="@+id/time"
     android:textSize="12sp"
     tools:text="3:00"
     android:background="@android:color/transparent"
     tools:ignore="RelativeOverlap" />
 <TextView
     android:id="@+id/title"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:textColor="#28055E"
     android:textSize="16sp"
     android:textStyle="bold"
     tools:text="some text"
     android:layout_below="@+id/section"
     android:background="@android:color/transparent"/>
 <TextView
     android:id="@+id/author"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:textSize="12sp"
     tools:text="Published by: Section's Name"
     android:layout_below="@+id/title"
     android:background="@android:color/transparent"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.List;

public class NewsArrayAdapter extends ArrayAdapter<News> {
    public NewsArrayAdapter(Context context, List<News> news) {
        super(context, 0, news);
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View listItemView = convertView;
        if (listItemView == null) {
            listItemView = LayoutInflater.from(getContext()).inflate(
                    R.layout.list_item, parent, false);
        }
        News currentNews = getItem(position);
        TextView titleView = listItemView.findViewById(R.id.title);
        assert currentNews != null;

        titleView.setText(currentNews.getTitle());
        TextView sectionView = listItemView.findViewById(R.id.section);
         sectionView.setText(currentNews.getSection());

        TextView authorView = listItemView.findViewById(R.id.author);
       authorView.setText(currentNews.getAuthor());

        TextView dateView = listItemView.findViewById(R.id.date);
         dateView.setText(currentNews.getDate());

        TextView timeView = listItemView.findViewById(R.id.time);
        timeView.setText(currentNews.getTime());
        return listItemView;
    }}



``    in main activity:
newsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                // Find the current news that was clicked on
                News currentNews = mAdapter.getItem(position);

                // Convert the String URL into a URI object (to pass into the Intent constructor)
                Uri newsUri = Uri.parse(currentNews.getUrl());

                // Create a new intent to view the news URI
                Intent websiteIntent = new Intent(Intent.ACTION_VIEW, newsUri);

                // Send the intent to launch a new activity
                startActivity(websiteIntent);
            }
        });

    

@Haider Saleem 和@Sam Raju 谢谢你们的帮助。我在自定义适配器中为卡设置了onclick,问题就解决了。

这是我所做的:

  CardView cardView= listItemView.findViewById(R.id.card);
        cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view)
            {

                // Find the current news that was clicked on
                News currentNews = getItem(position);
                // Convert the String URL into a URI object (to pass into the Intent constructor)
                assert currentNews != null;
                Uri newsUri = Uri.parse(currentNews.getUrl());

                // Create a new intent to view the news URI
                Intent websiteIntent = new Intent(Intent.ACTION_VIEW, newsUri);

                // Send the intent to launch a new activity
                getContext().startActivity(websiteIntent);
            }
        });