多布局

adapter

package com.example.myduobuju.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.myduobuju.R;
import com.example.myduobuju.bean.MyData;

import java.util.ArrayList;

public class MyAdapter extends RecyclerView.Adapterimplements View.OnClickListener {
  private ArrayList mList;
  private final int ONE_ITEM=1;
  private final int TWO_ITEM=2;

    public MyAdapter(ArrayList mList) {
        this.mList = mList;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
      View view=null;
      RecyclerView.ViewHolder holder=null;
      switch (viewType){
          case ONE_ITEM:
              view=View.inflate(viewGroup.getContext(), R.layout.recy_item_one,null);
              holder=new OneHolder(view);
              break;
          case TWO_ITEM:
              view=View.inflate(viewGroup.getContext(),R.layout.recy_item_two,null);
              holder=new TwoHolder(view);

              break;

      }
view.setOnClickListener(this);

        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {

        MyData myData=mList.get(i);

        if(viewHolder instanceof OneHolder){
            ((OneHolder) viewHolder).mName.setText(myData.getName());

        }else if(viewHolder instanceof TwoHolder){
            ((TwoHolder) viewHolder).mImage.setImageResource(myData.getImage());

        }


        viewHolder.itemView.setTag(i);
    }

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

    @Override
    public int getItemViewType(int position) {

        if(position%2==1){
            return ONE_ITEM;
        }else{
            return TWO_ITEM;

        }
    }

    class OneHolder extends RecyclerView.ViewHolder{

        private TextView mName;

        public OneHolder(@NonNull View itemView) {
            super(itemView);
            mName = itemView.findViewById(R.id.One_Text);
        }
    }

    class TwoHolder extends RecyclerView.ViewHolder{


        private ImageView mImage;

        public TwoHolder(@NonNull View itemView) {
            super(itemView);
            mImage = itemView.findViewById(R.id.Image);
        }
    }

    public interface ItemClick{
        void setOnItem(View v,int position);
    }
    private ItemClick itemClick;

    public void setOnItemClick(ItemClick itemClick){
        this.itemClick=itemClick;
    }
    @Override
    public void onClick(View v) {

        if(itemClick!=null){
            itemClick.setOnItem(v, (Integer) v.getTag());
        }
    }
}


activity

package com.example.myduobuju;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import com.example.myduobuju.adapter.MyAdapter;
import com.example.myduobuju.bean.MyData;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private RecyclerView Recy;

    private int[] images={R.mipmap.a1,R.mipmap.a2,R.mipmap.a3};
    private ArrayList mList=new ArrayList<>();
    private MyAdapter myAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
        myAdapter = new MyAdapter(mList);
        Recy.setAdapter(myAdapter);
        myAdapter.setOnItemClick(new MyAdapter.ItemClick() {
            @Override
            public void setOnItem(View v, int position) {
                Toast.makeText(MainActivity.this,mList.get(position).getName(),Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void initData() {

        for (int i = 0; i <30 ; i++) {

            mList.add(new MyData("真漂亮"+i,images[i%images.length]));
        }
    }


    private void initView() {
        Recy = (RecyclerView) findViewById(R.id.Recy);
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this);
        Recy.setLayoutManager(linearLayoutManager);
    }
}

你可能感兴趣的:(多布局)