Android可上下左右滑动的列表

前言:

最开始使用的HorizontalScrollView和ListView当数据量上去之后发现有些低性能的手机变得卡顿,手机屏幕刷新会黑屏(黑平的时候我都惊呆了)

实现

废话不多说了接下来实现功能,处于考虑我决定使用RecyclerView然后让他横向滑动并且让他们同步这样横向滑动就解决了,然后竖着滑动使用ScrollView,这就是我的思路。
RecyclerView Adapter 因为这里的item资源复用,渲染界面一定要仔细,不然一个界面变了会影响其他复用的界面

1.测试bean


public class Bean {
     
    private String color;
    private String name;

    public String getColor() {
     
        return color;
    }

    public void setColor(String color) {
     
        this.color = color;
    }

    public String getName() {
     
        return name;
    }

    public void setName(String name) {
     
        this.name = name;
    }

    public Bean() {
     
    }
}

2.界面布局

主activity

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:id="@+id/box"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

        </LinearLayout>


    </ScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>

RecyclerView item

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:id="@+id/lay_item_auto_check_device"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="80dp"
        android:layout_height="70dp"
        android:background="@drawable/item_bg"
        android:layout_margin="5dp"
        android:gravity="center"
        android:orientation="vertical">

        <LinearLayout
            android:id="@+id/item_background"
            android:layout_width="60dp"
            android:layout_height="55dp"
            android:background="#e6e6e6"
            android:orientation="vertical">

            <TextView
                android:id="@+id/item_textview"
                android:layout_width="60dp"
                android:layout_height="35dp"
                android:gravity="center"
                android:focusable="true"
                android:focusableInTouchMode="false"
                android:textColor="#ffffff"
                android:textStyle="bold"
                android:textSize="20dp"/>

        </LinearLayout>

    </LinearLayout>

</LinearLayout>

3.适配器

import java.util.List;

public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {
     

    private List<Bean> list;

    public Adapter(List<Bean> list) {
     
        this.list = list;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
     
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item,parent,false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
     
        Bean bean = list.get(position);
        holder.lay.setBackgroundColor(Color.parseColor(bean.getColor()));
        holder.tv.setText(bean.getName());
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {
     
        LinearLayout lay;
        TextView tv;
        public ViewHolder(@NonNull View itemView) {
     
            super(itemView);
            lay = itemView.findViewById(R.id.item_background);
            tv = itemView.findViewById(R.id.item_textview);
        }
    }
}

4.avtivity 代码

public class MainActivity extends AppCompatActivity {
     

    @Override
    protected void onCreate(Bundle savedInstanceState) {
     
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //1.获取控件
        LinearLayout boxView = findViewById(R.id.box);
        //2.生成模拟数据
        List<Bean> list = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
     
            Bean bean = new Bean();
            bean.setName("TNT" + i);
            switch (i % 10) {
     
                case 0:
                    bean.setColor("#ffbd21");
                    break;
                case 1:
                    bean.setColor("#99cc00");
                    break;
                case 2:
                    bean.setColor("#ff002a");
                    break;
                case 3:
                    bean.setColor("#57C5E8");
                    break;
                case 4:
                    bean.setColor("#B0B0B0");
                    break;
                case 5:
                    bean.setColor("#59B29C");
                    break;
                case 6:
                    bean.setColor("#ff6b29");
                    break;
                case 7:
                    bean.setColor("#00a0e9");
                    break;
                case 8:
                    bean.setColor("#f0f66e");
                    break;
                case 9:
                    bean.setColor("#FFA4A9");
                    break;
            }
            list.add(bean);
        }
        //3.根据数据生成界面
        final List<RecyclerView> syncRecyclerViews = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
     
            RecyclerView syncRecyclerView = new RecyclerView(this);
            Adapter adapter = new Adapter(list);
            LinearLayoutManager ms = new LinearLayoutManager(this);
            ms.setOrientation(LinearLayoutManager.HORIZONTAL);
            syncRecyclerView.setLayoutManager(ms);
            syncRecyclerView.setAdapter(adapter);
            syncRecyclerViews.add(syncRecyclerView);
            syncRecyclerView.setOverScrollMode(View.OVER_SCROLL_NEVER);//取消边缘波浪样式
            boxView.addView(syncRecyclerView);
            //添加监听,使所有的RecyclerView保持同步
            RecyclerView.OnScrollListener listener = new RecyclerView.OnScrollListener() {
     
                @Override
                public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
     
                    for (RecyclerView item : syncRecyclerViews) {
     
                        if (recyclerView == item) {
     
                            continue;
                        }
                        item.clearOnScrollListeners();
                        item.scrollBy(dx, dy);
                        item.addOnScrollListener(this);
                    }
                }
            };
            syncRecyclerView.addOnScrollListener(listener);
        }
    }
}

最后奉上github地址

https://github.com/1142235090/android-table-slide

你可能感兴趣的:(android)