mvp+SQL+RecyclerView

效果图


长按条目删除有动画,点击跳转


代码:


adapter包下NewsAdapter类

package com.example.yk_lx2.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.yk_lx2.R;
import com.example.yk_lx2.bean.News;
import com.example.yk_lx2.common.Constants;
import com.google.gson.Gson;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.List;

/**
 * Author:kson
 * E-mail:[email protected]
 * Time:2018/05/29
 * Description:
 */
public class NewsAdapter extends XRecyclerView.Adapter<XRecyclerView.ViewHolder> {

    private List<News.Data> list;
    private Context context;
    private  News news;

    public NewsAdapter(List<News.Data> list, Context context,News news) {
        this.list = list;
        this.context = context;
        this.news = news;
    }

    public void loadMore(List<News.Data> data) {
        if (list != null) {
            list.addAll(data);
            notifyDataSetChanged();
        }
    }


    @NonNull
    @Override
    public XRecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == Constants.TYPE1) {
            View view = LayoutInflater.from(context).inflate(R.layout.news_item2_layout, parent, false);

            return new Type1ViewHolder(view);
        } else {
            View view2 = LayoutInflater.from(context).inflate(R.layout.news_item_layout, parent, false);

            return new Type2ViewHolder(view2);
        }


    }

    @Override
    public void onBindViewHolder(@NonNull final XRecyclerView.ViewHolder holder, int position) {

        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {

                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setTitle("删除");
                builder.setNegativeButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        int pos = holder.getLayoutPosition();//得到下标
                        System.out.println("pos----"+pos);
                        list.remove(pos);//删除集合的数据
                        news.data = list;

                       String json =  new Gson().toJson(news);

                        notifyItemRemoved(pos);//局部删除当前view并局部刷新
                    }
                });
                builder.setNeutralButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                });
                builder.show();


                return true;
            }
        });

        News.Data data = list.get(position);
        if (holder instanceof Type1ViewHolder) {//1张图片


            ((Type1ViewHolder) holder).title.setText(data.topic);



        } else if (holder instanceof Type2ViewHolder) {//三张图片
            ((Type2ViewHolder) holder).title.setText(data.topic);
            if (data.miniimg != null && data.miniimg.size() > 0) {
                if (data.miniimg.size() == 1) {
                    Glide.with(context).load(data.miniimg.get(0).src).into(((Type2ViewHolder) holder).iv1);
                    Glide.with(context).load(data.miniimg.get(0).src).into(((Type2ViewHolder) holder).iv2);
                    Glide.with(context).load(data.miniimg.get(0).src).into(((Type2ViewHolder) holder).iv3);
                } else if (data.miniimg.size() == 2) {
                    Glide.with(context).load(data.miniimg.get(0).src).into(((Type2ViewHolder) holder).iv1);
                    Glide.with(context).load(data.miniimg.get(1).src).into(((Type2ViewHolder) holder).iv2);
                    Glide.with(context).load(data.miniimg.get(1).src).into(((Type2ViewHolder) holder).iv3);
                } else {
                    Glide.with(context).load(data.miniimg.get(0).src).into(((Type2ViewHolder) holder).iv1);
                    Glide.with(context).load(data.miniimg.get(1).src).into(((Type2ViewHolder) holder).iv2);
                    Glide.with(context).load(data.miniimg.get(2).src).into(((Type2ViewHolder) holder).iv3);
                }
            }
        }
    }

    @Override
    public int getItemViewType(int position) {
        return position % 2 == 0 ? Constants.TYPE1 : Constants.TYPE2;
    }


    @Override
    public int getItemCount() {
        return list.size();
    }

    class Type1ViewHolder extends XRecyclerView.ViewHolder {

        private TextView title;

        public Type1ViewHolder(View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.title1);
        }
    }

    class Type2ViewHolder extends XRecyclerView.ViewHolder {

        private TextView title;
        private ImageView iv1, iv2, iv3;

        public Type2ViewHolder(View itemView) {
            super(itemView);
            iv1 = itemView.findViewById(R.id.img1);
            iv2 = itemView.findViewById(R.id.img2);
            iv3 = itemView.findViewById(R.id.img3);
            title = itemView.findViewById(R.id.title3);
        }
    }
}
 
 

bean包下LocalNews

package com.example.yk_lx2.bean;

/**
 * Author:kson
 * E-mail:[email protected]
 * Time:2018/05/30
 * Description:
 */
public class LocalNews {
    public String title;
    public String imgurls;
    public String source;
    public String time;
    public boolean isDel;
}
 
 

News类

package com.example.yk_lx2.bean;

import java.util.List;

/**
 * Author:kson
 * E-mail:[email protected]
 * Time:2018/05/29
 * Description:
 */
public class News {
    public String stat;
    public List<Data> data;

    public class Data {
        public String topic;
        public String source;
        public List<IMG> miniimg;

        public class IMG {
            public String src;
        }

    }
}
 
 

common包下Constants

package com.example.yk_lx2.common;

/**
 * Author:kson
 * E-mail:[email protected]
 * Time:2018/05/29
 * Description:
 */
public class Constants {
    public static final String GET_URL = "http://ttpc.dftoutiao.com/jsonpc/refresh";

    public static final  int TYPE1 = 3;//条目是三张图
    public static final  int TYPE2 = 1;//条目是1张图

}
 
 

db包下DbHelper类

package com.example.yk_lx2.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * Author:kson
 * E-mail:[email protected]
 * Time:2018/05/30
 * Description:
 */
public class DbHelper extends SQLiteOpenHelper {
    //数据库文件名称
    private static final String DB_NAME = "news.db";
    public static final String NEWS_TABLE_NAME = "news";
    private static final int VERSION = 1;

    public DbHelper(Context context) {
        super(context, DB_NAME, null, VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {

        String sql = "create table " + NEWS_TABLE_NAME + " (_id Integer PRIMARY KEY ,json text)";

        sqLiteDatabase.execSQL(sql);

    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {



    }
}
 
 

model包下NewsModel类

package com.example.yk_lx2.model;

import com.example.yk_lx2.utils.OkhttpUtils;

import java.util.Map;

/**
 * Author:kson
 * E-mail:[email protected]
 * Time:2018/05/29
 * Description:
 */
public class NewsModel {

    /**
     * 请求数据
     * @param getUrl
     */
    public void getData(String getUrl, Map<String,String> params,final ResponseCallback responseCallback) {

        OkhttpUtils.getInstance().postData(getUrl,params, new OkhttpUtils.ICallback() {
            @Override
            public void success(String result) {
                responseCallback.success(result);

            }

            @Override
            public void fail(String msg) {

                responseCallback.fail(msg);
            }
        });
    }

    public interface  ResponseCallback{
        void success(String result);
        void fail(String msg);
    }
}
 
 

persenter包下NewsPresenter类

package com.example.yk_lx2.presenter;

import android.text.TextUtils;

import com.example.yk_lx2.bean.News;
import com.example.yk_lx2.model.NewsModel;
import com.example.yk_lx2.view.INews;
import com.google.gson.Gson;

import java.util.Map;

/**
 * Author:kson
 * E-mail:[email protected]
 * Time:2018/05/29
 * Description:p
 */
public class NewsPresenter {

    private INews iNews;
    private NewsModel model;

    public NewsPresenter(INews iNews) {

        model = new NewsModel();
        attach(iNews);
    }

    /**
     * 绑定view
     * @param iNews
     */
    public void attach(INews iNews){
        this.iNews = iNews;
    }
    /**
     * 获取数据的方法
     *
     * @param getUrl
     */
    public void getData(String getUrl, Map<String ,String> params) {

        model.getData(getUrl, params,new NewsModel.ResponseCallback() {
            @Override
            public void success(String result) {
                if (!TextUtils.isEmpty(result)) {
                    String s = result.replace("null(","")
                            .replace(")","");

                    News news = new Gson().fromJson(s, News.class);
                    iNews.success(news);
                }
            }

            @Override
            public void fail(String msg) {


            }
        });

//        OkhttpUtils.getInstance().getData(getUrl, new OkhttpUtils.ICallback() {
//            @Override
//            public void success(String result) {
//
//            }
//
//            @Override
//            public void fail(String msg) {
//
//            }
//        });
    }

    /**
     * 解绑
     */
    public void detach(){
        this.iNews = null;
    }
}
 
 

utils包下AppUtil类

package com.example.yk_lx2.utils;

import android.content.Context;
import android.util.DisplayMetrics;

/**
 * Author:kson
 * E-mail:[email protected]
 * Time:2018/05/30
 * Description:
 */
public class AppUtil {
    /**
     *
     * @param context
     * @return 屏幕宽度
     */
    public static int screenWidth(Context context){
        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        return  metrics.widthPixels;
    }
}
 
 

NetWorkUtil类

package com.example.yk_lx2.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

/**
 * Author:kson
 * E-mail:[email protected]
 * Time:2018/05/30
 * Description:判断网络
 */
public class NetWorkUtil {

    /**
     *
     * @return 是否有活动的网络连接
     */
    public final static boolean hasNetWorkConnection(Context context){
        //获取连接活动管理器
        final ConnectivityManager connectivityManager= (ConnectivityManager) context.
                getSystemService(Context.CONNECTIVITY_SERVICE);
        //获取链接网络信息
        final NetworkInfo networkInfo=connectivityManager.getActiveNetworkInfo();

        return (networkInfo!= null && networkInfo.isAvailable());

    }
    /**
     * @return 返回boolean ,是否为wifi网络
     *
     */
    public final static boolean hasWifiConnection(Context context)
    {
        final ConnectivityManager connectivityManager= (ConnectivityManager) context.
                getSystemService(Context.CONNECTIVITY_SERVICE);
        final NetworkInfo networkInfo=connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        //是否有网络并且已经连接
        return (networkInfo!=null&& networkInfo.isConnectedOrConnecting());


    }

    /**
     * @return 返回boolean,判断网络是否可用,是否为移动网络
     *
     */

    public final static boolean hasGPRSConnection(Context context){
        //获取活动连接管理器
        final ConnectivityManager connectivityManager= (ConnectivityManager) context.
                getSystemService(Context.CONNECTIVITY_SERVICE);
        final NetworkInfo networkInfo=connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        return (networkInfo!=null && networkInfo.isAvailable());

    }
    /**
     * @return  判断网络是否可用,并返回网络类型,ConnectivityManager.TYPE_WIFI,ConnectivityManager.TYPE_MOBILE,不可用返回-1
     */
    public static final int getNetWorkConnectionType(Context context){
        final ConnectivityManager connectivityManager=(ConnectivityManager) context.
                getSystemService(Context.CONNECTIVITY_SERVICE);
        final NetworkInfo wifiNetworkInfo=connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        final NetworkInfo mobileNetworkInfo=connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);


        if(wifiNetworkInfo!=null &&wifiNetworkInfo.isAvailable())
        {
            return ConnectivityManager.TYPE_WIFI;
        }
        else if(mobileNetworkInfo!=null &&mobileNetworkInfo.isAvailable())
        {
            return ConnectivityManager.TYPE_MOBILE;
        }
        else {
            return -1;
        }


    }
}
 
 

封装的OkhttpUtils类

package com.example.yk_lx2.utils;

import android.os.Handler;

import java.io.IOException;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;

/**
 * Author:kson
 * E-mail:[email protected]
 * Time:2018/05/29
 * Description:
 */
public class OkhttpUtils {
    private static OkhttpUtils okhttpUtils;
    private OkHttpClient okHttpClient;
    private Handler handler;

    private OkhttpUtils() {
        okHttpClient = new OkHttpClient.Builder()
                .addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
                .build();
        handler = new Handler();

    }

    public static OkhttpUtils getInstance() {

        if (okhttpUtils == null) {
            okhttpUtils = new OkhttpUtils();
        }

        return okhttpUtils;

    }

    /**
     * get方式
     */
    public void getData(String url, final ICallback callback){

        final Request request = new Request.Builder()
                .url(url).build();

        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

                if (callback!=null){
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            callback.fail("请求失败");
                        }
                    });

                }

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

                if (callback!=null){
                    if (response.isSuccessful()&&response.code()==200){

                        final String result = response.body().string();


                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                callback.success(result);
                            }
                        });
                    }
                }
            }
        });



    }


    /**
     * post方式
     */
    public void postData(String url, Map<String,String> params, final ICallback callback){

        FormBody.Builder builder = new FormBody.Builder();

        for (Map.Entry<String, String> bean : params.entrySet()) {
            builder.add(bean.getKey(),bean.getValue());

        }

        final Request request = new Request.Builder()
                .url(url).post(builder.build()).build();

        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

                if (callback!=null){
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            callback.fail("请求失败");
                        }
                    });

                }

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

                if (callback!=null){
                    if (response.isSuccessful()&&response.code()==200){

                        final String result = response.body().string();


                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                callback.success(result);
                            }
                        });
                    }
                }
            }
        });
    }


    public interface ICallback{
        void success(String result);
        void fail(String msg);
    }



}
 
 

view包下INews接口

package com.example.yk_lx2.view;


import com.example.yk_lx2.bean.News;

/**
 * Author:kson
 * E-mail:[email protected]
 * Time:2018/05/29
 * Description:
 */
public interface INews {
    void success(News news);
}
 
 

widget包下ThreeColorView类

package com.example.yk_lx2.widget;

import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.yk_lx2.MainActivity;
import com.example.yk_lx2.utils.AppUtil;

/**
 * Author:kson
 * E-mail:[email protected]
 * Time:2018/05/30
 * Description:
 */
public class ThreeColorView extends ViewGroup {

    public ThreeColorView(Context context) {
        super(context);
    }

    public ThreeColorView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ThreeColorView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    /**
     * 把此view的最终的宽度和高度定下来
     *
     * @param widthMeasureSpec
     * @param heightMeasureSpec
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int totalHeight = 0;//此控件的高度
        int totalWidth = 0;//此控件的宽度
        //得到子view数量
        int child = getChildCount();
        if (child > 0) {
            for (int i = 0; i < child; i++) {//遍历子控件
                View view = getChildAt(i);//得到此容器所有的子view
                totalHeight += view.getMeasuredHeight();
                measureChild(view,widthMeasureSpec,heightMeasureSpec);
//                view.measure(widthMeasureSpec, heightMeasureSpec);
            }
        }
        totalWidth = AppUtil.screenWidth(getContext());
        System.out.println("width:"+totalWidth);
        System.out.println("height:"+totalHeight);


        //设置宽度和高度给当前view,通过下面这个方法
        setMeasuredDimension(totalWidth, totalHeight);

    }

    @Override
    protected void onLayout(boolean bo, int left, int top, int right, int bottom) {

        int l = 0;
        int t = 0;
        int r = 0;
        int b = 0;

        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {

            View view = getChildAt(i);//得到每一个view的对象

            view.layout(l, t, l + view.getMeasuredWidth(), t + view.getMeasuredHeight());

            l += view.getMeasuredWidth();

            System.out.println("llll:"+l);

            t += view.getMeasuredHeight();

            if (l+view.getMeasuredWidth()>AppUtil.screenWidth(getContext())){
                l = 0;
            }


//            if (AppUtil.screenWidth(getContext()) - l < view.getMeasuredWidth()) {
//                l = 0;
//            }

            //点击事件
            final int finalI = i;
            view.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {

                    Toast.makeText(getContext(), finalI +":点击位置", Toast.LENGTH_SHORT).show();

                    TextView textView = (TextView) view;

                    Toast.makeText(getContext(), textView.getText().toString() +"文本", Toast.LENGTH_SHORT).show();

                    Intent intent = new Intent(getContext(), MainActivity.class);
                    intent.putExtra("id",textView.getText().toString());
                    getContext().startActivity(intent);

                }
            });

            view.setOnLongClickListener(new OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    Toast.makeText(getContext(), finalI +":长按位置", Toast.LENGTH_SHORT).show();

                    removeView(view);
                    return true;
                }
            });



        }


    }
}
 
 

MainActivity类

package com.example.yk_lx2;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.widget.Toast;

import com.example.yk_lx2.adapter.NewsAdapter;
import com.example.yk_lx2.bean.News;
import com.example.yk_lx2.common.Constants;
import com.example.yk_lx2.db.DbHelper;
import com.example.yk_lx2.presenter.NewsPresenter;
import com.example.yk_lx2.utils.NetWorkUtil;
import com.example.yk_lx2.view.INews;
import com.google.gson.Gson;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity implements INews {

    private XRecyclerView rv;
    private NewsPresenter presenter;
    private int page = 5010;
    private List<News.Data> data;
    private boolean isRefresh = true;//判断是下啦刷新还是上啦加载
    private NewsAdapter newsAdapter;
    private DbHelper dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
    }

    private void initData() {
        if (getIntent().getExtras() != null) {
            page = 5010 + Integer.parseInt(getIntent().getExtras().getString("id"));
        }

        dbHelper = new DbHelper(this);
        presenter = new NewsPresenter(this);
        data = new ArrayList<>();

        request();

    }

    public void request() {
        if (NetWorkUtil.hasWifiConnection(this) || NetWorkUtil.hasGPRSConnection(this)) {

            Map<String, String> p = new HashMap<>();
            p.put("type", page + "");
            presenter.getData(Constants.GET_URL, p);
        } else {
            Toast.makeText(this, "网络不通畅,请稍后再试!", Toast.LENGTH_SHORT).show();
            String json = null;
//
            //获取数据库对象,可读
            SQLiteDatabase db = dbHelper.getReadableDatabase();
            //获取数据库的游标
            Cursor cursor = db.rawQuery("select * from news", null);
            while (cursor.moveToNext()){
                json = cursor.getString(cursor.getColumnIndex("json"));
            }

            //本地列表刷新
            fillLocalData(json);

        }

    }

    /**
     * 本地列表刷新
     * @param json
     */
    private void fillLocalData(String json) {

        News news = new Gson().fromJson(json,News.class);
        newsAdapter = new NewsAdapter(news.data, this,news);
        rv.setAdapter(newsAdapter);

    }

    private void initView() {
        rv = findViewById(R.id.rv);

        //设置局部刷新动画
        rv.setItemAnimator(new DefaultItemAnimator());

        rv.setPullRefreshEnabled(true);//刷新配置
        rv.setLoadingMoreEnabled(true);//上拉配置
        rv.setLayoutManager(new LinearLayoutManager(this));
        rv.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {

//                rv.refreshComplete();
                isRefresh = true;
                //下拉刷新
                page = 5010;
                request();


            }

            @Override
            public void onLoadMore() {

                isRefresh = false;
                page++;
                request();

//                rv.loadMoreComplete();

            }
        });

    }

    @Override
    public void success(News news) {

        //转换json串
        String json = new Gson().toJson(news);



        System.out.println("size:" + news.data.size());




        data = news.data;

        if (isRefresh) {
            newsAdapter = new NewsAdapter(data, this,news);
            rv.setAdapter(newsAdapter);
            rv.refreshComplete();

            //保存json串数据
            SQLiteDatabase db = dbHelper.getWritableDatabase();
            ContentValues contentValues = new ContentValues();
            contentValues.put("json", json);
            db.insert(DbHelper.NEWS_TABLE_NAME, null, contentValues);



        } else {
            if (newsAdapter != null) {
                //上啦加载更多,刷新
                newsAdapter.loadMore(news.data);
            }
            rv.loadMoreComplete();
        }


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.detach();
    }
}
 
 

ThreeColorActivity类

package com.example.yk_lx2;

import android.animation.ObjectAnimator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.yk_lx2.utils.AppUtil;
import com.example.yk_lx2.widget.ThreeColorView;


public class ThreeColorActivity extends AppCompatActivity {

    private ThreeColorView threeColorView;
    private int count = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_threecolor);
        threeColorView = findViewById(R.id.threecolorview);
    }

    /**
     * 添加view
     * @param view
     */
    public void add(View view) {
        count++;

        int width = AppUtil.screenWidth(this);
        TextView textView = new TextView(this);
        textView.setText(count+"");
        textView.setGravity(Gravity.CENTER);
        textView.setTextColor(getResources().getColor(R.color.white));

        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(textView,"translationX",(width-width/3),0);

        objectAnimator.setDuration(3000);
        objectAnimator.start();
        if (count==1||count==4||count==7){

            textView.setBackgroundColor(getResources().getColor(R.color.colorAccent));
        }else{
            textView.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
        }



        threeColorView.addView(textView);

        //得到view的属性参数
        ViewGroup.LayoutParams params = textView.getLayoutParams();
        params.width = width/3;
        params.height = 70;
        textView.setLayoutParams(params);

    }
}
 
 

主页面布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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/rv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>

第二个页面的布局


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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"
    android:orientation="vertical"
    tools:context=".ThreeColorActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <TextView
            android:layout_width="0dp"
            android:layout_weight="3"
            android:layout_height="wrap_content"
            android:text="三色梯"
            android:gravity="center"
            />
        <Button
            android:id="@+id/add"
            android:onClick="add"
            android:text="+"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </LinearLayout>
    <com.example.yk_lx2.widget.ThreeColorView
        android:id="@+id/threecolorview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </com.example.yk_lx2.widget.ThreeColorView>

</LinearLayout>

两个条目布局news_item2

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="10dp">

    <ImageView
        android:id="@+id/img1"
        android:src="@mipmap/ic_launcher"
        android:layout_width="wrap_content"
        android:scaleType="centerCrop"
        android:layout_height="wrap_content"/>


    <LinearLayout
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginTop="10dp">

        <TextView
            android:id="@+id/title1"
            android:layout_marginLeft="10dp"
            android:text="我是标题"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>


    </LinearLayout>



</LinearLayout>

news_item


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="10dp">

    <TextView
        android:id="@+id/title3"
        android:text="我是标题"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="10dp">

        <ImageView
            android:id="@+id/img1"
            android:src="@mipmap/ic_launcher"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:scaleType="centerCrop"
            android:layout_height="70dp"/>
        <ImageView
            android:id="@+id/img2"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:src="@mipmap/ic_launcher"
            android:layout_width="0dp"
            android:scaleType="centerCrop"
            android:layout_weight="1"
            android:layout_height="70dp"/>
        <ImageView
            android:id="@+id/img3"
            android:src="@mipmap/ic_launcher"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:scaleType="centerCrop"
            android:layout_height="70dp"/>

    </LinearLayout>



</LinearLayout>

清单文件


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.yk_lx2">
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".ThreeColorActivity"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <activity android:name=".MainActivity">
        </activity>
    </application>

</manifest>

依赖

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26



    defaultConfig {
        applicationId "com.example.yk_lx2"
        minSdkVersion 15
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation 'com.github.bumptech.glide:glide:4.7.1'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.7.1'
    implementation 'com.jcodecraeer:xrecyclerview:1.5.9'
    implementation 'com.squareup.okhttp3:logging-interceptor:3.10.0'
    implementation 'com.squareup.okhttp3:okhttp:3.10.0'
    implementation 'com.google.code.gson:gson:2.8.5'
}
configurations.all {
    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
        def requested = details.requested
        if (requested.group == 'com.android.support') {
            if (!requested.name.startsWith("multidex")) {
                details.useVersion '27.1.1'
            }
        }
    }
}






猜你喜欢

转载自blog.csdn.net/yin_chenglong/article/details/80528689
MVP