美文网首页
2018-07-24

2018-07-24

作者: 李四的花儿 | 来源:发表于2018-07-24 23:32 被阅读0次

    仿淘宝宝贝详情页面三个Tablayout小demo:

    activity_main布局:

    <?xml version="1.0" encoding="utf-8"?>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity">

    <android.support.design.widget.TabLayout

    android:id="@+id/tb" android:layout_width="match_parent"

    android:layout_height="wrap_content">

    <android.support.design.widget.TabItem

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:text="baby" />

    <android.support.design.widget.TabItem

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:text="discuss"/>

    <android.support.design.widget.TabItem

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:text="detail" />

    </android.support.design.widget.TabLayout>

    <ListView

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:id="@+id/lv"/>

    </LinearLayout>

    MainActivity代码:

    package com.example.lijunlu.taobao;

    import android.os.Bundle;

    import android.support.design.widget.TabLayout;

    import android.support.v7.app.AppCompatActivity;

    import android.view.View;

    import android.widget.AbsListView;

    import android.widget.ListView;

    import android.widget.Toast;

    import java.lang.reflect.Field;

    import java.util.ArrayList;

    public class MainActivityextends AppCompatActivityimplements AbsListView.OnScrollListener {

    private TabLayouttb;

    private ListViewlv;

    private ArrayListlist;

    private int position;

    private CustomAdapteradapter1;

    @Override

        protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    list =new ArrayList<>();

    for (int i =0; i <200; i++) {

    list.add("龙雀" + i);

    }

    initView();

    }

    private void initView() {

    tb = (TabLayout) findViewById(R.id.tb);

    lv = (ListView) findViewById(R.id.lv);

    adapter1 =new CustomAdapter(list,this);

    lv.setAdapter(adapter1);

    lv.setOnScrollListener(this);

    for (int i =0; i

    TabLayout.Tab tab =tb.getTabAt(i);

    if (tab ==null)

    return;

    Class c = tab.getClass();

    try {

    Field field = c.getDeclaredField("mView");

    field.setAccessible(true);

    final View view = (View) field.get(tab);

    if (view ==null)

    return;

    view.setTag(i);

    view.setOnClickListener(new View.OnClickListener() {

    @Override

                        public void onClick(View v) {

    position = (int)view.getTag();

    switch (position) {

    case 0:

    lv.setSelection(0);

    break;

    case 1:

    lv.setSelection(50);

    break;

    case 2:

    lv.setSelection(70);

    break;

    }

    }

    });

    }catch (Exception e) {

    e.printStackTrace();

    }

    }

    }

    int post;

    @Override

        public void onScrollStateChanged(AbsListView view,int scrollState) {

    switch (scrollState) {

    case 0:

    switch (post) {

    case 0:

    TabLayout.Tab tab =tb.getTabAt(0);

    tab.select();

    break;

    case 50:

    TabLayout.Tab tab2 =tb.getTabAt(1);

    tab2.select();

    break;

    case 70:

    TabLayout.Tab tab3 =tb.getTabAt(2);

    tab3.select();

    break;

    }

    Toast.makeText(this,"我现在处于不滑动状态", Toast.LENGTH_SHORT).show();

    break;

    case 1:

    Toast.makeText(this,"我现在处于滑动状态", Toast.LENGTH_SHORT).show();

    break;

    }

    }

    @Override

        public void onScroll(AbsListView view,int firstVisibleItem,int visibleItemCount,int totalItemCount) {

    switch (firstVisibleItem) {

    case 0:

    post =0;

    break;

    case 50:

    post =50;

    break;

    case 70:

    post =70;

    break;

    case 49:

    post =0;

    break;

    case 69:

    post =50;

    break;

    }

    }

    }

    适配器:

    package com.example.lijunlu.taobao;

    import android.content.Context;

    import android.view.LayoutInflater;

    import android.view.View;

    import android.view.ViewGroup;

    import android.widget.BaseAdapter;

    import android.widget.TextView;

    import java.util.ArrayList;

    public class CustomAdapterextends BaseAdapter {

    private ArrayListlist =new ArrayList<>();

    private Contextcontext;

    public CustomAdapter(ArrayList list, Context context) {

    this.list = list;

    this.context = context;

    }

    @Override

        public int getCount() {

    return list.size();

    }

    @Override

        public Object getItem(int position) {

    return list.get(position);

    }

    @Override

        public long getItemId(int position) {

    return position;

    }

    @Override

        public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder viewHolder =null;

    if (convertView ==null) {

    convertView = LayoutInflater.from(context).inflate(R.layout.item,null);

    viewHolder =new ViewHolder(convertView);

    convertView.setTag(viewHolder);

    }else {

    viewHolder = (ViewHolder)convertView.getTag();

    }

    viewHolder.tv.setText(list.get(position));

    return convertView;

    }

    public static class ViewHolder {

    public ViewrootView;

    public TextViewtv;

    public ViewHolder(View rootView) {

    this.rootView = rootView;

    this.tv = (TextView) rootView.findViewById(R.id.tv);

    }

    }

    }

    相关文章

      网友评论

          本文标题:2018-07-24

          本文链接:https://www.haomeiwen.com/subject/oolumftx.html