Andriod实现类似excel布局

版权声明:本文为博主原创文章,未经博主允许不得转载。https://blog.csdn.net/chensenli/article/details/83536091

前言

“汽车报价大全”中的车型对比界面 ,可以左右滑动查看不同的车型,上下滑动查看不同的参数。所以我模仿写了一个,不过,本例子中车型数量最大为3。效果图见下:

下面来谈谈具体实现方法

界面布局

由于汽车车型和下面具体的参数值要报纸左右滑动同步,所以都采用水平的ScrollView进行包裹。

下面红色大块要上下滑动,用一个ScrollView包裹即可。

布局代码

<?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"
    android:orientation="vertical"
    tools:context="com.csl.carcompare.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="88dp"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="汽车名称" />

        <View
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="#cccccc" />

        <com.csl.carcompare.MyScrollView
            android:id="@+id/sv_above"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:overScrollMode="never"
            android:scrollbars="none">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:orientation="horizontal">

                <RelativeLayout
                    android:id="@+id/rl1"
                    android:layout_width="120dp"
                    android:layout_height="40dp">

                    <TextView
                        android:id="@+id/tv1"
                        android:layout_width="120dp"
                        android:layout_height="40dp"
                        android:gravity="center"/>

                    <ImageView
                        android:id="@+id/iv_delete1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentRight="true"
                        android:layout_alignParentTop="true"
                        android:src="@mipmap/ic_close" />
                </RelativeLayout>

                <View
                    android:id="@+id/view1"
                    android:layout_width="1dp"
                    android:layout_height="40dp"
                    android:background="#cccccc" />

                <RelativeLayout
                    android:id="@+id/rl2"
                    android:layout_width="120dp"
                    android:layout_height="40dp">

                    <TextView
                        android:id="@+id/tv2"
                        android:layout_width="120dp"
                        android:layout_height="40dp"
                        android:gravity="center"/>

                    <ImageView
                        android:id="@+id/iv_delete2"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentRight="true"
                        android:layout_alignParentTop="true"
                        android:src="@mipmap/ic_close" />
                </RelativeLayout>
                <View
                    android:id="@+id/view2"
                    android:layout_width="1dp"
                    android:layout_height="40dp"
                    android:background="#cccccc" />

                <RelativeLayout
                    android:id="@+id/rl3"
                    android:layout_width="120dp"
                    android:layout_height="40dp">

                    <TextView
                        android:id="@+id/tv3"
                        android:layout_width="120dp"
                        android:layout_height="40dp"
                        android:gravity="center"/>

                    <ImageView
                        android:id="@+id/iv_delete3"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentRight="true"
                        android:layout_alignParentTop="true"
                        android:src="@mipmap/ic_close" />
                </RelativeLayout>
                <View
                    android:id="@+id/view3"
                    android:layout_width="1dp"
                    android:layout_height="40dp"
                    android:background="#cccccc" />

                <RelativeLayout
                    android:id="@+id/rl4"
                    android:layout_width="100dp"
                    android:layout_height="40dp">

                    <ImageView
                        android:id="@+id/iv_add"
                        android:layout_width="20dp"
                        android:layout_height="20dp"
                        android:layout_centerInParent="true"
                        android:src="@mipmap/ic_add" />
                </RelativeLayout>
            </LinearLayout>
        </com.csl.carcompare.MyScrollView>
    </LinearLayout>
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#cccccc" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:overScrollMode="never"
        android:scrollbars="none">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">
            <com.csl.carcompare.MyListView
                android:id="@+id/lv_left"
                android:layout_width="88dp"
                android:layout_height="match_parent"
                android:overScrollMode="never"
                android:scrollbars="none">
            </com.csl.carcompare.MyListView>
            <View
                android:layout_width="1dp"
                android:layout_height="match_parent"
                android:background="#cccccc"/>
            <com.csl.carcompare.MyScrollView
                android:id="@+id/sv_bottom"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:overScrollMode="never"
                android:scrollbars="none">
                <com.csl.carcompare.MyListView
                    android:id="@+id/lv_right"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:overScrollMode="never"
                    android:scrollbars="none"/>
            </com.csl.carcompare.MyScrollView>
        </LinearLayout>
    </ScrollView>
</LinearLayout>

逻辑代码

1.顶层汽车车型由于数量最大值有限,所以采用RelativeLayout的显示和隐藏来进行车型的增加和删除

2.自定义ListView,解决ScrollView嵌套ListView的问题

public class MyListView extends ListView {
    public MyListView(Context context) {
        super(context);
    }

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

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

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
                MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
    }
}

3.自定义水平滑动的ScrollView,可以实现同步滑动

public class MyScrollView extends HorizontalScrollView {
    MyScrollView myScrollView;
    public void setScrollView(MyScrollView view){
        this.myScrollView = view;
    }
    public MyScrollView(Context context) {
        super(context);
    }

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

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

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        if(myScrollView != null){
            myScrollView.scrollTo(l,t);
        }
    }
}

在主界面的初始化View中,相互调用setScrollView的方法,即可实现同步滑动

  sv_above.setScrollView(sv_bottom);
  sv_bottom.setScrollView(sv_above);

4.其余就是listview的使用和制造模拟数据的代码,就不贴了

类似效果的博客,可参考 https://www.jianshu.com/p/791305fff837

本博客代码链接:https://download.csdn.net/download/chensenli/10758977

Android Studio 中,您可以使用 RecyclerView 和 GridLayout 来创建类似 Excel布局。 以下是一个示例: 1. 在 layout 文件夹中创建一个新的 XML 布局文件,例如 grid_layout.xml。 2. 在 XML 文件中添加以下代码: ```xml <androidx.recyclerview.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" /> ``` 3. 在 Activity 中创建一个 RecyclerView,并将其与 GridLayoutManager 和 GridAdapter 连接起来: ```java public class MainActivity extends AppCompatActivity { private RecyclerView recyclerView; private GridAdapter gridAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.grid_layout); // 设置 RecyclerView recyclerView = findViewById(R.id.recycler_view); recyclerView.setLayoutManager(new GridLayoutManager(this, 4)); // 设置适配器 List<String> data = new ArrayList<>(); for (int i = 1; i <= 20; i++) { data.add("Cell " + i); } gridAdapter = new GridAdapter(data); recyclerView.setAdapter(gridAdapter); } } ``` 4. 创建一个 GridAdapter 类来处理 RecyclerView 的数据和视图: ```java public class GridAdapter extends RecyclerView.Adapter<GridAdapter.ViewHolder> { private List<String> data; public GridAdapter(List<String> data) { this.data = data; } @NonNull @Override public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.grid_cell, parent, false); return new ViewHolder(view); } @Override public void onBindViewHolder(@NonNull ViewHolder holder, int position) { holder.textView.setText(data.get(position)); } @Override public int getItemCount() { return data.size(); } public static class ViewHolder extends RecyclerView.ViewHolder { public TextView textView; public ViewHolder(@NonNull View itemView) { super(itemView); textView = itemView.findViewById(R.id.text_view); } } } ``` 5. 创建一个 grid_cell.xml 布局文件来定义每个单元格的外观: ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/text_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:textColor="@android:color/black" android:padding="16dp" /> </LinearLayout> ``` 6. 运行应用程序,您应该看到一个包含 20 个单元格的 RecyclerView,每个单元格都包含一个文本视图。GridLayoutManger 将每个单元格排列成类似 Excel 表格布局
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值