如何在CardView布局中实现OnClickListener

时间:2015-02-05 10:28:22

标签: java android onclicklistener

MainActivity.java文件我想创建一个列表,其数字对应于数组的长度。 并将数组元素的值显示在列表中

这是MainActivity.java文件

package com.tolhedo.akew.mycardview2;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.List;
import java.util.ArrayList;


public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.cardList);
        recyclerView.setHasFixedSize(true);

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);

        recyclerView.setLayoutManager(layoutManager);

        /**** how to replace that number 3 with length of string array? *****/
        AnggraListAdapter anggraList = new AnggraListAdapter(createList(3));


        recyclerView.setAdapter(anggraList);

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private List<AnggraList> createList(int size){
        List<AnggraList> result = new ArrayList<>();

        for (int i=0; i<=size; i++){
            AnggraList list = new AnggraList();

            /** replace "i" with the contents or value of Title StringArray according to the index **/
            list.title = AnggraList.TITLE + i;

            /** replace "i" with the contents or value of Description StringArray according to the index **/
            list.desc = AnggraList.DESC + i + "..more";

            result.add(list);
        }
        return result;
    }



}

在AnggraListAdapter.java文件中,我想让项目可以被点击并移动到显示当前数组元素值的另一个页面(另一个活动)

package com.tolhedo.akew.mycardview2;

import android.support.v7.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


import java.util.List;

/**
 * Created by akew on 2/3/2015.
 */
public class AnggraListAdapter extends RecyclerView.Adapter<AnggraListAdapter.ListViewHolder> {
    private List<AnggraList> list;

    public AnggraListAdapter(List<AnggraList> list) {
        this.list = list;
    }

    @Override
    public ListViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_layout, viewGroup, false);
        return new ListViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ListViewHolder listViewHolder, int i) {
        AnggraList anggraList = list.get(i);
        listViewHolder.mTitle.setText(anggraList.title);
        listViewHolder.mDesc.setText(anggraList.desc);
    }

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

    public static class ListViewHolder extends RecyclerView.ViewHolder{
        protected TextView mTitle, mDesc;
        public View view;

        public ListViewHolder(View v){
            super(v);
            view = v;
            mTitle = (TextView) view.findViewById(R.id.textView);
            mDesc = (TextView) view.findViewById(R.id.textView2);

            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    /********
                    I want when this CardView in touch will open another activity. please help me
                     ********/

                }
            });
        }
    }
}

这是AnggraList.java文件

package com.tolhedo.akew.mycardview2;

/**
 * Created by akew on 2/3/2015.
 */
public class AnggraList {
    protected String title;
    protected String desc;

    protected static final String TITLE = "title_";
    protected static final String DESC = "desc_";
}

这是activity_main.xml布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="@dimen/abc_dropdownitem_text_padding_left"
    tools:context=".MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/cardList"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

</RelativeLayout>

和card_layout.xml布局文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="8dp"
    card_view:cardCornerRadius="4dp" >
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <FrameLayout
            android:layout_width="fill_parent"
            android:layout_height="300sp"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_margin="-16dp"
            android:id="@+id/frameLayout">
            <ImageView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:id="@+id/imageView"
                android:background="@drawable/anggra_poster_2"
                android:layout_gravity="center" />
            <ImageView
                android:layout_width="250sp"
                android:layout_height="250sp"
                android:src="@drawable/button"
                android:layout_gravity="center"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textStyle="bold|italic"
                android:text="EPISODE"
                android:textSize="48sp"
                android:layout_gravity="center"
                android:textColor="#ffffffff"
                android:shadowColor="#ff000000"
                android:shadowRadius="15.0"
                android:shadowDy="8.0"
                android:shadowDx="0.0"
                android:rotation="-10"/>
        </FrameLayout>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Large Text"
            android:id="@+id/textView"
            android:padding="@dimen/abc_dropdownitem_text_padding_left"
            android:layout_below="@+id/frameLayout"
            android:layout_centerHorizontal="true" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Text"
            android:id="@+id/textView2"
            android:padding="@dimen/abc_dropdownitem_text_padding_left"
            android:layout_below="@+id/textView"
            android:layout_centerHorizontal="true" />
    </RelativeLayout>
</android.support.v7.widget.CardView>

1 个答案:

答案 0 :(得分:2)

您可以创建一个界面,将您的适配器与您的活动进行通信。

创建一个类似

的界面
public interface AdapterComunication{

    public void callBack();
}

然后让您的活动实现此界面

public class MainActivity extends ActionBarActivity implements AdapterComunication {
....
@Override
    public void callBack() { .. }
}

然后让你的适配器接收

public AnggraListAdapter(AdapterComunication listener, List<AnggraList> list) {
    this.listener = listener;
    ...
}

在您的活动中

AnggraListAdapter anggraList = new AnggraListAdapter(this, createList(3));

最后,在OnClickListener中,调用类似

的方法
listener.callback();

这样,您可以将值从适配器传递给活动,然后创建另一个活动,例如传递一个Bundle。

我希望它有所帮助。