Recycleview 多布局添加和cardview使用

         CardView是Android 5.0系统引入的控件,相当于FragmentLayout布局控件然后添加圆角及阴影的效果;CardView被包装为一种布局,并且经常在ListView和RecyclerView的Item布局中,作为一种容器使用。CardView应该被使用在显示层次性的内容时;在显示列表或网格时更应该被选择,因为这些边缘可以使得用户更容易去区分这些内容。 

使用地方:ListView和RecyclerView的Item布局中。cardview在所有布局外面

?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 

android:layout_width="match_parent" 

android:layout_height="match_parent">

 <android.support.v7.widget.CardView 

android:id="@+id/cardView" 

android:layout_width="match_parent"

 android:layout_height="wrap_content"

 android:layout_margin="10dp"> 

<LinearLayout android:layout_width="match_parent" 

android:layout_height="100dp">

 <ImageView

android:layout_width="150dp" 

android:layout_height="match_parent" 

android:layout_margin="5dp" 

android:scaleType="centerCrop" 

android:src="@drawable/sng" /> 

<LinearLayout android:layout_width="match_parent" 

android:layout_height="match_parent" 

android:orientation="vertical"> 

<TextView

android:layout_width="match_parent" 

android:layout_height="wrap_content"

 android:padding="5dp" 

android:text="棒冰行动"

 android:textSize="18sp" 

android:textStyle="bold" /> 

<TextView 

android:layout_width="match_parent" 

android:layout_height="wrap_content"

 android:padding="5dp" 

ndroid:text="棒冰行动,公益传播设计夏令营" /> 

</LinearLayout> 

</LinearLayout> 

</android.support.v7.widget.CardView> 

</LinearLayout>



二、cardview的activty代码

public class actvity extends AppCompatActivity {  

private CardView cardView;  

@Override  

protected void onCreate(Bundle savedInstanceState) { 

super.onCreate(savedInstanceState);  

setContentView(R.layout.activity_main);  

cardView = (CardView)findViewById(R.id.cardView);

cardView.setRadius(8);//设置图片圆角的半径大小

cardView.setCardElevation(8);//设置阴影部分大小 

cardView.setContentPadding(5,5,5,5);//设置图片距离阴影大小

}

三、多布局添在。意味着在adapter中即可能添加子布局1,也可能添加子布局2.甚至更复杂。

1.现在加载子布局了一个新的view2,同时我们现在需要确定的是谁到底和holder绑在一起,是view呢?还是view2呢?核心的地方在viewtype,viewtype就是我们要加载的view的是一个int值,在这里它有一个重写的方法,同时设计两个静态的变量记得赋值。

public static int bigimage=1;
public static int Smallimage=2;

@Override
public int getItemViewType(int position) {
    if(position%3==0)
    {
        return bigimage;
    }
    return Smallimage;
}
2,同时要在第一个方面里面对view的类型进行判端然后加载不同同的布局,在进行反出不同的布局
 if(viewType==Smallimage)
    {
         view=inflater.from(parent.getContext()).inflate(R.layout.listitem,null);
        return new ViewHolder(view);
    }else
        {
            Log.e("msg","bigimage"+bigimage);
            view=inflater.from(parent.getContext()).inflate(R.layout.layoutitem2,null);
            return new ViewHolder2(view);
        }

3.再第二个方法进行传递数据时,需要判断绑定过来的这个时候就需要判断要进行添加数据的是那个viewhoder了,所以需要进行父类向子类转型和判断
if(fatherholder instanceof ViewHolder)

ViewHolder holder=(ViewHolder)fatherholder;

fatherholder
@Override
public void onBindViewHolder(RecyclerView.ViewHolder fatherholder, final int position)
{
    Log.e("msg","111111111111111111");
    if(fatherholder instanceof ViewHolder)
    {
        Log.e("msg","88888888888888888888888888888888888");
    ViewHolder holder=(ViewHolder)fatherholder;
    holder.tv1.setText(list.get(position).getSongname());
    holder.tv2.setText(list.get(position).getSingername());
    Picasso.with(context)
            .load(list.get(position).getAlbumpic_big())
            .error(R.mipmap.ic_launcher)
            .placeholder(R.drawable.dibu8)
            .into(holder.IV);

    View view=holder.itemView;
    view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(context,list.get(position).getSongname(),Toast.LENGTH_LONG).show();

            manager.cutmusic(list.get(position).getUrl());

            if(listen!=null) {
                listen.onitemclick(v, position);
            }
        }
    });
}if(fatherholder instanceof ViewHolder2)
    {
        ViewHolder2 holder=(ViewHolder2)fatherholder;
        Picasso.with(context)
                .load(list.get(position).getAlbumpic_big())
                .error(R.mipmap.ic_launcher)
                .placeholder(R.drawable.dibu8)
                .into(holder.IV1);
        Log.e("msg","9999999999999999999999999999999999999");
    }
      Log.e("msg","22222222222222222222");

}



4.由于现在两个布局的id不同,那么现在就需要有两个viewholder,同时在适配器开始的地方
public class recycleadapter extends RecyclerView.Adapter<recycleadapter.ViewHolder>写的也要是recycleadapter.ViewHolder,记住。
class ViewHolder extends RecyclerView.ViewHolder
    {

        TextView tv1,tv2;
        private ImageView IV;

        public ViewHolder(View itemView) {
            super(itemView);
//            tv = (TextView)itemView.findViewById(R.id.recycleitem);
//            IV=(ImageView)itemView.findViewById(R.id.imageView10);
            tv1=itemView.findViewById(R.id.textView4song);
            tv2=itemView.findViewById(R.id.textViewname);
            IV=itemView.findViewById(R.id.imageView5);
        }


    }
    class ViewHolder2 extends RecyclerView.ViewHolder
    {


        private ImageView IV;

        public ViewHolder2(View itemView) {
            super(itemView);
            IV=itemView.findViewById(R.id.textViewname);
        }


    }

//这个完整多种布局recycleview的适配器写法
public class recycleadapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private ArrayList<josntop.ShowapiResBodyBean.PagebeanBean.SonglistBean> list=new ArrayList<josntop.ShowapiResBodyBean.PagebeanBean.SonglistBean>();
    public LayoutInflater inflater;
    private Context context;
    public static int bigimage=1;
    public static int Smallimage=2;
    int a;


    private Playmanager manager;
    public void setdata(ArrayList<josntop.ShowapiResBodyBean.PagebeanBean.SonglistBean> list)
    {
        this.list=list;
        notifyDataSetChanged();
    }
    public recycleadapter(int a)
    {
         this.a=a;
    }
    public recycleadapter()
    {

    }
    public void delete(int position)
    {
        list.remove(position);
        notifyItemRemoved(position);

    }

    @Override
    public int getItemViewType(int position) {
        Log.e("msg","position"+position);
        if(position%3==0)
        {

            return bigimage;
        }

        return Smallimage;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        View view;
        context=parent.getContext();
        manager= Playmanager.getinstance();
        if(viewType==Smallimage)
        {
             view=inflater.from(parent.getContext()).inflate(R.layout.listitem,null);
            return new ViewHolder(view);
        }else
            {
                Log.e("msg","bigimage"+bigimage);
                view=inflater.from(parent.getContext()).inflate(R.layout.layoutitem2,null);
                return new ViewHolder2(view);
            }






    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder fatherholder, final int position)
    {
        Log.e("msg","111111111111111111");
        if(fatherholder instanceof ViewHolder)
        {
            Log.e("msg","88888888888888888888888888888888888");
        ViewHolder holder=(ViewHolder)fatherholder;
        holder.tv1.setText(list.get(position).getSongname());
        holder.tv2.setText(list.get(position).getSingername());
        Picasso.with(context)
                .load(list.get(position).getAlbumpic_big())
                .error(R.mipmap.ic_launcher)
                .placeholder(R.drawable.dibu8)
                .into(holder.IV);

        View view=holder.itemView;
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context,list.get(position).getSongname(),Toast.LENGTH_LONG).show();

                manager.cutmusic(list.get(position).getUrl());

                if(listen!=null) {
                    listen.onitemclick(v, position);
                }
            }
        });
    }if(fatherholder instanceof ViewHolder2)
        {
            ViewHolder2 holder=(ViewHolder2)fatherholder;
            Picasso.with(context)
                    .load(list.get(position).getAlbumpic_big())
                    .error(R.mipmap.ic_launcher)
                    .placeholder(R.drawable.dibu8)
                    .into(holder.IV1);
            Log.e("msg","9999999999999999999999999999999999999");
        }
          Log.e("msg","22222222222222222222");

    }




    @Override
    public int getItemCount() {
        Log.e("msg","3333333333333333333333");
        return list.size();
    }
    class ViewHolder extends RecyclerView.ViewHolder
    {

        TextView tv1,tv2;
        private ImageView IV;

        public ViewHolder(View itemView) {
            super(itemView);
//            tv = (TextView)itemView.findViewById(R.id.recycleitem);
//            IV=(ImageView)itemView.findViewById(R.id.imageView10);
            tv1=itemView.findViewById(R.id.textView4song);
            tv2=itemView.findViewById(R.id.textViewname);
            IV=itemView.findViewById(R.id.imageView5);
        }


    }
    class ViewHolder2 extends RecyclerView.ViewHolder
    {


        private ImageView IV1;

        public ViewHolder2(View itemView) {
            super(itemView);
            IV1=itemView.findViewById(R.id.item3333333333);
        }


    }
    public  interface Onitemlisten
    {
        void onitemclick(View view, int position);
    }
    Onitemlisten listen;
    public void SetOnItemclick(Onitemlisten listen)
    {
         this.listen=listen;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值