6步轻松实现两个listView联动效果
程序员文章站
2023-01-21 11:10:15
看了网上更新的好多联动demo,感觉写的不是很简洁(表示不知道他们在说什么)
自己写了一个简单的demo分享给大家- -!
效果图:
直接上车,少说废话!...
看了网上更新的好多联动demo,感觉写的不是很简洁(表示不知道他们在说什么)
自己写了一个简单的demo分享给大家- -!
效果图:
直接上车,少说废话!
所用到以下的这几个依赖,直接粘到build.gradle文件中
compile 'com.squareup.picasso:picasso:2.5.2' compile 'io.reactivex:rxjava:1.2.7' compile 'io.reactivex:rxandroid:1.2.1' compile 'com.squareup.retrofit2:retrofit:2.2.0' compile 'com.squareup.retrofit2:converter-gson:2.2.0' compile 'com.squareup.retrofit2:adapter-rxjava:2.2.0'
1.定义首页的布局文件
<?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:id="@+id/activity_main" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.twolist.mainactivity"> <listview android:id="@+id/left" android:layout_width="88dp" android:background="#eaebec" android:layout_height="match_parent" /> <listview android:id="@+id/right" android:layout_width="match_parent" android:layout_height="match_parent" /> </linearlayout>
2.mainactivity.java
package com.example.twolist; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.view; import android.widget.abslistview; import android.widget.adapterview; import android.widget.listview; import com.example.twolist.adapters.leftadapter; import com.example.twolist.adapters.rightadapter; import retrofit2.retrofit; import retrofit2.adapter.rxjava.rxjavacalladapterfactory; import retrofit2.converter.gson.gsonconverterfactory; import rx.observable; import rx.subscriber; import rx.android.schedulers.androidschedulers; import rx.schedulers.schedulers; public class mainactivity extends appcompatactivity{ /** * 右侧电影列表的接口 * http://app.vmoiver.com/apiv3/post/getpostincate?cateid=0&p=1 */ private listview mleft; private listview mright; private rightadapter rightadapter; private leftadapter adapter; //记录滑动的listview 滑动的位置 private int scrollposition = -1; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); initview(); getdata(); } private void getdata() { retrofit retrofit = new retrofit.builder() .addcalladapterfactory(rxjavacalladapterfactory.create()) .addconverterfactory(gsonconverterfactory.create()) .baseurl("http://app.vmoiver.com").build(); apiservice apiservice = retrofit.create(apiservice.class); observable<parasedata> apiservicemovielist = apiservice.getmovielist(); apiservicemovielist.subscribeon(schedulers.io()) .observeon(androidschedulers.mainthread()) .subscribe(new subscriber<parasedata>() { @override public void oncompleted() { } @override public void onerror(throwable e) { } @override public void onnext(parasedata parasedata) { rightadapter.addres(parasedata.getdata()); } }); } private void initview() { mleft = (listview) findviewbyid(r.id.left); mright = (listview) findviewbyid(r.id.right); adapter = new leftadapter(this); mleft.setadapter(adapter); rightadapter = new rightadapter(this, null); mright.setadapter(rightadapter); mleft.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { adapter.setselectitem(position); mright.setselection(position); } }); mright.setonscrolllistener(new abslistview.onscrolllistener() { @override public void onscrollstatechanged(abslistview view, int scrollstate) { } @override public void onscroll(abslistview view, int firstvisibleitem, int visibleitemcount, int totalitemcount) { if (scrollposition != firstvisibleitem) { adapter.setselectitem(firstvisibleitem); mleft.setselectionfromtop(firstvisibleitem, 40); scrollposition = firstvisibleitem; } } }); } }
3.左边listview的适配器
package com.example.twolist.adapters; import android.content.context; import android.graphics.color; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.baseadapter; import android.widget.textview; import com.example.twolist.r; import java.util.arraylist; import java.util.list; /** * created by along on 2017/4/5. */ public class leftadapter extends baseadapter { list<string> data; layoutinflater inflater; private int selectitem=0; public void setselectitem(int selectitem) { this.selectitem = selectitem; notifydatasetchanged(); } public leftadapter(context context) { data=new arraylist<>(); for (int i = 0; i < 10; i++) { data.add("电影"+i); } inflater=layoutinflater.from(context); } @override public int getcount() { return data!=null?data.size():0; } @override public string getitem(int position) { return data.get(position); } @override public long getitemid(int position) { return position; } @override public view getview(int position, view convertview, viewgroup parent) { viewholder holder; if (convertview == null) { convertview=inflater.inflate(r.layout.left_item,parent,false); holder=new viewholder(convertview); convertview.settag(holder); }else { holder= (viewholder) convertview.gettag(); } holder.mtext.settext(data.get(position)); if (selectitem == position) { holder.mtext.settextcolor(color.blue); holder.mindicator.setvisibility(view.visible); }else { holder.mtext.settextcolor(color.black); holder.mindicator.setvisibility(view.invisible); } return convertview; } public static class viewholder{ textview mtext; view mindicator; public viewholder(view itemview) { mtext= (textview) itemview.findviewbyid(r.id.left_text); mindicator=itemview.findviewbyid(r.id.left_indicator); } } }
4.右边listview的适配器
package com.example.twolist.adapters; import android.content.context; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.baseadapter; import android.widget.imageview; import android.widget.textview; import com.example.twolist.parasedata; import com.example.twolist.r; import com.squareup.picasso.picasso; import java.util.arraylist; import java.util.list; /** * created by along on 2017/4/5. */ public class rightadapter extends baseadapter { list<parasedata.databean> data; layoutinflater inflater; context context; public rightadapter(context context, list<parasedata.databean> data) { this.context=context; inflater = layoutinflater.from(context); if (data != null) { this.data=data; }else { this.data=new arraylist<>(); } } public void addres(list<parasedata.databean> data){ if (data != null) { this.data.clear(); this.data.addall(data); notifydatasetchanged(); } } @override public int getcount() { return data.size(); } @override public parasedata.databean getitem(int position) { return data.get(position); } @override public long getitemid(int position) { return position; } @override public view getview(int position, view convertview, viewgroup parent) { viewholder holder; if (convertview == null) { convertview = inflater.inflate(r.layout.right_item, parent, false); holder = new viewholder(convertview); convertview.settag(holder); } else { holder= (viewholder) convertview.gettag(); } holder.mtitle.settext(getitem(position).gettitle()); holder.mmsg.settext(getitem(position).gettitle()); picasso.with(context).load(getitem(position).getimage()).into(holder.mimg); return convertview; } public static class viewholder { imageview mimg; textview mtitle; textview mmsg; public viewholder(view itemview) { mimg = (imageview) itemview.findviewbyid(r.id.right_img); mtitle = (textview) itemview.findviewbyid(r.id.right_text); mmsg = (textview) itemview.findviewbyid(r.id.right_msg); } } }
5.retrofit解析的接口文件
package com.example.twolist; import retrofit2.http.get; import rx.observable; /** * created by along on 2017/4/5. */ public interface apiservice { //可以使用参数的拼接,实现分页的加载,简单的demo就不搞那么复杂了 @get("/apiv3/post/getpostincate?cateid=0&p=1") observable<parasedata> getmovielist(); }
6.gson解析需要使用的类吐舌头
package com.example.twolist; import java.util.list; /** * created by along on 2017/4/5. */ public class parasedata { private list<databean> data; public list<databean> getdata() { return data; } public void setdata(list<databean> data) { this.data = data; } public static class databean { private string title; private string image; public string gettitle() { return title; } public void settitle(string title) { this.title = title; } public string getimage() { return image; } public void setimage(string image) { this.image = image; } } }
然后就没有然后了,6个步骤写完了。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: 蒸鱼豉油有哪些用法呢