package com.example.ykdemo.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.example.ykdemo.R;
import com.example.ykdemo.bean.InfoData;
public class MyInfoadapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
Context context;
InfoData fd;
public MyInfoadapter(Context context, InfoData fd) {
this.context = context;
this.fd = fd;
}
public static final int TYPE_ONE = 0;
public static final int TYPE_TWO = 1;
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if(viewType == TYPE_ONE){
View view = View.inflate(context, R.layout.recy_item1,null);
ViewHolder holder = new ViewHolder(view);
return holder;
}else{
View view = View.inflate(context, R.layout.recy_item11,null);
ViewHolder1 holder1=new ViewHolder1(view);
return holder1;
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if(holder instanceof ViewHolder){
((ViewHolder) holder).title.setText(fd.getStories().get(position).getTitle());
Glide.with(context).load(fd.getStories().get(position).getImages().get(0)).into(((ViewHolder) holder).iv);
}
if(holder instanceof ViewHolder1){
holder.itemView.setTag(position);
((ViewHolder1) holder).title1.setText(fd.getStories().get(position).getTitle());
Glide.with(context).load(fd.getStories().get(position).getImages().get(0)).into(((ViewHolder1) holder).iv1);
}
}
@Override
public int getItemCount() {
return fd==null?0:fd.getStories().size();
}
@Override
public int getItemViewType(int position) {
if(position %2 == 0){
return TYPE_ONE;
}else{
return TYPE_TWO;
}
}
public class ViewHolder extends RecyclerView.ViewHolder{
TextView title,name,author;
ImageView iv;
public ViewHolder(View itemView) {
super(itemView);
title = itemView.findViewById(R.id.title);
iv = itemView.findViewById(R.id.iv);
}
}
public class ViewHolder1 extends RecyclerView.ViewHolder{
TextView title1,name1,author1;
ImageView iv1;
public ViewHolder1(View itemView) {
super(itemView);
title1 = itemView.findViewById(R.id.title1);
iv1 = itemView.findViewById(R.id.iv1);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/iv"
android:layout_width="180px"
android:layout_height="180px"
android:src="@mipmap/ic_launcher"/>
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/iv"
android:text="标题"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:textSize="20sp"/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/title1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/iv1"
android:text="标题"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:textSize="20sp"/>
<ImageView
android:id="@+id/iv1"
android:layout_width="180px"
android:layout_height="180px"
android:src="@mipmap/ic_launcher"
android:layout_alignParentRight="true"/>
</RelativeLayout>
package com.example.ykdemo.activity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import com.example.ykdemo.R;
import com.example.ykdemo.adapter.MyInfoadapter;
import com.example.ykdemo.bean.InfoData;
import com.example.ykdemo.okhttp.OKHttpUtils;
import com.example.ykdemo.okhttp.OnUiCallback;
import com.google.gson.Gson;
import java.io.IOException;
import okhttp3.Call;
public class InfoActivity extends AppCompatActivity {
RecyclerView info_rv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_info);
initview();
initData();
}
private void initview() {
info_rv= (RecyclerView) findViewById(R.id.info_rv);
LinearLayoutManager mar=new LinearLayoutManager(this);
info_rv.setLayoutManager(mar);
}
private void initData() {
OKHttpUtils.getInstance().doGet("http://news-at.zhihu.com/api/4/theme/11", new OnUiCallback() {
@Override
public void onFailed(Call call, IOException e) {
}
@Override
public void onsuccess(String result) throws IOException {
InfoData fd=new Gson().fromJson(result,InfoData.class);
MyInfoadapter adapter=new MyInfoadapter(InfoActivity.this,fd);
info_rv.setAdapter(adapter);
}
});
}
}