OkHttp网络加载数据+RecyclerView展示+属性动画

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'
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值