当前位置: 移动技术网 > IT编程>移动开发>Android > android使用 ScrollerView 实现 可上下滚动的分类栏实例

android使用 ScrollerView 实现 可上下滚动的分类栏实例

2019年07月24日  | 移动技术网IT编程  | 我要评论

李宗瑞快播 qvod,殊胜柯子,k123电影

如果不考虑更深层的性能问题,我个人认为scrollerview还是很好用的。而且单用scrollerview就可以实现分类型的recyclerview或listview所能实现的效果。

下面我单单从效果展示方面考虑,使用scrollerview实现如下图所示的可滚动的多条目分类,只是为了跟大家一起分享一下新思路。(平时:若从复用性等方面考虑,这显然是存在瑕疵的~)

特点描述:

1.可上下滚动

2.有类似于网格布局的样式

3.子条目具有点击事件

刚看到这个效果时,首先想到的是使用分类型的recyclerview 或者 listview  ,里面再嵌套gridview来实现。

但转而又一想scrollerview也可以滚动,只要往里面循环添加子item不就可以了吗。

实现的逻辑大致如下:


具体的实现如下:

第一步:布局里写一个scrollerview,里面添加一个竖直的线性布局

第二步:实例化垂直线性布局

allhonor_hscroll = (linearlayout) findviewbyid(r.id.allhonor_hscroll); 

第三步:联网请求获取数据

setallhonordata(); 
/** 
  * 使用okhttp 
  */ 
  public void setallhonordata() { 
    okhttputils 
        .get() 
        .url(url) 
        .build() 
        .execute(new stringcallback() { 
          @override 
          public void onerror(okhttp3.call call, exception e, int id) { 
            log.e("tag", "111"); 
            log.e("tag", "onerror" + e.getmessage()); 
          } 
 
          @override 
          public void onresponse(string response, int id) { 
            log.e("tag", "222"); 
            log.e("tag", "onrespons" + response); 
 
            //联网成功后使用fastjson来解析数据 
            processdata(response); 
          } 
 
          @override 
          public void onbefore(request request, int id) { 
          } 
 
          @override 
          public void onafter(int id) { 
          } 
 
        }); 
  } 
/** 
   * 使用fastjson进行解析 
   * 
   * @param json 
   */ 
  private void processdata(string json) { 
    //使用gsonformat生成对应的bean类 
    com.alibaba.fastjson.jsonobject jsonobject = json.parseobject(json); 
    string data = jsonobject.getstring("data"); 
    list<allhonorbean.hornorbean> hornorslist = json.parsearray(data, allhonorbean.hornorbean.class); 
 
    //测试是否解析数据成功 
//    string strtest = hornorslist.get(0).getregion(); 
//    log.e("tag", strtest); 
 
    //第四步:装配数据,使用两层for循环 
 
  } 

第四步:设置两种item的布局

第一种item布局:item_allhornors0.xml

<?xml version="1.0" encoding="utf-8"?> 
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" 
  android:background="#ffffff" 
  android:orientation="vertical"> 
 
  <textview 
    android:id="@+id/tv_allhornors_big" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="#11000000" 
    android:gravity="center_vertical" 
    android:paddingbottom="12dp" 
    android:paddingleft="13dp" 
    android:paddingtop="12dp" 
    android:text="热门" 
    android:textcolor="#000000" 
    android:textsize="14sp" /> 
 
  <view 
    android:layout_width="match_parent" 
    android:layout_height="1dp" 
    android:background="#11000000" /> 
 
</linearlayout> 

第二种item布局:item_allhornors1.xml

<?xml version="1.0" encoding="utf-8"?> 
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" 
  android:layout_width="match_parent" 
  android:layout_height="wrap_content" 
  android:background="#ffffff" 
  android:orientation="vertical"> 
 
  <linearlayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 
 
    <textview 
      android:id="@+id/tv_allhornors_sn0" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:clickable="true" 
      android:gravity="center" 
      android:paddingbottom="12sp" 
      android:paddingtop="12sp" 
      android:text="你好你好" 
      android:textcolor="#000000" 
      android:textsize="13sp" /> 
 
    <view 
      android:layout_width="1dp" 
      android:layout_height="match_parent" 
      android:background="#11000000" /> 
 
    <!--注意这里的text文本一定要为空,不要设置任何内容--> 
    <textview 
      android:id="@+id/tv_allhornors_sn1" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:clickable="true" 
      android:gravity="center" 
      android:paddingbottom="12sp" 
      android:paddingtop="12sp" 
      android:text="" 
      android:textcolor="#000000" 
      android:textsize="13sp" /> 
  </linearlayout> 
 
  <view 
    android:layout_width="match_parent" 
    android:layout_height="1dp" 
    android:background="#11000000" /> 
 
</linearlayout> 

第五步:装配数据

if (hornorslist != null && hornorslist.size() > 0) { 
      //-->外层 
      for (int i = 0; i < hornorslist.size(); i++) { 
        //创建并添加第一种item布局 
        view globalview = view.inflate(this, r.layout.item_allhornors0, null); 
        textview tv_allhornors_big = (textview) globalview.findviewbyid(r.id.tv_allhornors_big); 
        allhonorbean.hornorbean hornorslistbean = hornorslist.get(i); 
        string region = hornorslistbean.getregion(); 
        //外层for中直接装配数据 
        tv_allhornors_big.settext(region); 
        //将布局添加进去 
        allhonor_hscroll.addview(globalview); 
 
        list<allhonorbean.hornorbean.festivalsbean> festivalslist = hornorslistbean.getfestivals(); 
        //-->内层,每次装两个数据 
        for (int j = 0; j < festivalslist.size(); j = j + 2) { 
          //创建并添加第二种item布局 
          view smallview = view.inflate(this, r.layout.item_allhornors1, null); 
          final textview tv_sn0 = (textview) smallview.findviewbyid(r.id.tv_allhornors_sn0); 
          textview tv_sn1 = (textview) smallview.findviewbyid(r.id.tv_allhornors_sn1); 
 
          //顺带在这里就直接添加点击事件的监听 
          if (j < festivalslist.size() - 1) { 
            setlistener(tv_sn0, tv_sn1); 
          } 
 
          //装配左边的数据 
          honorname0 = festivalslist.get(j).getfestivalname(); 
          tv_sn0.settext(honorname0); 
 
          //判读越界否 
          if (j < festivalslist.size() - 1) { 
            //装配右边的数据 
            honorname1 = festivalslist.get(j + 1).getfestivalname(); 
            tv_sn1.settext(honorname1); 
          } 
 
          //添加进去 
          allhonor_hscroll.addview(smallview); 
        } 
      } 
    } 

点击事件的监听:

private void setlistener(final textview tv_sn0, final textview tv_sn1) { 
    //给左边的textview 设置监听 
    tv_sn0.setonclicklistener(new view.onclicklistener() { 
      @override 
      public void onclick(view view) { 
        toast.maketext(mainactivity.this, "" + tv_sn0.gettext(), toast.length_short).show(); 
      } 
    }); 
 
    //给右边的textview 设置监听 
    tv_sn1.setonclicklistener(new view.onclicklistener() { 
      @override 
      public void onclick(view view) { 
        toast.maketext(mainactivity.this, "" + tv_sn1.gettext(), toast.length_short).show(); 
      } 
    }); 
  } 

完成~

再看一眼最后效果:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网