Android 两个ListView联动,仿电影展示界面
程序员文章站
2022-05-09 20:38:03
Android 两个ListView联动,仿电影展示界面
需要导入的依赖包:
compile 'com.squareup.picasso:picasso:2.5...
Android 两个ListView联动,仿电影展示界面
需要导入的依赖包:
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'
xml布局:(布局里面是两个listview,方向是横向的)
适配器:(既然是两个布局格式不同listview,就必须是两个适配器了,下面是左边listview的适配器,左边控制listview标题,右边控制展示)
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 java.util.ArrayList; import java.util.List; public class LeftAdapter extends BaseAdapter { List 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); } } }
listview右边适配器:
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.squareup.picasso.Picasso; import java.util.ArrayList; import java.util.List; public class RightAdapter extends BaseAdapter { Listdata; LayoutInflater inflater; Context context; public RightAdapter(Context context, List data) { this.context=context; inflater = LayoutInflater.from(context); if (data != null) { this.data=data; }else { this.data=new ArrayList<>(); } } public void addRes(List 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); } } }
总实现代码:
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 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还有就是加网络权限。apiServiceMovieList = apiService.getMovieList(); apiServiceMovieList.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Subscriber () { @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; } } }); } }