如何从recyclerView获取正确的元素

时间:2015-06-13 15:04:29

标签: java android

我们目前正在开发Android应用程序,但我们正在解决问题。

我们有一个RecyclerView,根据数据库结果填充CardView。 在CardView内部,我们有一个按钮。

现在我们尝试在按钮上创建一个onclick效果(这可行)。

但是当我们点击cardView中的按钮时,我们希望代码返回该cardView的specefic ID。

我们尝试使用

getParent()

但即使我们按下另一张cardView上的按钮,它也会返回相同的卡片视图。如果没有在recyclerView本身上创建onClick,这可能吗?

我们想要cardview的唯一ID

编辑:

我们希望卡上的按钮能够提取该卡上的信息

片段:

    package info.androidhive.materialnavbar.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

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

import info.androidhive.materialnavbar.Activity.MainActivity;
import info.androidhive.materialnavbar.CardItem;
import info.androidhive.materialnavbar.R;
import info.androidhive.materialnavbar.ViewAdapters.RVAdapter;

/**
 * Created by bart on 10-6-2015
 * 2015 - 06
 * Superclass for all Information Fragments
 * Class handles the inflation of the RecyclerView and the XML files
 */

public class InformationFragment extends Fragment {
    private RecyclerView recyclerView;
    private RVAdapter mRecyclerViewAdapter;
    private View containerView;
    private static List<CardItem> CardEntry;
    private Map<Integer, String> mFragmentTags;
    public static boolean refresh = false;



    //haalt list leeg
    public void refreshFragment(){
            // zet fragment op null
            Fragment frg = null;
            frg = getFragmentManager().findFragmentById(2131296339);
            final FragmentTransaction ft = getFragmentManager().beginTransaction();
            ft.detach(frg);
            ft.attach(frg);
            ft.commit();
    }

    // recycler list
    public List<CardItem> getCardData(int type) {
        CardEntry = new ArrayList<>();
        // tijdelijk knop voor testing
        int reporticon = R.drawable.reporttemp;
        switch (type) {
            case 0:
                // Vull kaartjes met volgende items :
                CardEntry.add(new CardItem("Today", "string a", R.drawable.ic_facts,reporticon));
                CardEntry.add(new CardItem("Today", "string a", R.drawable.ic_facts,reporticon));
                CardEntry.add(new CardItem("Today", "string a", R.drawable.ic_facts,reporticon));
                CardEntry.add(new CardItem("Today", "string a", R.drawable.ic_facts,reporticon));
                CardEntry.add(new CardItem("Today", "string a", R.drawable.ic_facts,reporticon));
                CardEntry.add(new CardItem("Today", "string a", R.drawable.ic_facts,reporticon));
                //workaround
                //zet case type om naar cardcounter > inflater > refresh
                MainActivity.cardcounter = 0;
                if (!refresh){
                refreshFragment();
                    refresh = true;
                }
                break;
            case 1:                         // V title  Vcontent    V img
                CardEntry.add(new CardItem("Facts", "string a", R.drawable.ic_birthdays,reporticon));
                //workaround
                MainActivity.cardcounter = 1;
                if (!refresh){
                    refreshFragment();
                    refresh = true;
                }
                break;
            case 2:
                CardEntry.add(new CardItem("History", "string a", R.drawable.ic_history,reporticon));
                //workaround
                MainActivity.cardcounter = 2;
                if (!refresh){
                    refreshFragment();
                    refresh = true;
                }
                break;
            case 3:
                CardEntry.add(new CardItem("Birthday", "string a", R.drawable.ic_lifehacks,reporticon));
            //workaround
            MainActivity.cardcounter = 3;
            if (!refresh){
                refreshFragment();
                refresh = true;
            }
            break;
            case 4:
                CardEntry.add(new CardItem("Lifehacks", "string a", R.drawable.ic_facts,reporticon));
                //workaround
                MainActivity.cardcounter = 4;
                if (!refresh){
                    refreshFragment();
                    refresh = true;
                }
                break;
            case 5:
                CardEntry.add(new CardItem("Quotes", "string a", R.drawable.ic_facts,reporticon));
                //workaround
                MainActivity.cardcounter = 5;
                if (!refresh){
                    refreshFragment();
                    refresh = true;
                }
                break;
            case 6:
                CardEntry.add(new CardItem("Favorites", "string a", R.drawable.ic_facts,reporticon));
                //workaround
                MainActivity.cardcounter = 6;
                if (!refresh){
                    refreshFragment();
                    refresh = true;
                }
                break;
        }
        return CardEntry;
    }


    public List<CardItem> getCardEntry() {
        return CardEntry;
    }
    // end recycler


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
            // Inflate the layout for this fragment * iets aangepast, return onderaan voor recycler drawer
            View layout = inflater.inflate(R.layout.fragment_content, container, false);
            // pak het id van de recycler uit xml
            recyclerView = (RecyclerView) layout.findViewById(R.id.RecyclerCardList);
            // haal de data  op uit de return/switch hier boven
            //
            mRecyclerViewAdapter = new RVAdapter(getCardData(MainActivity.cardcounter));
            //
            recyclerView.setAdapter(mRecyclerViewAdapter);
            // zet hem als dit op deze activity
            recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
              return layout;
    }

}

适配器:

    package info.androidhive.materialnavbar.ViewAdapters;

import android.content.Context;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import info.androidhive.materialnavbar.Activity.DownloadImageTask;
import info.androidhive.materialnavbar.Animations;
import info.androidhive.materialnavbar.CardItem;
import info.androidhive.materialnavbar.R;

public class RVAdapter extends RecyclerView.Adapter<RVAdapter.PersonViewHolder> {


    // Recycler View Adapter
    public static class PersonViewHolder extends RecyclerView.ViewHolder {

        CardView cv;
        TextView personName;
        TextView personAge;
        ImageView personPhoto;
        ImageButton FavBut;
        ImageButton ReportBut;

        PersonViewHolder(View itemView) {
            super(itemView);
            cv = (CardView) itemView.findViewById(R.id.cv);
            personName = (TextView) itemView.findViewById(R.id.person_name);
            personAge = (TextView) itemView.findViewById(R.id.person_age);
            personPhoto = (ImageView) itemView.findViewById(R.id.person_photo);
            FavBut = (ImageButton) itemView.findViewById(R.id.FavBut);
            ReportBut = (ImageButton) itemView.findViewById(R.id.ReportBut);

        }
    }

    List<CardItem> cardItems;

    public RVAdapter(List<CardItem> cardItems) {
        this.cardItems = cardItems;
    }


    public Object getCard(int position) {
        return cardItems.get(position);
    }

    public List<CardItem> getCardItems() {
        return cardItems;
    }

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

    @Override
    public PersonViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.carditem, viewGroup, false);
        PersonViewHolder pvh = new PersonViewHolder(v);
        return pvh;
    }


    // de 'onCreate'
    @Override
    public void onBindViewHolder(PersonViewHolder personViewHolder, int i) {
        // vult het object/element met
        personViewHolder.personName.setText(cardItems.get(i).name);
        personViewHolder.personAge.setText(cardItems.get(i).age);
        personViewHolder.personPhoto.setImageResource(cardItems.get(i).photoId);
        //fav
        personViewHolder.FavBut.setImageResource(cardItems.get(i).favId);
        //report
        personViewHolder.ReportBut.setImageResource(cardItems.get(i).reportId);

        //zet animation op dit object/element
        Animations.animateScatter(personViewHolder, true);


    }


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

}

1 个答案:

答案 0 :(得分:1)

创建一个像这样的界面

public static interface ItemClickListener<T>{
    void onItemClick(T itemdata);
}

在适配器中创建feild并在构造函数中初始化,如下所示

ItemClickListener<CardItem> mItemClickListener;

public ClientsAdapter(ItemClickListener<CardItem> itemClickListener) {
    mItemClickListener = itemClickListener;
}

在你的绑定中

@Override
public void onBindViewHolder(PersonViewHolder personViewHolder, int i) {
final CardItem item = mCardItems.get(i);
personViewHolder.FavBut.setOnClickListener(new OnClickListener(){
    public void onClick(View v){
        mItemClickListener.onItemClick(item);
    }

});

在你的片段中实现ItemClickListener接口并将其传递给适配器