自定义增加梯形布局及数据跳转

本文介绍了如何在Android应用中实现自定义梯形布局,并在点击后无上限地动态增加视图。同时,文章详细讲解了如何通过MVP架构进行数据多条目的展示,涉及布局设计、接口定义、数据库操作以及关键的适配器NewsAdapter的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先上依赖

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'

与当前版本不兼容可将下方代码CV到gradle的最下方(注意是最下方,最下方,最下方重要的事情说三遍)

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'
            }
        }
    }
}

依赖导了,权限也不能忘了

<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"/>
好了进入正题

既然是做自定义view自然要先敲了,点击某控件增加一个view呈梯形增加,增加满屏幕后,回到左侧继续增加(无上限)

public class MyViewGroup extends ViewGroup {

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

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

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

    /**
     * 把此view的最终的宽度和高度定下来
     */
    @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);
            }
        }
        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 ((i+1)%3==0) {
                view.setBackgroundColor(Color.GREEN);
            }else  if ((i+1)%3==1){
                view.setBackgroundColor(Color.BLUE);
            }else if ((i+1)%3==2){
                view.setBackgroundColor(Color.RED);
            }
            //点击事件
            final int finalI = i;
            view.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {

                    TextView textView = (TextView) view;
                    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) {
                    removeView(view);
                    return true;
                }
            });



        }


    }
}

自定义view页的布局  activity_threecolor


<?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">

    <LinearLayout
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="56dp">

        <TextView
            android:text="三色梯"
            android:layout_weight="8"
            android:layout_width="0dp"
            android:gravity="center"
            android:layout_height="match_parent" />

        <TextView
            android:onClick="add"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:text="加"
            android:gravity="center"
            android:layout_height="match_parent" />

    </LinearLayout>
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <com.example.kson.monthdemo.widget.MyViewGroup
            android:id="@+id/threecolorview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        </com.example.kson.monthdemo.widget.MyViewGroup>
    </ScrollView>

</LinearLayout>

对应的ThreeColorActivity


public class ThreeColorActivity extends AppCompatActivity {

    private MyViewGroup myViewGroup;
    private int count = 0;

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

    /**
     * 添加view
     * @param view
     */
    public void add(View view) {
        TextView textView = new TextView(this);
        count++;
        int width = AppUtil.screenWidth(this);
        int height = AppUtil.screenHeight(this);
        textView = new TextView(this);
        textView.setText(count+"");

        textView.setTextSize(25);
        textView.setGravity(Gravity.CENTER);

        textView.setTextColor(getResources().getColor(R.color.white));
        textView.setBackgroundColor(getResources().getColor(R.color.colorAccent));
        ObjectAnimator translationX = ObjectAnimator.ofFloat(textView, "translationX", (width - width / 3), 0);
        ObjectAnimator alpha = ObjectAnimator.ofFloat(textView, "alpha", 0f,1f);
        alpha.setDuration(5000);
        alpha.start();
        translationX.setDuration(3000);
        translationX.start();


        myViewGroup.addView(textView);

        ViewGroup.LayoutParams layoutParams = textView.getLayoutParams();
        layoutParams.height = height/15;
        layoutParams.width = width/3;
        textView.setLayoutParams(layoutParams);

    }
}


接下来就是MVP进行数据多条目展示了

先来个布局   activity_main

<?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"
    tools:context=".MainActivity">

    <com.jcodecraeer.xrecyclerview.XRecyclerView
        android:id="@+id/rv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>


对应的MainActivity

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();
    }
}

View层的接口 INews

public interface INews {
    void success(News news);
}

util包下的三个类

AppUtil

public class AppUtil {
    /**
     *
     * @param context
     * @return 屏幕宽度
     * @return 屏幕高度
     */
    public static int screenWidth(Context context){
        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        return  metrics.widthPixels;
    }
    public static int screenHeight(Context context) {
        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        return metrics.heightPixels;
    }
}

NetWorkUtil

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

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);
    }



}

P层的  NewsPresenter

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)) {
                    Log.e("TAG", "success: result = "+result );
                    String s = result.replace("null(","")
                            .replace(")","");
                    Log.e("TAG", "success: s = "+s );
                    News news = new Gson().fromJson(s, News.class);
                    iNews.success(news);
                }
            }

            @Override
            public void fail(String msg) {


            }
        });
    }

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

Model层的  NewsModel


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);
    }
}

db包下的  数据库  DbHelper

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) {



    }
}

common包下的 Constants 

public class Constants {
    public static final String GET_URL = "http://ttpc.dftoutiao.com/jsonpc/refresh";

    public static final  int TYPE1 = 1;
    public static final  int TYPE2 = 2;
    public static final  int TYPE3 = 3;

}

bean包下 的两个bean类

News   请求数据的bean类

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;
        }

    }
}

LocalNews   操作数据库的bean类


public class LocalNews {
    public String title;
    public String imgurls;
    public String source;
    public String time;
    public boolean isDel;
}

多条目的三种布局

news_item_layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:padding="10dp"
    android:gravity="center_vertical"
    android:layout_width="match_parent" android:layout_height="150dp">

    <ImageView
        android:id="@+id/img1"
        android:src="@mipmap/ic_launcher"
        android:scaleType="centerCrop"
        android:layout_width="150dp"
        android:layout_height="120dp" />
    <TextView
        android:id="@+id/title1"
        android:text="title"
        android:layout_toRightOf="@+id/img1"
        android:layout_marginLeft="30dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RelativeLayout>


news_item2_layout

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

    <TextView
        android:layout_margin="10dp"
        android:id="@+id/title2"
        android:text="title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <ImageView
            android:id="@+id/img1"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_marginRight="5dp"
            android:scaleType="centerCrop"
            android:src="@mipmap/ic_launcher"
            android:layout_height="120dp" />
        <ImageView
            android:id="@+id/img2"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_marginLeft="5dp"
            android:scaleType="centerCrop"
            android:src="@mipmap/ic_launcher"
            android:layout_height="120dp" />

    </LinearLayout>
</LinearLayout>


news_item3_layout

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

    <TextView
        android:layout_margin="10dp"
        android:id="@+id/title3"
        android:text="title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <ImageView
            android:id="@+id/img1"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_marginRight="10dp"
            android:scaleType="centerCrop"
            android:src="@mipmap/ic_launcher"
            android:layout_height="120dp" />
        <ImageView
            android:id="@+id/img2"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:scaleType="centerCrop"
            android:src="@mipmap/ic_launcher"
            android:layout_height="120dp" />
        <ImageView
            android:id="@+id/img3"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_marginLeft="10dp"
            android:scaleType="centerCrop"
            android:src="@mipmap/ic_launcher"
            android:layout_height="120dp" />

    </LinearLayout>
</LinearLayout>

最最最最最最最最最最最重要的适配器!!!!!!!!!!!!!!!!

NewsAdapter

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_item_layout,parent,false);
            return new MyViewHolder01(view);
        }else if (viewType == Constants.TYPE2){
            View view2 = LayoutInflater.from(context).inflate(R.layout.news_item2_layout,parent,false);
            return new MyViewHolder02(view2);
        }else {
            View view3 = LayoutInflater.from(context).inflate(R.layout.news_item3_layout,parent,false);
            return new MyViewHolder03(view3);
        }


    }

    @Override
    public void onBindViewHolder(@NonNull final RecyclerView.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 layoutPosition = holder.getLayoutPosition()-1;
                        list.remove(layoutPosition);//删除集合的数据
//                        beans.data = list;
//                        String s = new Gson().toJson(beans);
                        notifyItemRemoved(layoutPosition);//局部删除当前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 MyViewHolder03){
            ((MyViewHolder03) 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(((MyViewHolder03) holder).img1);
                    Glide.with(context).load(data.miniimg.get(0).src).into(((MyViewHolder03) holder).img2);
                    Glide.with(context).load(data.miniimg.get(0).src).into(((MyViewHolder03) holder).img3);
                } else if (data.miniimg.size() == 2) {
                    Glide.with(context).load(data.miniimg.get(0).src).into(((MyViewHolder03) holder).img1);
                    Glide.with(context).load(data.miniimg.get(1).src).into(((MyViewHolder03) holder).img2);
                    Glide.with(context).load(data.miniimg.get(1).src).into(((MyViewHolder03) holder).img3);
                } else {
                    Glide.with(context).load(data.miniimg.get(0).src).into(((MyViewHolder03) holder).img1);
                    Glide.with(context).load(data.miniimg.get(1).src).into(((MyViewHolder03) holder).img2);
                    Glide.with(context).load(data.miniimg.get(2).src).into(((MyViewHolder03) holder).img3);
                }
            }
        }else if (holder instanceof MyViewHolder02){
            ((MyViewHolder02) holder).title.setText(data.topic);
            if (data.miniimg.size() == 1){
                Glide.with(context).load(data.miniimg.get(0).src).into(((MyViewHolder02) holder).img1);
                Glide.with(context).load(data.miniimg.get(0).src).into(((MyViewHolder02) holder).img2);
            }else {
                Glide.with(context).load(data.miniimg.get(0).src).into(((MyViewHolder02) holder).img1);
                Glide.with(context).load(data.miniimg.get(1).src).into(((MyViewHolder02) holder).img2);
            }
        }else if (holder instanceof MyViewHolder01){
            ((MyViewHolder01) holder).title.setText(data.topic);
            Glide.with(context).load(data.miniimg.get(0).src).into(((MyViewHolder01) holder).img1);
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position % 3 == 0){
            return Constants.TYPE1;
        }else if (position % 3 == 1){
            return Constants.TYPE2;
        }else {
            return Constants.TYPE3;
        }
    }

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

    public class MyViewHolder01 extends XRecyclerView.ViewHolder{
        ImageView img1;
        TextView title;
        public MyViewHolder01(View itemView) {
            super(itemView);
            img1 = itemView.findViewById(R.id.img1);
            title = itemView.findViewById(R.id.title1);
        }
    }
    public class MyViewHolder02 extends XRecyclerView.ViewHolder{
        ImageView img1,img2;
        TextView title;
        public MyViewHolder02(View itemView) {
            super(itemView);
            img1 = itemView.findViewById(R.id.img1);
            img2 = itemView.findViewById(R.id.img2);
            title = itemView.findViewById(R.id.title2);
        }
    }
    public class MyViewHolder03 extends XRecyclerView.ViewHolder{
        ImageView img1,img2,img3;
        TextView title;
        public MyViewHolder03(View itemView) {
            super(itemView);
            img1 = itemView.findViewById(R.id.img1);
            img2 = itemView.findViewById(R.id.img2);
            img3 = itemView.findViewById(R.id.img3);
            title = itemView.findViewById(R.id.title3);
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值