2015-02-05 3 views
0

MainActivity.java 파일 배열의 길이에 해당하는 숫자의 목록을 만들고 싶습니다. 하고 목록CardView 레이아웃에서 OnClickListener를 구현하는 방법

이것으로 배열 요소의 값을 디스플레이 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 파일, I는 상품을 클릭 할 수 있도록하려면, 다른 페이지로 이동 현재의 배열 요소

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

이다 (다른 활성)

이 당신은 당신의 활동 어댑터를 comunicate 수있는 인터페이스를 만들 수 있습니다

<?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> 

답변

2

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 레이아웃 파일입니다.

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

를받을 수 있도록 할

,210

마지막에 OnClickListener를, 당신은 예를 들어, 번들을 통과, 다른 활동을 만든 다음 활동에 어댑터에서 값을 전달할 수 있으며,이 방법

listener.callback(); 

같은 메서드를 호출합니다.

도움이 되었기를 바랍니다.

+0

고맙습니다. 문제가 해결되었습니다. –

관련 문제