如何在recyclerview

时间:2016-10-25 13:12:30

标签: java android view android-recyclerview

我想在RecyclerView中拥有多个视图。当“pauze”这个词通过时,我想使用“card_view_row_pauze”,当没有“pauze”这个词时,我想使用“card_view_row”。我尝试了一些东西(参见我在代码中添加的评论),但它只是没有用。我在stackoverflow上找到了一些答案,但遗憾的是我似乎无法实现它。我怎么能这样做?

MyRecyclerViewAdapter.java

package com.company.app;

import android.app.Activity;
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.TextView;
import android.widget.Toast;

import java.util.ArrayList;

public class MyRecyclerViewAdapter extends RecyclerView .Adapter<MyRecyclerViewAdapter .DataObjectHolder> {
    private ArrayList<DataObject> mDataset;
    private static MyClickListener myClickListener;
    private Activity mActivity;

    public static class DataObjectHolder extends RecyclerView.ViewHolder implements View .OnClickListener {
        TextView label;
        TextView dateTime;
        TextView tijd;
        CardView cardView;

        public DataObjectHolder(View itemView) {
            super(itemView);
            label = (TextView) itemView.findViewById(R.id.textView);
            dateTime = (TextView) itemView.findViewById(R.id.textView2);
            tijd = (TextView) itemView.findViewById(R.id.textView3);
            cardView = (CardView) itemView.findViewById(R.id.card_view);
            itemView.setOnClickListener(this);
        }

        @Override public void onClick(View v) {
            myClickListener.onItemClick(getAdapterPosition(), v);
        }
    }

    ViewGroup myParent; // THIS I ADDED TO SUPPORT MULTIPLE VIEWS

    public void setOnItemClickListener(MyClickListener myClickListener) {
        this.myClickListener = myClickListener;
    }

    public MyRecyclerViewAdapter(Activity activity, ArrayList<DataObject> myDataset) {
        mDataset = myDataset;
        mActivity = activity;
    }

    @Override public DataObjectHolder onCreateViewHolder(ViewGroup parent,int viewType) {
        myParent = parent; // THIS I ADDED TO SUPPORT MULTIPLE VIEWS
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view_row, parent, false);
        DataObjectHolder dataObjectHolder = new DataObjectHolder(view);
        return dataObjectHolder;
    }

    @Override
    public void onBindViewHolder(DataObjectHolder holder, final int position) {
        if (mDataset.get(position).getmText1().equals("Pauze")) { //HERE I WANT TO USE THE "card_view_row_pauze" VIEW WHEN THE PASSED TEXT CONTAINS "PAUZE"
            View viewd = LayoutInflater.from(myParent.getContext()).inflate(R.layout.card_view_row_pauze, myParent, false);
            DataObjectHolder dataObjectHolderd = new DataObjectHolder(viewd);
            dataObjectHolderd.label.setText(mDataset.get(position).getmText1());
            dataObjectHolderd.tijd.setText(mDataset.get(position).getmText3());
        }
        else {
            holder.cardView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String myDescription = mDataset.get(position).getDescription();
                    if (myDescription.length() >= 2) {
                        Toast.makeText(MainActivity.getMain().getApplicationContext(), myDescription, Toast.LENGTH_SHORT).show();
                    }
                }
            });
            holder.label.setText(mDataset.get(position).getmText1());
            holder.dateTime.setText(mDataset.get(position).getmText2());
            holder.tijd.setText(mDataset.get(position).getmText3());
        }
    }

    public void addItem(final DataObject dataObj, final int index) {if (mActivity == null){return;}

        mActivity.runOnUiThread(new Runnable() {
            @Override public void run() {
                mDataset.add(index, dataObj);
                notifyItemInserted(index);
            }
        });
    }

    public void deleteItem(int index) {
        mDataset.remove(index);
        notifyItemRemoved(index);
    }

    @Override public int getItemCount() {return mDataset.size();}
    public interface MyClickListener {public void onItemClick(int position, View v);}
}

DataObject.java

package com.company.app;

public class DataObject {
    private String mText1;
    private String mText2;
    private String mText3;
    private String mState;
    private String mDescription;

    DataObject (String text1, String text2, String text3, String state, String description){
        mText1 = text1;
        mText2 = text2;
        mText3 = text3;
        mState = state;
        mDescription = description;
    }

    public String getmText1() {return mText1;}
    public void setmText1(String mText1) {
        this.mText1 = mText1;
    }
    public String getmText2() {
        return mText2;
    }
    public void setmText2(String mText2) {
        this.mText2 = mText2;
    }
    public String getmText3() {
        return mText3;
    }
    public void setmText3(String mText3) {
        this.mText3 = mText3;
    }
    public String getmState() {
        return mState;
    }
    public void setmState(String mState) {
        this.mState = mState;
    }
    public String getDescription() {
        return mDescription;
    }
    public void setDescription(String mDescription) {
        this.mDescription = mDescription;
    }
} 

1 个答案:

答案 0 :(得分:0)

您应该使用Pauze和行xmls创建两个ViewHolder。并在ItemViewType中检查来自数据源的文本值以及根据Inflate特定的ViewHolder。

以下是一些代码。

public int getItemViewType(int position) {
        if (products.size()==0){
            return 0;
        }else if (viewFormat== Constants.listV){
            return Constants.listV;
        }else if (viewFormat==Constants.gridV){
            return Constants.gridV;
        }
        return super.getItemViewType(position);
    }

ViewHolder:

public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        if (viewType== Constants.gridV){
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.product_items,parent,false);
            return new MovieViewHolder(view);
        }else if (viewType==Constants.listV)
        {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.linear_item_view,parent,false);
            return new OtherViewHolder(view);
        }else{
            return null;
        }
    }