android RecyclerView的线性跟网格互换


点击图片的背景:

                   

drawable包下的文件:item.xml文件

 


<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item  android:state_checked="true" android:drawable="@drawable/kind_grid"></item>
    <item  android:state_checked="false" android:drawable="@drawable/kind_liner"></item>
</selector>
main的布局:



    <CheckBox
                        android:button="@null"
                        android:id="@+id/iv_msg"
                        android:layout_width="30dp"
                        android:layout_height="20dp"
                        android:background="@drawable/item2"
                        />
  <android.support.v7.widget.RecyclerView
      android:id="@+id/lv"
      android:layout_width="match_parent"
      android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>

</LinearLayout>

主要的逻辑上代码:


  private boolean flag=false;  
  private SelectRelAdapter selectRelAdapter;  
  private CheckBox img;  
  
rcy =  findViewById(R.id.lv);  
img= findViewById(R.id.iv_msg);  
  

 img.setChecked(flag);  
      img.setOnClickListener(new View.OnClickListener() {  
          @Override  
          public void onClick(View view) {  
           if(flag){  
              adapter();  
                img.setChecked(false);  
                flag=img.isChecked();  
           }else {  
                adapter1();  
               img.setChecked(true);  
               flag=img.isChecked();  
           }  
          }  
      });  
  
      if(img.isChecked()){  
          adapter();  
      }else {  
          adapter1();  
      }  

private void  adapter(){  
      rcy.setLayoutManager(new LinearLayoutManager(this));  
      SelectRelAdapter selectRelAdapter = new SelectRelAdapter(data, this);  
      rcy.setAdapter(selectRelAdapter);  
      
  
  }  

  private void  adapter1(){  
      rcy.setLayoutManager(new GridLayoutManager(this,2));  
      selectRelAdapter = new SelectRelAdapter(data, this);  
      rcy.setAdapter(selectRelAdapter);  
      
  }  
阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭