1. 程式人生 > >recyclerview多條目載入

recyclerview多條目載入

adapter介面卡

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import com.jcodecraeer.xrecyclerview.XRecyclerView;

import 
java.util.ArrayList; import java.util.List; /** * Created by MK on 2017/11/12. */ public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { Context context; JavaBean javaBean; // private List<JavaBean.SongListBean> list ; public MyAdapter(Context context, JavaBean javaBean) { this
.context = context; this.javaBean = javaBean; } @Override public int getItemViewType(int position) { if (position % 2 == 0) { return 0; } return 1; } /*public void addData(List<JavaBean.SongListBean> list){ if (this.list==null){
this.list=new ArrayList<>(); } list.addAll(list); notifyDataSetChanged(); }*/ @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { if (viewType == 0) { View view = View.inflate(context, R.layout.item_adapter, null); MyHodler myHodler = new MyHodler(view); return myHodler; } else { View view = View.inflate(context, R.layout.item02_adapter, null); MyHodler01 myHodler01 = new MyHodler01(view); return myHodler01; } } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) { if (holder instanceof MyHodler) { Glide.with(context) .load(javaBean.getSong_list().get(position).getPic_big()) .into(((MyHodler) holder).image01); ((MyHodler) holder).text01.setText(javaBean.getSong_list().get(position).getTitle()); } else { Glide.with(context) .load(javaBean.getSong_list().get(position).getPic_big()) .into(((MyHodler01) holder).image01); Glide.with(context) .load(javaBean.getSong_list().get(position).getPic_big()) .into(((MyHodler01) holder).image02); ((MyHodler01) holder).text01.setText(javaBean.getSong_list().get(position).getTitle()); } } @Override public int getItemCount() { return javaBean.getSong_list() == null ? 0 : javaBean.getSong_list().size(); } class MyHodler extends RecyclerView.ViewHolder { private final ImageView image01; private final TextView text01; public MyHodler(View itemView) { super(itemView); image01 = (ImageView) itemView.findViewById(R.id.image01); text01 = (TextView) itemView.findViewById(R.id.text01); itemView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View view) { if (onClick != null) { onClick.onItemClick(view, getAdapterPosition()); } return true; } }); } } class MyHodler01 extends RecyclerView.ViewHolder { private final ImageView image01; private final ImageView image02; private final TextView text01; public MyHodler01(View itemView) { super(itemView); image01 = (ImageView) itemView.findViewById(R.id.image01); image02 = (ImageView) itemView.findViewById(R.id.image02); text01 = (TextView) itemView.findViewById(R.id.text01); itemView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View view) { if (onClick != null) { onClick.onItemClick(view, getAdapterPosition()); } return true; } }); } } OnItemClickListener onClick; public void setOnClick(OnItemClickListener onClick) { this.onClick = onClick; } public interface OnItemClickListener { public void onItemClick(View view, int position); } } mainactivity
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.liaoinstan.springview.container.DefaultFooter;
import com.liaoinstan.springview.container.DefaultHeader;
import com.liaoinstan.springview.widget.SpringView;

import java.util.List;

public class ScondActivity extends AppCompatActivity implements MyView {

    private MyPresenter presenter;
    private int type = 1;
    private int size = 10;
    private XRecyclerView xRecyclerView;
    private MyAdapter myAdapter;
    private SpringView springView;
    private Handler handler = new Handler() {
        @Override
public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0) {
                size += 10;
                presenter.abc(type, size);
                myAdapter.notifyDataSetChanged();
                xRecyclerView.loadMoreComplete();
            }
        }
    };

    @Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scond);

        // xRecyclerView = (RecyclerView) findViewById(R.id.xrecyclerview_id);
        //springView = (SpringView) findViewById(R.id.springview);
xRecyclerView = (XRecyclerView) findViewById(R.id.xrecyclerview_id);
        presenter = new MyPresenter(this);

        presenter.abc(type, size);
    }

    @Override
public void init(final JavaBean javaBean) {

        //  Log.i("----",javaBean.getSong_list().get(3).getTitle());
LinearLayoutManager liner = new LinearLayoutManager(ScondActivity.this, LinearLayoutManager.VERTICAL, false);
        xRecyclerView.setLayoutManager(liner);
        myAdapter = new MyAdapter(ScondActivity.this, javaBean);
        xRecyclerView.setAdapter(myAdapter);

        xRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {

            @Override
public void onRefresh() {
                type++;

                presenter.abc(type, size);
                //myAdapter.addData(javaBean.getSong_list());
myAdapter.notifyDataSetChanged();
                xRecyclerView.refreshComplete();
            }

            @Override
public void onLoadMore() {
                handler.sendEmptyMessageDelayed(0, 1000);

            }
        });

        myAdapter.setOnClick(new MyAdapter.OnItemClickListener() {
            @Override
public void onItemClick(View view, int position) {
                Toast.makeText(ScondActivity.this, position + "", Toast.LENGTH_SHORT).show();
                List<JavaBean.SongListBean> song_list = javaBean.getSong_list();
                song_list.remove(position - 1);
                myAdapter.notifyDataSetChanged();
            }
        });

        /*springView.setHeader(new DefaultHeader(this));
        springView.setFooter(new DefaultFooter(this));*/
       /* springView.setListener(new SpringView.OnFreshListener() {
            @Override
            public void onRefresh() {
                type++;
                presenter.abc(type,size);
                myAdapter.notifyDataSetChanged();
            }
            @Override
            public void onLoadmore() {
                size+=10;
                presenter.abc(type,size);
                myAdapter.notifyDataSetChanged();
            }
        });*/
}
}