关于okhttputils的简单使用(含 XRecyclerView的加载,刷新)2

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.example.lenovo.shishnagyuan.MainActivity;
import com.example.lenovo.shishnagyuan.R;
import com.example.lenovo.shishnagyuan.adapter.MyClosetSQList;
import com.example.lenovo.shishnagyuan.adapter.MySp;
import com.example.lenovo.shishnagyuan.adapter.Viewpager;
import com.example.lenovo.shishnagyuan.url.MyCommodityJavaBean;
import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.io.Serializable;
import java.util.ArrayList;

public class ShangPinX extends AppCompatActivity implements View.OnClickListener{
private Button xq_gm;
private RelativeLayout xq_gwc;
private TextView xq_log1,xq_log2,xq_jg,xq_ckj,xq_wodeyichu,xq_text,xq_jgg;
private ImageView xq_heixin,xq_wodeyic;
private String name;
private int id1;
private ViewPager xq_img1;
private String sqlid;
private long i;
private android.support.v4.app.FragmentManager fm;
private ArrayList<Fragment> liat;
private Viewpager adapter;
private String thumb,price;
private Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
int arg1 = msg.arg1;
if (i%2==0){
xq_img1.setCurrentItem(0);
}else {
xq_img1.setCurrentItem(1);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shang_pin_x);

xq_gm = (Button) findViewById(R.id.xq_gm);
xq_log1 = (TextView) findViewById(R.id.xq_log1);
xq_log2 = (TextView) findViewById(R.id.xq_log2);
xq_jg = (TextView) findViewById(R.id.xq_jg);
xq_ckj = (TextView) findViewById(R.id.xq_ckj);
xq_img1 = (ViewPager) findViewById(R.id.xq_img1);
xq_heixin = (ImageView) findViewById(R.id.xq_heixin);
xq_wodeyic = (ImageView) findViewById(R.id.xq_wodeyic);
xq_wodeyichu = (TextView) findViewById(R.id.xq_wodeyichu);
xq_gwc = (RelativeLayout) findViewById(R.id.xq_gwc);
xq_text = (TextView) findViewById(R.id.xq_text);
xq_jgg= (TextView) findViewById(R.id.xq_jgg);

Intent intent=getIntent();
Serializable id = intent.getSerializableExtra("id1");
String str=(String) id;
id1=Integer.parseInt(str);
String url="http://atp.fulishe.com/ClientApi/category.php?api_version=1.0&act=search_category_goods_list&" +
"c_id=35&order_price=0&page_num=20&page=1&debug=true&client_id=null";
OkHttpUtils
.get()
.url(url)
.build()
.execute(new StringCallback() {
@Override
public void onError(Request request, Exception e) {
}
@Override
public void onResponse(String response) {
MyCommodityJavaBean javaBrea= JSONObject.parseObject(response,MyCommodityJavaBean.class);
price=javaBrea.getInfo().getGoods().get(id1).getPromote_price();
name=javaBrea.getInfo().getGoods().get(id1).getGoods_name();
xq_log1.setText(name);
thumb = javaBrea.getInfo().getGoods().get(id1).getThumb();
xq_log2.setText(javaBrea.getInfo().getGoods().get(id1).getEnglish_name());
// xq_ckj.append(javaBrea.getInfo().getGoods().get(id1).getGoods_id());
xq_text.setText(javaBrea.getInfo().getGoods().get(id1).getDescription());
xq_jg.setText(javaBrea.getInfo().getGoods().get(id1).getCurrency_price()+"");
xq_jgg.setText(javaBrea.getInfo().getGoods().get(id1).getCurrency_price()+"");
fm=getSupportFragmentManager();
liat=new ArrayList<>();
liat.add(new Ncarousel(thumb));
liat.add(new NcarouselA());
xq_img1.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
return true;
}
});
adapter = new Viewpager(fm,liat);
xq_img1.setAdapter(adapter);
new Thread(){
@Override
public void run() {
super.run();
while (true){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Message message=new Message();
message.arg1=(int)i++;
handler.sendMessage(message);
}
}
}.start();
}
});
xq_gwc.setOnClickListener(this);
}
















@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.xq_gwc:
MySp sp = new MySp();
String flag = sp.getFlag(ShangPinX.this);
if (flag.equals("成功")){
String gwc = sp.getGWC(ShangPinX.this);
if (gwc.equals("0")){
xq_wodeyichu.setTextColor(Color.parseColor("#FFB6C1"));
xq_heixin.setImageResource(R.drawable.xiaoheixin);
MyClosetSQList sqList=new MyClosetSQList(ShangPinX.this);
ContentValues values=new ContentValues();
values.put("img",thumb);
values.put("name",name);
values.put("price",price);
sqList.insert(values);
sp.setGWC(ShangPinX.this,"1");
Toast.makeText(this, "你已经加入到购物车", Toast.LENGTH_SHORT).show();
}else {
xq_wodeyichu.setTextColor(Color.parseColor("#000000"));
xq_heixin.setImageResource(R.drawable.heixin);
MyClosetSQList sqList=new MyClosetSQList(ShangPinX.this);
sp.setGWC(ShangPinX.this,"0");
Cursor cursor=sqList.query();
while (cursor.moveToNext()){
}
}
}else {
Intent intent=new Intent(ShangPinX.this, MainActivity.class);
startActivity(intent);
}
break;
}
}
}






//布局

<ImageView
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_marginTop="15dp"
android:id="@+id/newon_iv"/>

<LinearLayout
android:background="@mipmap/mengceng"
android:layout_height="50dp"
android:layout_width="270dp"
android:orientation="vertical"
android:layout_marginLeft="15dp"
android:layout_alignLeft="@+id/newon_iv"
android:layout_alignRight="@+id/newon_iv"
android:layout_alignBottom="@+id/newon_iv">

<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/newon_tv1"
android:layout_marginLeft="10dp"
android:textColor="#ffffff"
android:textSize="18dp"/>

<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="horizontal">

<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/newon_tv2"
android:layout_marginLeft="10dp"
android:textColor="#ffffff"
android:textSize="18dp"
android:singleLine="true"
android:ems="4"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/newon_tv3"
android:layout_marginLeft="40dp"
android:textColor="#ffffff"
android:textSize="18dp"
android:singleLine="true"
android:ems="6"
android:paddingLeft="20dp"/>
</LinearLayout>
</LinearLayout>





转载于:https://www.cnblogs.com/yudada/p/7571287.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
介绍:一个实现了下拉刷新,滚动到底部加载更多以及添加header功能的的RecyclerView使用方式和RecyclerView完全一致,不需要额外的layout,不需要写特殊的adater。 加载效果内置了AVLoadingIndicatorView上的所有效果,可以根据需要指定。项目地址:https://github.com/jianghejie/XRecyclerView 效果:使用:xml<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     tools:context=".MainActivity">       <com.jcodecraeer.xrecyclerview.XRecyclerView         android:id="@ id/recyclerview"         android:layout_width="fill_parent"         android:layout_height="fill_parent" />   </RelativeLayout>2.MainActivitypackage com.example.xrecyclerview;   import android.os.Bundle; import android.os.Handler; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup;   import com.jcodecraeer.xrecyclerview.ProgressStyle; import com.jcodecraeer.xrecyclerview.XRecyclerView;   import java.util.ArrayList;   public class MainActivity extends AppCompatActivity {     private XRecyclerView mRecyclerView;     private MyAdapter mAdapter;     private ArrayList<String> listData;     private int refreshTime = 0;     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         mRecyclerView = (XRecyclerView)this.findViewById(R.id.recyclerview);         LinearLayoutManager layoutManager = new LinearLayoutManager(this);         layoutManager.setOrientation(LinearLayoutManager.VERTICAL);         mRecyclerView.setLayoutManager(layoutManager);           mRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);         mRecyclerView.setLaodingMoreProgressStyle(ProgressStyle.Pacman);         mRecyclerView.setArrowImageView(R.drawable.iconfont_downgrey);           View header =   LayoutInflater.from(this).inflate(R.layout.recyclerview_header, (ViewGroup)findViewById(android.R.id.content),false);         mRecyclerView.addHeaderView(header);             mRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {             @Override             public void onRefresh() {                 refreshTime  ;                 new Handler().postDelayed(new Runnable(){                     public void run() {                         listData.clear();                         for(int i = 0; i < 15 ;i ){                             listData.add("item"   i   "after "   refreshTime   " times of refresh");                         }                         mAdapter.notifyDataSetChanged();                         mRecyclerView.refreshComplete();                     }                   }, 3000);            //refresh data here             }               @Override             public void onLoadMore() {                 new Handler().postDelayed(new Runnable(){                     public void run() {                         for(int i = 0; i < 15 ;i ){                             listData.add("item"   (i   listData.size()) );                         }                         mAdapter.notifyDataSetChanged();                         mRecyclerView.loadMoreComplete();                     }                 }, 3000);               }         });           listData = new  ArrayList<String>();         mAdapter = new MyAdapter(listData);         for(int i = 0; i < 15 ;i ){             listData.add("item"   i);         }         mRecyclerView.setAdapter(mAdapter);     }       @Override     public boolean onCreateOptionsMenu(Menu menu) {         // Inflate the menu; this adds items to the action bar if it is present.         getMenuInflater().inflate(R.menu.menu_main, menu);         return true;     }       @Override     public boolean onOptionsItemSelected(MenuItem item) {         // Handle action bar item clicks here. The action bar will         // automatically handle clicks on the Home/Up button, so long         // as you specify a parent activity in AndroidManifest.xml.         int id = item.getItemId();           //noinspection SimplifiableIfStatement         if (id == R.id.action_settings) {             return true;         }           return super.onOptionsItemSelected(item);     }     }3. adapterpackage com.example.xrecyclerview;   import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView;   import java.util.ArrayList;   /**  * Created by jianghejie on 15/11/26.  */ public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {     public ArrayList<String> datas = null;     public MyAdapter(ArrayList<String> datas) {         this.datas = datas;     }     //创建新View,被LayoutManager所调用     @Override     public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {         View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item,viewGroup,false);         ViewHolder vh = new ViewHolder(view);         return vh;     }     //将数据与界面进行绑定的操作     @Override     public void onBindViewHolder(ViewHolder viewHolder, int position) {         viewHolder.mTextView.setText(datas.get(position));     }     //获取数据的数量     @Override     public int getItemCount() {         return datas.size();     }     //自定义的ViewHolder,持有每个Item的的所有界面元素     public static class ViewHolder extends RecyclerView.ViewHolder {         public TextView mTextView;         public ViewHolder(View view){             super(view);             mTextView = (TextView) view.findViewById(R.id.text);         }     } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值