CardView ClickListener Android

时间:2015-11-03 15:53:21

标签: android android-activity android-recyclerview android-cardview android-navigation-drawer

我有一个带适配器的cardView和一个导航抽屉。我想要做的是,当点击一张卡片时,打开抽屉并在抽屉内显示卡片。

这是我到目前为止尝试过的,但是听众没有工作:

CardViewLayout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="1dp"
android:foreground="?android:attr/selectableItemBackground"
>

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/cv"
    card_view:cardCornerRadius="0dp"
    card_view:cardElevation="@dimen/cardview_default_elevation"
    android:foreground="?android:attr/selectableItemBackground"
    android:clickable="true"
    >

        <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="16dp"
        >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/extension_photo"
            android:layout_alignParentStart="true"
            android:layout_centerVertical="true"
            android:layout_marginEnd="16dp"
            />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/file_name"
            android:layout_toEndOf="@+id/extension_photo"
            android:text="archivo.txt"
            android:layout_centerVertical="true"
            android:textSize="20sp"
            />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/file_size"
            android:layout_alignParentEnd="true"
            android:layout_centerVertical="true"
            android:text="50kb"
            />

        </RelativeLayout>

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

</LinearLayout>

DrawerLayout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingTop="@dimen/header_height">

    <android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/right_drawe_card_view"
    card_view:cardCornerRadius="0dp"
    card_view:cardElevation="@dimen/cardview_default_elevation"
    android:foreground="?android:attr/selectableItemBackground"
    android:clickable="true"
    >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="16dp"
        >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/extension_photo"
            android:layout_alignParentStart="true"
            android:layout_centerVertical="true"
            android:layout_marginEnd="16dp"
            android:src="@mipmap/ic_insert_drive_file_black_24dp"
            />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/file_name"
            android:layout_toEndOf="@+id/extension_photo"
            android:text="archivo.txt"
            android:layout_centerVertical="true"
            android:textSize="20sp"
            />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/file_size"
            android:layout_alignParentEnd="true"
            android:layout_centerVertical="true"
            android:text="50kb"
            />

    </RelativeLayout>

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

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:paddingBottom="10dp"
        android:paddingTop="20dp">

        <ImageButton
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:id="@+id/right_drawer_download_button"
            android:contentDescription="downloadButton"
            android:src="@mipmap/ic_file_download_black_24dp"
            style="?android:attr/borderlessButtonStyle"
            android:background="?android:selectableItemBackgroundBorderless"
            android:layout_weight="1"
            android:clickable="true" />
        <ImageButton
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:id="@+id/right_drawer_share_button"
            android:contentDescription="shareButton"
            android:src="@mipmap/ic_share_black_24dp"
            style="?android:attr/borderlessButtonStyle"
            android:background="?android:selectableItemBackgroundBorderless"
            android:layout_weight="1"
            android:clickable="true" />
        <ImageButton
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:id="@+id/right_drawer_delete_button"
            android:contentDescription="deleteButton"
            android:src="@mipmap/ic_delete_black_24dp"
            style="?android:attr/borderlessButtonStyle"
            android:background="?android:selectableItemBackgroundBorderless"
            android:layout_weight="1"
            android:clickable="true"
            android:contextClickable="true" />


    </LinearLayout>

    <ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    </ListView>
</LinearLayout>

适配器

public class CardAdapter extends RecyclerView.Adapter<CardAdapter.FileViewHolder> {

public static class FileViewHolder extends RecyclerView.ViewHolder{
    CardView cardView;
    ImageView icon;
    TextView namePlusExtension;
    TextView size;

    FileViewHolder(final View itemView,View.OnClickListener newListener){
        super(itemView);
        icon = (ImageView) itemView.findViewById(R.id.extension_photo);
        namePlusExtension = (TextView) itemView.findViewById(R.id.file_name);
        size = (TextView) itemView.findViewById(R.id.file_size);

        itemView.setOnClickListener(newListener);

    }




}

List<FileCard> files;
View.OnClickListener listener;
CardAdapter(View.OnClickListener newListener,List<FileCard> newFiles){
    this.listener = newListener;
    this.files = newFiles;
}

@Override
public int getItemCount(){
    return this.files.size();
}

@Override
public FileViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.file_card_layout, viewGroup, false);
    FileViewHolder fileViewHolder = new FileViewHolder(v,this.listener);
    return fileViewHolder;
}

@Override
public void onBindViewHolder(FileViewHolder fileViewHolder, int i) {
    List<String> imageExtensionList = Arrays.asList(".jpg",".bmp",".gif",".png",".psd",".pspimage",".thm",".tiff",".yuv");
    List<String> sourceExtensionList = Arrays.asList(".c",".cpp",".java",".py",".sh",".pl");
    List<String> musicExtensionList = Arrays.asList(".mp3",".wav",".mid",".wma");
    List<String> videoExtensionList = Arrays.asList(".3gp",".avi",".mp4",".mkv",".3g2",".asf",".asx",".mov",".mpg",".wmv");
    fileViewHolder.namePlusExtension.setText(files.get(i).name + files.get(i).extension);
    fileViewHolder.size.setText(files.get(i).size);

    String extension = files.get(i).extension;
    int resource = R.mipmap.ic_insert_drive_file_black_24dp;
    if( imageExtensionList.contains(extension) ) {
        resource = R.mipmap.ic_photo_black_24dp;
    }else if( sourceExtensionList.contains(extension)){
        resource = R.mipmap.ic_code_black_24dp;
    }else if( musicExtensionList.contains(extension)){
        resource = R.mipmap.ic_music_note_black_24dp;
    }else if( videoExtensionList.contains(extension)){
        resource = R.mipmap.ic_videocam_black_24dp;
    }
    fileViewHolder.icon.setImageResource(resource);
}

@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
    super.onAttachedToRecyclerView(recyclerView);
}

活动

onCreate(){
   ...
   ...
   DrawerLayout drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
   NavigationView rightDrawerView = (NavigationView)findViewById(R.id.right_drawer_view);
   ...
   ...
this.recyclerView = (RecyclerView) findViewById(R.id.recycler_files_view);
    LinearLayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
    this.recyclerView.setLayoutManager(layoutManager);

    this.fileCards  = new ArrayList<>();
    fileCards.add(new FileCard("archivo1",".jpg","50kb"));
    fileCards.add(new FileCard("archivo2",".cpp","100kb"));
    fileCards.add(new FileCard("archivo3", ".txt", "200kb"));

    updateFileCards();
}

private void updateFileCards(){
    View.OnClickListener l = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            CardView c = (CardView) rightDrawerView.findViewById(R.id.right_drawe_card_view);
            TextView t1 = (TextView)c.findViewById(R.id.file_size);
            TextView t2 = (TextView)c.findViewById(R.id.file_name);
            ImageView t3 =(ImageView) c.findViewById(R.id.extension_photo);
            TextView a1 = (TextView)v.findViewById(R.id.file_size);
            TextView a2 = (TextView)v.findViewById(R.id.file_name);
            ImageView a3 =(ImageView) v.findViewById(R.id.extension_photo);
            a1.setText(t1.getText().toString());
            a2.setText(t2.getText().toString());
            a3.setImageDrawable(t3.getDrawable());
            drawerLayout.openDrawer(rightDrawerView);
        }
    };
    CardAdapter adapter = new CardAdapter(l,this.fileCards);
    this.recyclerView.setAdapter(adapter);

}

知道我该怎么做吗?

0 个答案:

没有答案