简体   繁体   English

如何在CardView布局中实现OnClickListener

[英]how to implement OnClickListener in CardView layout

MainActivity.java file I want to make a list whose numbers corresponding length of the array . 我要创建MainActivity.java文件,该列表的数字对应于数组的长度。 and display the value of the array element into the list 并将数组元素的值显示在列表中

this is MainActivity.java file 这是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;
    }



}

and in AnggraListAdapter.java file, I want to make the items can be clicked and moved to another page (another activity) that displays the value of the current array element 并在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
                     ********/

                }
            });
        }
    }
}

and this is AnggraList.java file 这是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_";
}

this is activity_main.xml layout file 这是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>

and card_layout.xml layout file 和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>

You can create an interface to comunicate your adapter with your activity. 您可以创建一个接口以使适配器与活动通信。

Create an interface like 创建一个类似的界面

public interface AdapterComunication{

    public void callBack();
}

Then make your activity implement this interface 然后让您的活动实现此接口

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

then make your adapter receive 然后让您的适配器接收

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

in your activity 在你的活动中

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

Finally, in you OnClickListener, call the method like 最后,在您的OnClickListener中,调用类似

listener.callback();

this way, you can pass values from the adapter to the activity and then, create another activity, passing an Bundle, for example. 这样,您可以将值从适配器传递到活动,然后创建另一个活动,例如传递捆绑。

I hope it helped. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM