retrofit+RecyclerView+SwipeRefreshLayout 드 롭 다운 새로 고침 업 로드+item 클릭

//  ApiService
public interface ApiService {
    //http://app.vmoiver.com/apiv3/post/getPostInCate?cateid=0&p=1
    @GET("/apiv3/post/getPostInCate?cateid=0&p=1")
    Call getList();

}
//item    
public interface OnItemClickLitener {
    void onItemClick(View view, int position);
}

//adapter
public class RecyclerAdapter extends RecyclerView.Adapter {
    private List list;
    private Context context;

    public RecyclerAdapter(List list, Context context) {
        this.list = list;
        this.context = context;
    }
 private OnItemClickLitener mOnItemClickLitener;

 public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
    this.mOnItemClickLitener = mOnItemClickLitener;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) { LayoutInflater view = LayoutInflater.
from(
context); View inflate = view.inflate(R.layout.
one_item,
null); OneViewHolder viewHolder =
new OneViewHolder(inflate);
return viewHolder; }
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder,
int position) { ((OneViewHolder) holder).
oneText.setText(
list.get(position).
title);
           if (mOnItemClickLitener != null) {
               holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                  mOnItemClickLitener.onItemClick(viewHolder.itemView, position);
                  ObjectAnimator
                     .ofFloat(v, "rotationX", 0.0F, 360.0F)
                     .setDuration(500)
                     .start();
        }
    });
}

 }
@Override
public int getItemCount() {
return
list ==
null ?
0 :
list.size();
}
public class OneViewHolder
extends RecyclerView.ViewHolder {
public TextView
oneText;
public OneViewHolder(View itemView) {
super(itemView);
oneText = (TextView) itemView.findViewById(R.id.
one_text);
}
}}
/main 주 클래스
public class MainActivity extends AppCompatActivity {
    private SwipeRefreshLayout swip;
    private String urlPath = "http://app.vmoiver.com/apiv3/post/getPostInCate?cateid=0&p=1";
    private RecyclerView recyclerView;
    private RecyclerAdapter adapter;
    private List list = new ArrayList<>();
    private LinearLayoutManager lin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        swip=(SwipeRefreshLayout)findViewById(R.id.swip);
        recyclerView = (RecyclerView) findViewById(R.id.recycler);

        swip.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                loadData();
                adapter.notifyDataSetChanged();
                swip.setRefreshing(false);
            }
        });

        loadData();
        lin = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(lin);
        recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        adapter = new RecyclerAdapter(list, this);
        recyclerView.setAdapter(adapter);
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if(newState==RecyclerView.SCROLL_STATE_IDLE){
                    int position = lin.findLastVisibleItemPosition();
                    if(position==list.size()-1){
                        loadData();
                        adapter.notifyDataSetChanged();
                    }
                }
            }
        });
    }

    private void loadData() {
        Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl("http://app.vmoiver.com")
                .build();

        ApiService apiService = retrofit.create(ApiService.class);
        Call call = apiService.getList();
        call.enqueue(new Callback() {
            @Override
            public void onResponse(Call call, Response response) {
                Bean bean = response.body();
                list.addAll(bean.data);
                adapter.notifyDataSetChanged();
                adapter.setOnItemClickLitener(new OnItemClickLitener() {
                    @Override
                    public void onItemClick(View view, int position) {
                        Toast.makeText(MainActivity.this,position+" ",Toast.LENGTH_SHORT).show();
                    }
                });
            }

            @Override
            public void onFailure(Call call, Throwable t) {

            }
        });
    }

}
 

좋은 웹페이지 즐겨찾기