在android

时间:2016-03-09 06:28:14

标签: android android-recyclerview

我有一个Recyclerview,其中我有2个按钮。我想在点击按钮时在两个不同的视图之间切换。在我的recyclerview适配器类中,我已将视图填充为

 public Offline_ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
     view=LayoutInflater.from(parent.getContext()).inflate(R.layout.offline_data_card, null);
  Offline_ViewHolder ovh = new Offline_ViewHolder(view);
 return ovh;

但我不知道如何在按钮点击时使用2个视图。我已经附加了我想要点击按钮点击view 1

的视图

我的完整代码如下所示:

Offline_Adapter.java:

public class Offline_Adapter extends RecyclerView.Adapter<Offline_ViewHolder> {
     Context context;
    RequestQueue mRequestQueue;
    ImageLoader mImageLoader;
    DBHelper myDB;
    ArrayList<Offers> datas;
    SparseBooleanArray selectedItems;
    ActionMode mActionMode;
    Button b1,b2;
    public Offline_Adapter(Context context, ArrayList<Offers> datas) {

        this.context = context;
        this.datas = datas;
        selectedItems = new SparseBooleanArray();
        mRequestQueue = Volley.newRequestQueue(context);
        mImageLoader = new ImageLoader(mRequestQueue, new ImageLoader.ImageCache() {

            private final LruCache<String, Bitmap> mCache = new LruCache<String, Bitmap>(10);

            public void putBitmap(String url, Bitmap bitmap) {
                mCache.put(url, bitmap);
            }

            public Bitmap getBitmap(String url) {
                return mCache.get(url);
            }
        });

    }


    @Override
    public Offline_ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        b1=(Button)parent.findViewById(R.id.b1);
        b2=(Button)parent.findViewById(R.id.b2);

        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                view = LayoutInflater.from(v.getContext()).inflate(R.layout.offline_data_card, null);
               view.setVisibility(View.VISIBLE);
            }
        });
        b2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                view = LayoutInflater.from(v.getContext()).inflate(R.layout.offline_data_card_view2, null);
                view.setVisibility(View.VISIBLE);
            }
        });
        Offline_ViewHolder ovh = new Offline_ViewHolder(view);
        return ovh;
    }

    @Override
    public void onBindViewHolder(Offline_ViewHolder holder, final int position) {
        String card_id = datas.get(position).getLe_id().toString();
        String img_url = URL.IMAGE;
        String Thumb_image = img_url + card_id + ".jpg";
        if (Thumb_image == null || Thumb_image == "") {
            holder.image.setImageResource(R.drawable.vg_placeholder);
        } else {
            holder.image.setImageUrl(Thumb_image, mImageLoader);
        }

        String font_path = "fonts/Roboto-Bold.ttf";
        Typeface font = Typeface.createFromAsset(context.getAssets(), font_path);
        holder.title.setTypeface(font);
        holder.title.setText(datas.get(position).getTitle().toString());

        String count = datas.get(position).getView_count();
        count = Integer.valueOf(count).toString();
        holder.v_count.setText(count);
        holder.c_count.setText(datas.get(position).getComment_count());
        holder.f_count.setText(datas.get(position).getFav_count());


        holder.image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String downloadpath = URL.DOWNLOAD;
                String url_id = datas.get(position).getLe_id().toString();
                String download_link = downloadpath + url_id + ".zip";

                String file_title = datas.get(position).getTitle().toString();
                String publishversion = datas.get(position).getPublish_ver().toString();
                String fav = datas.get(position).getFav_count().toString();
                String com = datas.get(position).getComment_count().toString();
                String view = datas.get(position).getView_count().toString();

                Intent intent = new Intent(context, Offline_view.class);
                intent.putExtra("url_id", url_id);
                intent.putExtra("d_url", download_link);
                intent.putExtra("file_title", file_title);
                intent.putExtra("publishV", publishversion);
                intent.putExtra("fav", fav);
                intent.putExtra("com", com);
                intent.putExtra("view", view);


                context.startActivity(intent);
            }
        });

    }

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

Offline_ViewHolder.class

public class Offline_ViewHolder extends RecyclerView.ViewHolder {
    NetworkImageView image;
    TextView title;
    TextView tv1,v_count,c_count,f_count;
    Offline_Adapter adapter;
    public Offline_ViewHolder(View itemView) {
        super(itemView);
        image=(NetworkImageView)itemView.findViewById(R.id.img_thumbnail);
        title=(TextView)itemView.findViewById(R.id.name);
        v_count= (TextView) itemView.findViewById(R.id.viewcount_txt);
        c_count= (TextView) itemView.findViewById(R.id.comment_text);
        f_count= (TextView) itemView.findViewById(R.id.fav_text);

   }
}

recyclerview with button

2 个答案:

答案 0 :(得分:0)

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
       if(position==0){
         youbutton.setOnclickListener();
       }else{
       }
    }

在onBindViewHolder中使用位置。

答案 1 :(得分:0)

你这里做错了

@Override
    public Offline_ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        b1=(Button)findViewById(R.id.b1);
        b2=(Button)findViewById(R.id.b2);

为什么要在RecyclerView Adapter类的onCreateViewHolder()中执行此操作?从您的代码看,这些按钮似乎在Activity或Fragment布局中。对? 如果是,请在Activity片段中找到它们并设置Click Listener to them。

下一步:在Adapter类中创建一个公共方法,在Adapter类中创建一个状态变量。

    public class Offline_Adapter extends RecyclerView.Adapter<Offline_ViewHolder> {
         Context context;
private boolean mIsOfflineMode = false;

        RequestQueue mRequestQueue;
        ImageLoader mImageLoader;
        DBHelper myDB;
        ArrayList<Offers> datas;

和适配器中的公共方法

   public void setOfflineSate(boolean state){
     mIsOfflineMode  = state;
 this.notifyDataSetChanged();
    }

下一步:现在是时候来处理适配器的onCreateViewHolder方法中的视图了。

   @Override
    public Offline_ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view ;
        if(isOffileMode){
        view = LayoutInflater.from(v.getContext()).inflate(R.layout.offline_data_card, null);     
    }else{
     view=LayoutInflater.from(v.getContext()).inflate(R.layout.offline_data_card_view2,null);
    }

     Offline_ViewHolder ovh=new Offline_ViewHolder(view);
        return ovh;
   }

现在在您的活动或片段类中,处理按钮单击

 // I am supposing you have Offline_Adapter Object as mOffLineAdapter and is  initialized 
 b1=(Button)parent.findViewById(R.id.b1);
        b2=(Button)parent.findViewById(R.id.b2);

        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
             mOffLineAdapter.setOfflineSate(false);
            }
        });
        b2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               mOffLineAdapter.setOfflineSate(true);
            }
        });