Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在CardView布局中实现OnClickListener_Java_Android_Onclicklistener - Fatal编程技术网

Java 如何在CardView布局中实现OnClickListener

Java 如何在CardView布局中实现OnClickListener,java,android,onclicklistener,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; i

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;
    }



}
这是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>
<?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>

和card_layout.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>
<?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>

您可以创建一个接口,将适配器与活动进行通信

创建一个像

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;
    ...
}
最后,在OnClickListener中,调用如下方法

listener.callback();
通过这种方式,您可以将值从适配器传递到活动,然后创建另一个活动,例如传递捆绑包

我希望这有帮助

listener.callback();