美文网首页Android开发手机移动程序开发Android UI
RecyclerView简单实现列表和网格切换

RecyclerView简单实现列表和网格切换

作者: DrJasonZhang | 来源:发表于2017-01-17 16:13 被阅读0次

效果图

bbb.gif.gif

布局文件

<?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.jason.togglelistgrid.MainActivity">
 
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="切换" />


    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>
<?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:background="@color/colorPrimaryDark"
    android:layout_height="wrap_content">

    <TextView
        android:layout_marginBottom="5dp"
        android:background="@color/colorAccent"
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:textSize="15px"
        android:gravity="center" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/sub_rv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />


</LinearLayout>

关键代码

    public class MainActivity extends AppCompatActivity {
    RecyclerView rv;
    int lastPostion;  //切换前rv的第一个item的位置

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        rv = (RecyclerView) findViewById(R.id.rv);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        rv.setLayoutManager(linearLayoutManager);
        final MyAdapter myAdapter = new MyAdapter(R.layout.rv_item, DataServer.getData(),true);
        rv.setAdapter(myAdapter);

        Button btn = (Button) findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (rv.getLayoutManager() instanceof  GridLayoutManager){
                    lastPostion = ((GridLayoutManager)rv.getLayoutManager()).findFirstVisibleItemPosition();
                    rv.setLayoutManager(new LinearLayoutManager(MainActivity.this));
                }else {
                    lastPostion = ((LinearLayoutManager)rv.getLayoutManager()).findFirstVisibleItemPosition();
                    rv.setLayoutManager(new GridLayoutManager(MainActivity.this,3));
                }
                rv.setAdapter(myAdapter);
                rv.scrollToPosition(lastPostion);
            }
        });
    }
    .....
}

说明

实现也比较简单就通过切换RecyclerView的布局管理器实现Item的不同布局, 
lastPostion则是用于回到切换之前的位置,通过scrollToPosition()方法滚动到切换之前的位置

相关文章

网友评论

    本文标题:RecyclerView简单实现列表和网格切换

    本文链接:https://www.haomeiwen.com/subject/brwmbttx.html