温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android开发中listView怎么实现联动效果

发布时间:2020-12-02 15:51:25 来源:亿速云 阅读:152 作者:Leah 栏目:移动开发

本篇文章为大家展示了Android开发中listView怎么实现联动效果,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

需要用到以下的这几个依赖,直接粘到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.定义首页的布局文件

<&#63;xml version="1.0" encoding="utf-8"&#63;> 
<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&#63;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<&#63;> 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&#63;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&#63;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; 
    } 
  } 
} 

上述内容就是Android开发中listView怎么实现联动效果,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI