图中这样一个界面的实现用到了OkHttp获取数据,fastjson解析数据,RecyclerView呈现出来。
【1】布局
【1】activity的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_my_gift_scene1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/bg_my"
android:orientation="vertical"
tools:context="com.example.sweetgirl.magiccup1.outdated.MyGiftScene1Activity">
<include layout="@layout/my_gift_scene1_toolbar"/>
<TextView
android:layout_width="match_parent"
android:layout_height="3dp"/>
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/layout_swipe_refresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/new_recyclerView"/>
</android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>
【2】item的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:background="@drawable/edit_view_shape"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp">
<TextView
android:id="@+id/scene1_tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textColor="@color/text_b"
android:text="唠叨"
android:paddingTop="10dp"
tools:ignore="HardcodedText,SpUsage" />
<TextView
android:id="@+id/scene1_tv_dep"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:textSize="14sp"
android:text="说你是爱你"
android:textColor="@color/text"
tools:ignore="HardcodedText" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.jit.lib.SmartImageView
android:id="@+id/scene1_iv_photo"
android:layout_width="wrap_content"
android:layout_height="200dp"
android:scaleType="fitXY"
android:layout_marginTop="8dp"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_marginBottom="12dp"
android:src="@mipmap/iv_item"
tools:ignore="ContentDescription" />
</LinearLayout>
</LinearLayout>
【2】JavaBean
public class TextImage {
public String name; //名字
public String dep; //描述
public String image; //图片
public TextImage(String image, String text,String text1) {
this.image = image;
this.name = text;
this.dep=text1;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDep() {
return dep;
}
public void setDep(String dep) {
this.dep = dep;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
}
【3】Adapter
public class MyRecycleViewAdapter extends RecyclerView.Adapter<MyRecycleViewAdapter.ViewHolder> {
// private final Context context;
private ArrayList<String> datas;
private LayoutInflater layoutInflater;
private VolleySingleton volleySingleton;
private ImageLoader imageLoader;
private DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
private int previousPosition=0;
private ArrayList<TextImage> textImages=new ArrayList<>();
public MyRecycleViewAdapter(Context context){
// this.context=context;
//this.datas=datas;
layoutInflater = LayoutInflater.from(context);
volleySingleton = VolleySingleton.getInstance(context);
imageLoader = volleySingleton.getImageLoader();
}
public void setTextImages(ArrayList<TextImage> textImages){
this.textImages=textImages;
notifyItemMoved(0,textImages.size());
}
/**
*相当于getView 方法中创建view和ViewHolder
* * @param
*
* */
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent,int viewType){
View itemView=layoutInflater.inflate(R.layout.item_scene1, parent,false);
ViewHolder viewHolder=new ViewHolder(itemView);
return viewHolder;
}
/**
* 相当于getView 方法中创建view和ViewHolder
* @param
*
*/
@Override
public void onBindViewHolder(ViewHolder holder, int position){
// String data=datas.get(position);
//holder.mText.setText(data);
TextImage currentTextImage=textImages.get(position);
holder.mText.setText(currentTextImage.getName());
holder.mDep.setText(currentTextImage.getDep());
holder.itemView.setTag(position);
String url=currentTextImage.getImage();
loadImages(url,holder);
}
private void loadImages(String urlThumbnail, final ViewHolder holder) {
if (!urlThumbnail.equals(Constants.NA)) {
imageLoader.get(urlThumbnail, new ImageLoader.ImageListener() {
@Override
public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
holder.mImage.setImageBitmap(response.getBitmap());
}
@Override
public void onErrorResponse(VolleyError error) {
}
});
}
}
/**
*相当于getView 方法中创建view和ViewHolder
* @param
*/
@Override
public int getItemCount(){
//return datas.size();
return textImages.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
TextView mDep;
TextView mText;
ImageView mImage;
public ViewHolder(View itemView){
super(itemView);
mText = (TextView)itemView.findViewById(R.id.scene1_tv_name);
mImage = (ImageView)itemView.findViewById(R.id.scene1_iv_photo);
mDep=(TextView)itemView.findViewById(R.id.scene1_tv_dep);
//设置item的点击事件
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (onItemClickListener!=null){
onItemClickListener.OnItemClick(v,(int)v.getTag());
}
}
});
}
}
//定义点击事件的接口
public interface OnItemClickListener{
public void OnItemClick(View view,int position);
}
private OnItemClickListener onItemClickListener;
public void setOnItemClickListener(OnItemClickListener onItemClickListener){
this.onItemClickListener=onItemClickListener;
}
}
【4】Activity
【1】找到控件
private RecyclerView mRecyclerView;
private SwipeRefreshLayout mRefreshLayout;
mRecyclerView=(RecyclerView) findViewById(R.id.new_recyclerView);
mRefreshLayout=(SwipeRefreshLayout)findViewById(R.id.layout_swipe_refresh);
【2】下载数据
ArrayList<TextImage> list=new ArrayList<>();
private boolean doGet(){
new Thread(new Runnable() {
@Override
public void run() {
try {
enqueue();
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
return false;
}
private void enqueue(){
//[1]拿到OkHttpClient
OkHttpClient client = new OkHttpClient();
//[2]构造Request
Request request = new Request.Builder()
.url(path)
.build();
//[3]将Request封装为call
Call call=client.newCall(request);
//[4]执行call
call.enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
L.i(TAG,"onFailure"+e.getMessage());
e.printStackTrace();
}
@Override
public void onResponse(Response response) throws IOException {
String res=response.body().string();
L.i(TAG,"onResponse"+res);
jsonParser(res);
}
});
}
[3]解析数据
private List<TextImage> jsonParser(String jsonData) {
try {
JSONTokener jsonTokener = new JSONTokener(jsonData);
org.json.JSONObject jsonObject = (org.json.JSONObject) jsonTokener.nextValue();
org.json.JSONArray array = jsonObject.getJSONArray("data");
for (int i = 0; i < array.length(); i++) {
org.json.JSONObject object = array.getJSONObject(i);
String name=object.getString("name");
String image=object.getString("image");
String dep=object.getString("depiction");
L.i(TAG,name);
L.i(TAG,image);
L.i(TAG,dep);
TextImage textImage=new TextImage(image,name,dep);
list.add(textImage);
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
[4]设置数据
private MyRecycleViewAdapter mmAdapter;
mmAdapter.setTextImages(list);
[5]设置Adapter
LinearLayoutManager mLinearLayoutManager = new LinearLayoutManager(this);
mmAdapter=new MyRecycleViewAdapter(RecyclerViewActivity.this);
mRecyclerView.setLayoutManager(mLinearLayoutManager);
mRecyclerView.addItemDecoration(new SpaceItemDecoration(12,8,12,30));
mRecyclerView.setAdapter(mmAdapter);
mmAdapter.setTextImages(list);
mmAdapter.setOnItemClickListener(new MyRecycleViewAdapter.OnItemClickListener() {
@Override
public void OnItemClick(View view,int position) {
mName=list.get(position).getName();
Toast.makeText(RecyclerViewActivity.this,"你选择了"+mName,Toast.LENGTH_SHORT).show();
}
});
mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener(){
public void onRefresh() {
//数据重新加载完成后,提示数据发生改变,并且设置现在不在刷新
mmAdapter.notifyDataSetChanged();
mRefreshLayout.setRefreshing(false);
}
});
mRecyclerView.addOnScrollListener(new EndLessOnScrollListener(mLinearLayoutManager) {
@Override
public void onLoadMore(int currentPage) {
loadMoreData();
}
});
//每次上拉加载的时候,给RecyclerView的后面添加了10条数据数据
private void loadMoreData(){
for (int i =0; i < 10; i++){
mmAdapter.notifyDataSetChanged();
}
}
【5】分享
最近看了一本书,叫《街猫》----Le Matou
这本书,陪伴了我一整个寒假,书中有黄金屋,说的就是这样的书吧。
另外最近还看到这样一句话,很喜欢,与你分享。
真正的好机会,永远都存在与未知中