==================================依赖==================================================
compile 'com.android.support:recyclerview-v7:26.0.0-alpha1' compile 'com.squareup.okhttp3:okhttp:3.9.0' compile 'com.squareup.okio:okio:1.13.0' compile 'com.jcodecraeer:xrecyclerview:1.3.2' compile 'com.facebook.fresco:fresco:0.9.0+' compile files('libs/gson-2.2.4.jar') compile files('libs/universal-image-loader-1.9.3-with-sources.jar') compile 'com.squareup.retrofit2:retrofit:2.0.1' compile 'com.squareup.retrofit2:converter-gson:2.0.1' compile 'com.squareup.retrofit2:adapter-rxjava:2.0.1' compile 'io.reactivex:rxandroid:1.1.0'
=================================API===========================================
public class Api { // //http://tingapi.ting.baidu.com/v1/restserver/ting?method=baidu.ting.billboard.billList&type=1&size=10&offset=0 public static final String api_man="http://tingapi.ting.baidu.com/v1/restserver/"; }
===================================ApiService=====================================
public interface ApiService { //ting?method=baidu.ting.billboard.billList&type=1&size=10&offset=0 @GET("ting") Observable<RecyBean> path(@Query("method") String method,@Query("type") int type,@Query("size") int size,@Query("offset") int offest); }
====================================================================================
public class OkHttpUtils { private Handler handler = new Handler(); public Handler getHandler(){ return handler; } //单例 private static OkHttpUtils okHttpUtils = new OkHttpUtils(); private OkHttpUtils(){}; public static OkHttpUtils getInstance(){ return okHttpUtils; } private OkHttpClient client; private void initOkHttpClient(){ if(client == null){ LoggingInterceptor log=new LoggingInterceptor(); client = new OkHttpClient.Builder().addInterceptor(log).build(); } } //公用的get请求方法 完成的功能不确定 public void doGet(String url, Callback callback){ initOkHttpClient(); Request request = new Request.Builder().addHeader("User-Agent","").url(url).build(); Call call = client.newCall(request); call.enqueue(callback); } }
====================================================================================
public abstract class OnUiCallback implements Callback{ private Handler handler = OkHttpUtils.getInstance().getHandler(); public abstract void onFailed(Call call, IOException e); public abstract void onSuccess(String result) throws IOException; @Override public void onFailure(final Call call, final IOException e) { //该方式 存在问题 网络请求也跑到了主线程 待解决 //该方法就是把 线程post到handler所在的线程 handler.post(new Runnable() { @Override public void run() { onFailed(call, e); } }); } @Override public void onResponse(final Call call, final Response response) throws IOException { final String result = response.body().string(); //该方式 存在问题 网络请求也跑到了主线程 待解决 handler.post(new Runnable() { @Override public void run() { try { onSuccess(result); } catch (IOException e) { e.printStackTrace(); } } }); } }
====================================================================================
public interface IRecyView { void showrecy(RecyBean rb); int getID(); }
====================================================================================
public interface FinishListener { void onScuess(RecyBean bean); }
====================================================================================
public interface IRecyModel { void recy(int type, FinishListener finishListener); }
====================================================================================
public class RecyModel implements IRecyModel{ @Override public void recy(int type, final FinishListener listener) { LoggingInterceptor loggingInterceptor = new LoggingInterceptor(); OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(loggingInterceptor).build(); Retrofit retrofit = new Retrofit.Builder().baseUrl(Api.api_man).addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .client(okHttpClient) .build(); ApiService apiService = retrofit.create(ApiService.class); Observable<RecyBean> observable = apiService.path("baidu.ting.billboard.billList",type,10,0); observable.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Observer<RecyBean>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { } @Override public void onNext(RecyBean bean) { listener.onScuess(bean); } }); } }
====================================================================================
public class RecyPresenter implements FinishListener{ IRecyView iRecyView; IRecyModel model; Context context; public RecyPresenter(IRecyView iRecyView, Context context) { this.iRecyView = iRecyView; this.context = context; model=new RecyModel(); } //请求数据的方法 public void Showrecy() { int type=iRecyView.getID(); model.recy(type,this); } @Override public void onScuess(RecyBean bean) { iRecyView.showrecy(bean); } }
====================================================================================
public class MainActivity extends AppCompatActivity implements IRecyView, View.OnClickListener { private XRecyclerView mXrv; private RecyPresenter presenter; private ImageView mBack; private TextView mRegeHeader; private TextView mBj; private RelativeLayout mRl1; private CheckBox mCheckboxAll; private Button mButton; private RelativeLayout mRel; int type = 21; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Fresco.initialize(this); setContentView(R.layout.activity_main); initView(); presenter = new RecyPresenter(this, this); presenter.Showrecy(); } private void initView() { mXrv = (XRecyclerView) findViewById(R.id.xrv); LinearLayoutManager manager = new LinearLayoutManager(this); mXrv.setLayoutManager(manager); mXrv.setLoadingMoreEnabled(true); mBack = (ImageView) findViewById(R.id.back); mRegeHeader = (TextView) findViewById(R.id.header_rege); mBj = (TextView) findViewById(R.id.bj); mBj.setOnClickListener(this); mRl1 = (RelativeLayout) findViewById(R.id.rl1); mCheckboxAll = (CheckBox) findViewById(R.id.checkboxAll); mButton = (Button) findViewById(R.id.button); mButton.setOnClickListener(this); mRel = (RelativeLayout) findViewById(R.id.rel); } @Override public void showrecy(RecyBean rb) { MyAdapter adapter = new MyAdapter(MainActivity.this, rb); TextView tv = new TextView(this); adapter.addHeader(tv); mXrv.setAdapter(adapter); adapter.setOnItemClickListener(new MyAdapter.OnItemClickListener() { @Override public void onItemClick(View view, int position) { Toast.makeText(MainActivity.this, "1112", Toast.LENGTH_SHORT).show(); } }); mXrv.setLoadingListener(new XRecyclerView.LoadingListener() { @Override public void onRefresh() { type++; presenter.Showrecy(); mXrv.refreshComplete(); } @Override public void onLoadMore() { } }); } @Override public int getID() { return type; } @Override public void onClick(View v) { switch (v.getId()) { case R.id.bj: // TODO 17/11/08 String text = mBj.getText().toString(); if (text.equals("编辑")) { mBj.setText("完成"); mRel.setVisibility(View.VISIBLE); // bool = true; // adapter = new MyAdapter(this, list2, bool); // mXrv.setAdapter(adapter); } else { mBj.setText("编辑"); mRel.setVisibility(View.INVISIBLE); // bool = false; // adapter = new MyAdapter(this, list2, bool); // mXrv.setAdapter(adapter); } break; case R.id.button: // TODO 17/11/08 break; default: break; } } }
=====================================activity_main.xml================================
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <RelativeLayout android:id="@+id/rl1" android:layout_width="match_parent" android:layout_height="50dp" android:background="@color/blue"> <ImageView android:id="@+id/back" android:layout_width="20dp" android:layout_height="20dp" android:src="@drawable/back" android:layout_marginLeft="10dp" android:layout_marginTop="10dp"/> <TextView android:id="@+id/header_rege" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/back" android:text="百度热歌榜" android:textColor="#ffffffff" android:textSize="20sp" android:layout_marginLeft="90dp" android:layout_marginTop="10dp"/> <TextView android:id="@+id/bj" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="编辑" android:textColor="#ffffffff" android:textSize="20sp" android:layout_alignParentRight="true" android:layout_marginTop="10dp" /> </RelativeLayout> <com.jcodecraeer.xrecyclerview.XRecyclerView android:id="@+id/xrv" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/rl1" > </com.jcodecraeer.xrecyclerview.XRecyclerView> <RelativeLayout android:id="@+id/rel" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:background="#ffffff" android:visibility="visible" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true"> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/checkboxAll" android:text="全选" android:layout_alignBaseline="@+id/button" android:layout_alignBottom="@+id/button" /> <Button android:layout_alignParentRight="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="删除" android:id="@+id/button" /> </RelativeLayout> </RelativeLayout>
================================recy_header1.xml===================================
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/rl"> <RelativeLayout android:layout_width="match_parent" android:layout_height="130dp" > <com.facebook.drawee.view.SimpleDraweeView android:id="@+id/header_iv" android:layout_width="200px" android:layout_height="200px" android:src="@mipmap/ic_launcher" /> <TextView android:id="@+id/header_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_toRightOf="@+id/header_iv" android:text="标题" android:textSize="20sp" android:layout_marginLeft="10dp" android:layout_marginTop="20dp"/> <TextView android:id="@+id/header_author" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/header_iv" android:text="最近更新:" android:layout_marginLeft="10dp" android:layout_below="@+id/header_title"/> <TextView android:id="@+id/header_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/header_title" android:layout_toRightOf="@+id/header_author" android:text="名字" android:layout_marginLeft="10dp" /> <TextView android:id="@+id/header_info" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="内容" android:layout_below="@+id/header_name" android:layout_toRightOf="@+id/header_iv" android:layout_marginLeft="10dp"/> </RelativeLayout> </RelativeLayout>
==================================recy_item1.xml======================================
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/rl"> <RelativeLayout android:layout_width="match_parent" android:layout_height="130dp" > <com.facebook.drawee.view.SimpleDraweeView android:id="@+id/header_iv" android:layout_width="200px" android:layout_height="200px" android:src="@mipmap/ic_launcher" /> <TextView android:id="@+id/header_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_toRightOf="@+id/header_iv" android:text="标题" android:textSize="20sp" android:layout_marginLeft="10dp" android:layout_marginTop="20dp"/> <TextView android:id="@+id/header_author" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/header_iv" android:text="最近更新:" android:layout_marginLeft="10dp" android:layout_below="@+id/header_title"/> <TextView android:id="@+id/header_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/header_title" android:layout_toRightOf="@+id/header_author" android:text="名字" android:layout_marginLeft="10dp" /> <TextView android:id="@+id/header_info" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="内容" android:layout_below="@+id/header_name" android:layout_toRightOf="@+id/header_iv" android:layout_marginLeft="10dp"/> </RelativeLayout> </RelativeLayout>
==================================recy_item11.xml======================================
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <CheckBox android:id="@+id/check" android:layout_width="wrap_content" android:layout_height="wrap_content" android:visibility="visible" android:layout_alignTop="@+id/title1" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> <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="40dp" android:textSize="20sp"/> <TextView android:id="@+id/author1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toLeftOf="@+id/iv1" android:text="作者" android:layout_marginLeft="10dp" android:layout_below="@+id/title1" android:layout_marginTop="10dp"/> <TextView android:id="@+id/name1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/title1" android:layout_toLeftOf="@+id/author1" android:text="名字" android:layout_marginLeft="10dp" android:layout_marginTop="10dp" /> <ImageView android:id="@+id/iv1" android:layout_width="180px" android:layout_height="180px" android:src="@mipmap/ic_launcher" android:layout_alignParentRight="true"/> </RelativeLayout>