OkHttp網路載入資料+RecyclerView展示+屬性動畫
阿新 • • 發佈:2018-11-19
MainActivity.java
public class MainActivity extends AppCompatActivity implements HttpUrls.ff{ private ImageView mImageView; private RecyclerView mRecyclerView; RvAdapter rvAdapter; LinearLayoutManager linearLayoutManager; ObjectAnimator oa; public boolean a=true; public String urlString="http://www.xieast.com/api/news/news.php";//資料路徑 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViews(); HttpUrls httpUrl = new HttpUrls(this); httpUrl.okHttp(urlString); //呼叫工具類 rvAdapter = new RvAdapter(this); mRecyclerView.setAdapter(rvAdapter);//設定adapter linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false); mRecyclerView.setLayoutManager(linearLayoutManager); rvAdapter.setOnItemClickListener(new RvAdapter.OnItemClickListener() { @Override public void onItemClick(int position) { //點選X刪除條目 rvAdapter.remove(position); } }); } private void findViews() { mImageView = findViewById(R.id.image); mRecyclerView = findViewById(R.id.rv); //圖片點選動畫 mImageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { AnimatorSet set = new AnimatorSet(); oa = ObjectAnimator.ofFloat(mImageView, "translationX", new float[]{0f,-500f,0f}); oa.setDuration(1000); ObjectAnimator oa2 = ObjectAnimator.ofFloat(mImageView, "translationY", new float[]{0f,1000f,0f}); oa2.setDuration(1000); set.playTogether(oa, oa2); set.start(); oa.addListener(new AnimatorListenerAdapter() { //動畫結束切換圖片 @Override public void onAnimationEnd(Animator animation) { super.onAnimationEnd(animation); if (a){ mImageView.setImageResource(R.drawable.b); a=false; }else { mImageView.setImageResource(R.drawable.a); a=true; } } }); } }); } @Override public void success(final String data) { //介面回撥來的資料 ResponseBean responseBean = new Gson().fromJson(data,ResponseBean.class); final List<ResponseBean.DataBean> data1 = responseBean.getData(); runOnUiThread(new Runnable() { @Override public void run() { rvAdapter.setDatas(data1); } }); } @Override public void bai() { } }
activity_main.xml
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="wrap_content" android:layout_height="50dp" android:orientation="horizontal"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginTop="10dp" android:src="@drawable/c"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:textColor="#000" android:layout_marginLeft="120dp" android:text="今日頭條"/> </LinearLayout> <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="350dp" android:layout_marginTop="-40dp" android:src="@drawable/a"/> <android.support.v7.widget.RecyclerView android:id="@+id/rv" android:layout_width="match_parent" android:layout_height="match_parent"/> </LinearLayout> </android.support.constraint.ConstraintLayout>
RvAdapter.java
public class RvAdapter extends RecyclerView.Adapter<RvAdapter.ViewHolder>{ private Context mContext; List<ResponseBean.DataBean> list; public RvAdapter(Context context) { mContext = context; list = new ArrayList<>(); } public void setDatas(List<ResponseBean.DataBean> datas) { list.addAll(datas); notifyDataSetChanged(); } public void remove(int position) { list.remove(position); notifyItemRemoved(position); notifyDataSetChanged(); } public interface OnItemClickListener{ //自定義介面回撥設定點選事件 void onItemClick(int position); } private OnItemClickListener mOnItemClickListener; public void setOnItemClickListener(OnItemClickListener onItemClickListener){ mOnItemClickListener=onItemClickListener; } @NonNull @Override public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = View.inflate(mContext, R.layout.item, null); ViewHolder holder = new ViewHolder(view); return holder; } @Override public void onBindViewHolder(@NonNull final ViewHolder holder, int position) { ImageLoader.getInstance().displayImage(list.get(position).getThumbnail_pic_s(),holder.mImageView); holder.mt1.setText(list.get(position).getTitle()); holder.mt2.setText(list.get(position).getCategory()); holder.mt3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { int po = holder.getLayoutPosition(); /*list.remove(po); notifyItemRemoved(po); notifyDataSetChanged();*/ mOnItemClickListener.onItemClick(po); } }); } @Override public int getItemCount() { return list.size(); } public class ViewHolder extends RecyclerView.ViewHolder { ImageView mImageView; TextView mt1; TextView mt2; TextView mt3; public ViewHolder(View itemView) { super(itemView); mImageView = itemView.findViewById(R.id.image); mt1 = itemView.findViewById(R.id.t1); mt2 = itemView.findViewById(R.id.t2); mt3 = itemView.findViewById(R.id.t3); } } }
HttpUrls.java
public class HttpUrls {
public HttpUrls(ff ff) {
mFf = ff;
}
public interface ff{
void success(String data);
void bai();
}
private ff mFf;
public void okHttp(String urlString) {
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.readTimeout(10,TimeUnit.SECONDS)
.build();
Request request = new Request.Builder()
.url(urlString)
.build();
Call call = okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
//Toast.makeText(mContext,"hhe", Toast.LENGTH_SHORT).show();
Log.e("fff","ff");
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String mS = response.body().string();
mFf.success(mS);
}
});
}
}
item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/image"
android:layout_width="60dp"
android:layout_height="80dp"
android:src="@mipmap/ic_launcher"/>
<TextView
android:id="@+id/t1"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:text="fff"/>
<TextView
android:id="@+id/t2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="-10dp"
android:layout_marginTop="30dp"
android:text="fff"/>
<TextView
android:id="@+id/t3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:layout_marginTop="30dp"
android:textColor="#f00"
android:text="X"/>
</LinearLayout>
objectAnimator.xml
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:propertyName="rotationX"
android:duration="3000"
android:repeatCount="1"
android:repeatMode="reverse"
android:startOffset="0"
android:valueFrom="360.0">
</objectAnimator>
App.java
public class App extends Application{
@Override
public void onCreate() {
super.onCreate();
//BitmapDisplayer bitmapDisplayer = new CircleBitmapDisplayer();
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.ic_launcher_background)
.showImageForEmptyUri(R.drawable.ic_launcher_foreground)
.cacheInMemory(true)
.build();
ImageLoaderConfiguration configuration = new ImageLoaderConfiguration
.Builder(this)
.defaultDisplayImageOptions(options)
.build();
ImageLoader.getInstance().init(configuration);
}
}
依賴
implementation 'com.android.support:recyclerview-v7:27.1.1'
implementation 'com.squareup.okhttp3:okhttp:3.11.0'
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'