ListView联动
底部是RadioButton实现的,下面的图片自己可以自备。
activity_main xml文件
<android.support.design.widget.TabLayout xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/main_content_tab" android:layout_width="match_parent" android:layout_height="60dp" tools:layout_editor_absoluteX="8dp" app:layout_constraintBottom_toBottomOf="parent" android:background="#F4F4F4" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" app:tabSelectedTextColor="#68c1dc" app:tabIndicatorHeight="0dp" ></android.support.design.widget.TabLayout> <ListView android:id="@+id/left" android:layout_width="120dp" android:background="#eaebec" android:layout_height="match_parent" android:layout_above="@id/main_content_tab"/> <ListView android:id="@+id/right" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/left" android:layout_toEndOf="@+id/left" android:layout_above="@id/main_content_tab"/>
左边的listview xml文件
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/main_activity_tab"> <TextView android:id="@+id/main_content_leftitem" android:layout_width="match_parent" android:layout_height="60dp" android:textSize="16sp" android:textColor="#000" android:gravity="center" /> </RelativeLayout>
在Draweble里写个文件
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@color/green"></item> <item android:drawable="@color/white"></item> </selector>
现在开始Class类了,依然使用Xutils
public class App extends Application { @Override public void onCreate() { super.onCreate(); x.Ext.init(this); x.Ext.setDebug(BuildConfig.DEBUG); } }
Leftadapter左边的适配器
public class Leftadapter extends BaseAdapter { private Context context; private List<LeftListData.DataBean.CategoriesBean> list; public Leftadapter(Context context, List<LeftListData.DataBean.CategoriesBean> list) { this.context = context; this.list = list; } @Override public int getCount() { return list != null ? list.size():0; } @Override public Object getItem(int i) { return list.get(i); } @Override public long getItemId(int i) { return i; } @Override public View getView(int i, View view, ViewGroup viewGroup) { ViewHodler viewHodler; if(view==null){ viewHodler = new ViewHodler(); view = View.inflate(context,R.layout.main_left_item,null); viewHodler.textView = view.findViewById(R.id.main_content_leftitem); view.setTag(viewHodler); }else{ viewHodler = (ViewHodler) view.getTag(); } viewHodler.textView.setText(list.get(i).getName()); return view; } class ViewHodler{ TextView textView; } }
LeftListData 类 这个是接口类,存放数据 主Activity类
public class MainActivity extends AppCompatActivity { private TabLayout tabLayout; private List<LeftListData.DataBean.CategoriesBean> mlist = new ArrayList<>(); private List<String> mlista = new ArrayList<String>(); private List<LeftListData.DataBean.CategoriesBean> rightList = new ArrayList<>(); private ListView left,right; private BaseAdapter rightadapter; private LeftListData.DataBean.CategoriesBean.ProductsBean productsBean; private Leftadapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); inintdata(); LoadData(); adapter = new Leftadapter(MainActivity.this, mlist); left.setAdapter(adapter); left.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { rightadapter = new Rightadapter(MainActivity.this,rightList.get(position).getProducts()); right.setAdapter(rightadapter); } }); } private void LoadData() { RequestParams mparamms = new RequestParams("http://api.eleteam.com/v1/category/list-with-product"); x.http().get(mparamms, new Callback.CommonCallback<String>() { @Override public void onSuccess(String result) { Gson mgson = new Gson(); LeftListData leftListData = mgson.fromJson(result, LeftListData.class); mlist.addAll(leftListData.getData().getCategories()); for (LeftListData.DataBean.CategoriesBean a:mlist) { mlista.add(a.getName()); rightList.add(a); Log.e("name", "onSuccess: "+a.getName()+a.getProducts().toString()); } adapter.notifyDataSetChanged(); rightadapter.notifyDataSetChanged(); } @Override public void onError(Throwable ex, boolean isOnCallback) { } @Override public void onCancelled(CancelledException cex) { } @Override public void onFinished() { } }); } private void inintdata() { tabLayout = (TabLayout) findViewById(R.id.main_content_tab); left = (ListView) findViewById(R.id.left); right = (ListView) findViewById(R.id.right); tabLayout.addTab(tabLayout.newTab().setText("月光茶人").setIcon(R.drawable.tab_home)); tabLayout.addTab(tabLayout.newTab().setText("优惠").setIcon(R.drawable.tab_topic)); tabLayout.addTab(tabLayout.newTab().setText("购物车").setIcon(R.drawable.main_index_cart_normal)); tabLayout.addTab(tabLayout.newTab().setText("我的").setIcon(R.drawable.main_index_my_normal)); } }
RightAdapter类
public class Rightadapter extends BaseAdapter { List<LeftListData.DataBean.CategoriesBean.ProductsBean> data; LayoutInflater inflater; Context context; public Rightadapter(Context context, List<LeftListData.DataBean.CategoriesBean.ProductsBean> data) { this.context=context; inflater = LayoutInflater.from(context); if (data != null) { this.data=data; }else { this.data=new ArrayList<>(); } } public void addRes(List<LeftListData.DataBean.CategoriesBean.ProductsBean> data){ if (data != null) { this.data.clear(); this.data.addAll(data); notifyDataSetChanged(); } } @Override public int getCount() { return data.size(); } @Override public LeftListData.DataBean.CategoriesBean.ProductsBean 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.main_right_item, parent, false); holder = new ViewHolder(convertView); convertView.setTag(holder); } else { holder= (ViewHolder) convertView.getTag(); } holder.mTitle.setText(getItem(position).getName()); Log.e("name", "onSuccess: "+getItem(position).getName()+getItem(position).getApp_long_image1()); holder.mMsg.setText("¥"+getItem(position).getPrice()); Glide.with(context).load(getItem(position).getImage_small()).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); } } }