Activity界面网络解析 ,这里我们使用的是OkHttp3
private String mUrl = "http://v.juhe.cn/toutiao/index?type=top&key=097060266650f67b2cebd2a06aded587"; private RecyclerView mRecy; private ListmList = new ArrayList<>();
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
String jsonStr = (String) msg.obj;
Gson gson = new Gson();
MyData myData = gson.fromJson(jsonStr, MyData.class);
mList = myData.getResult().getData();
mAdapter = new MyAdapter(mList, MainActivity.this);
mRecy.setAdapter(mAdapter);
mAdapter.setOnItemListener(new MyAdapter.OnItemClick() {
@Override
public void setOnItem(View v, int position) {
Toast.makeText(MainActivity.this, mList.get(position).getTitle(), Toast.LENGTH_SHORT).show();
}
});
}
};
private MyAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
getInfo();
}
private void initView() {
mRecy = (RecyclerView) findViewById(R.id.Recycler);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(MainActivity.this);
mRecy.setLayoutManager(linearLayoutManager);
}
private void getInfo() {
OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder().url(mUrl).build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Message msg = new Message();
msg.obj = response.body().string();
mHandler.sendMessage(msg);
}
});
}
}
RecyclerView适配器 代码:
因为是多布局 所以写了需要数量的ViewHolder,模板当然也是需要数量的模板
在item 监听这块 我们直接监听view 就可以了。
public class MyAdapter extends RecyclerView.Adapterimplements View.OnClickListener { private ListmList; private Context mContext; private final static int ITEM_ONE = 1; private final static int ITEM_TWO = 2; public MyAdapter(ListmList, Context mContext) {
this.mList = mList;
this.mContext = mContext;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(mContext);
View view;
RecyclerView.ViewHolder holder;
if (viewType == ITEM_ONE) {
view = inflater.inflate(R.layout.recy_item_one, parent, false);
holder = new HolderOne(view);
} else {
view = inflater.inflate(R.layout.recy_item_two, parent, false);
holder = new HolderTwo(view);
}
view.setOnClickListener(this);
return holder;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
MyData.ResultBean.DataBean dataBean = mList.get(position);
if (holder instanceof HolderOne) {
((HolderOne) holder).mTextOne.setText(dataBean.getTitle());
Picasso.with(mContext).load(dataBean.getThumbnail_pic_s()).into(((HolderOne) holder).mImageOne);
((HolderOne) holder).itemView.setTag(position);
} else {
((HolderTwo) holder).mTextTwo.setText(dataBean.getTitle());
Picasso.with(mContext).load(dataBean.getThumbnail_pic_s()).into(((HolderTwo) holder).mImageTwo);
((HolderTwo) holder).itemView.setTag(position);
}
}
@Override
public int getItemCount() {
return mList.isEmpty() ? 0 : mList.size();
}
//控制到底用的是那个模板
@Override
public int getItemViewType(int position) {
if (position % 2 == 0) {
return ITEM_ONE;
} else {
return ITEM_TWO;
}
}
class HolderOne extends RecyclerView.ViewHolder {
private TextView mTextOne;
private ImageView mImageOne;
public HolderOne(View itemView) {
super(itemView);
mTextOne = (TextView) itemView.findViewById(R.id.Recy_One_Text);
mImageOne = (ImageView) itemView.findViewById(R.id.Recy_One_Image);
}
}
class HolderTwo extends RecyclerView.ViewHolder {
private TextView mTextTwo;
private ImageView mImageTwo;
public HolderTwo(View itemView) {
super(itemView);
mTextTwo = (TextView) itemView.findViewById(R.id.Recy_Two_Text);
mImageTwo = (ImageView) itemView.findViewById(R.id.Recy_Two_Image);
}
}
public interface OnItemClick {
void setOnItem(View v, int position);
}
private OnItemClick onitem;
@Override
public void onClick(View v) {
if (onitem != null) {
onitem.setOnItem(v, (int) v.getTag());
}
}
public void setOnItemListener(OnItemClick item) {
this.onitem = item;
}
}