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

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

先上依赖

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.C
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值