Android单击视图上方的视图

时间:2017-09-28 14:06:35

标签: android

我有标签布局,动态添加了自定义标签(图片视图+文字视图)

我想在选择标签和重新选择(2种图像)上更改图像

但是Tab上没有点击事件监听器,所以我添加了透明布局来捕捉点击。

如何更改设置,使4个线性布局可点击?

代码:

<RelativeLayout
    android:id="@+id/layout_sort_bar_fc"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_alignParentBottom="true">

        <android.support.design.widget.TabLayout
            android:id="@+id/sort_bar_fc"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clickable="false"
            android:focusable="false"
            android:focusableInTouchMode="false"
            app:tabMaxWidth="0dp"
            app:tabGravity="fill"
            app:tabMode="fixed"
            app:tabBackground="@color/colorPrimary"
            app:tabIndicatorColor="#f00"
            app:tabSelectedTextColor="@color/white_text"
            app:tabTextColor="@color/colorPrimaryDark"/>

        <LinearLayout
            android:id="@+id/tab_listenter"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <LinearLayout
                android:id="@+id/dummy_tab_name"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="0"
                android:clickable="true"
                android:orientation="horizontal"></LinearLayout>

            <LinearLayout
                android:id="@+id/dummy_tab_rate"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="0"
                android:orientation="horizontal"></LinearLayout>

            <LinearLayout
                android:id="@+id/dummy_tab_change"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="0"
                android:orientation="horizontal"></LinearLayout>

            <LinearLayout
                android:id="@+id/dummy_tab_24h"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="0"
                android:orientation="horizontal"></LinearLayout>

        </LinearLayout>


    </RelativeLayout>

2 个答案:

答案 0 :(得分:0)

以下answer处理的问题有些相同,标记为解决方案的答案正是您所寻找的,它解释了如何定义和使用选择器

答案 1 :(得分:0)

那么,您可以通过两种方法在选定的选项卡上更改图像,您需要在自定义寻呼机适配器中创建它,并从选项卡选择的侦听器中调用它。

“MainActivity”

public class MainActivity extends AppCompatActivity {
    TabLayout tabLayout;
    ViewPager viewPager;
    ViewPagerAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        viewPager = (ViewPager) findViewById(R.id.viewpager);


        tabLayout = (TabLayout) findViewById(R.id.tabs);

        createViewPager(viewPager);

        tabLayout.setupWithViewPager(viewPager);

        createTabIcons();

        adapter.SetOnSelectView(tabLayout,0);
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {

                int c = tab.getPosition();
                adapter.SetOnSelectView(tabLayout,c);

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                int c = tab.getPosition();
                adapter.SetUnSelectView(tabLayout,c);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });


        for (int i = 0; i < tabLayout.getTabCount() - 1; i++) {
            View tab = ((ViewGroup) tabLayout.getChildAt(0)).getChildAt(i);
            ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) tab.getLayoutParams();
            p.setMargins(0, 0, 10, 0);
            tab.requestLayout();
        }


    }


    private void createTabIcons() {
        try {
            View tabOne = LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
            TextView textView = tabOne.findViewById(R.id.tab);
            ImageView imgViewTabIcon = tabOne.findViewById(R.id.imgViewTabIcon);
            imgViewTabIcon.setImageResource(R.drawable.img_11);
            textView.setText("A");
            tabLayout.getTabAt(0).setCustomView(tabOne);


            View tabTwo = LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
            TextView textView1 = tabTwo.findViewById(R.id.imgViewTabIcon);
            textView1.setText("B");
            ImageView imgViewTabIcon1 = tabTwo.findViewById(R.id.imgViewTabIcon);
            imgViewTabIcon1.setImageResource(R.drawable.img_22);
            tabLayout.getTabAt(1).setCustomView(tabTwo);


            View tabThree = LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
            TextView textView2 = tabThree.findViewById(R.id.tab);
            textView2.setText("C");
            ImageView imgViewTabIcon2 = tabThree.findViewById(R.id.imgViewTabIcon);
            imgViewTabIcon2.setImageResource(R.drawable.img_33);
            tabLayout.getTabAt(2).setCustomView(tabThree);

            View tabFour = LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
            TextView textView3 = tabFour.findViewById(R.id.tab);
            textView3.setText("D");
            ImageView imgViewTabIcon3 = tabFour.findViewById(R.id.imgViewTabIcon);
            imgViewTabIcon3.setImageResource(R.drawable.img_33);
            tabLayout.getTabAt(3).setCustomView(tabFour);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private void createViewPager(ViewPager viewPager) {
        adapter = new ViewPagerAdapter(getSupportFragmentManager(),HomeActivity.this);
        adapter.addFrag(new A(), "A");
        adapter.addFrag(new B(), "B");
        adapter.addFrag(new C(), "C");
        adapter.addFrag(new D(), "D");
        viewPager.setAdapter(adapter);
    }
}

“自定义PagerAdapter”

public class ViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();
    private Context mContext;

    public ViewPagerAdapter(FragmentManager manager, Context context) {
        super(manager);
        mContext=context;
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    public void addFrag(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }


    public int dpToPx(int dp) {
        DisplayMetrics displayMetrics = mContext.getResources().getDisplayMetrics();
        return Math.round(dp * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
    }

  public void SetOnSelectView(TabLayout tabLayout, int position)
    {

        TabLayout.Tab tab = tabLayout.getTabAt(position);
        View selected = tab.getCustomView();
        TextView textView = (TextView) selected.findViewById(R.id.tab);

        textView.setTextColor(mContext.getResources().getColor(R.color.colorPrimary));

        ImageView imgViewTabIcon = selected.findViewById(R.id.imgViewTabIcon);

        int height = dpToPx(35);

        if(textView.getText().toString().equals("tab1")){
            BitmapDrawable bitmapdraw = (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.img_1);
            Bitmap b = bitmapdraw.getBitmap();
            Bitmap smallMarker = Bitmap.createScaledBitmap(b, height, height, false);
            imgViewTabIcon.setImageBitmap(smallMarker);
        }else if(textView.getText().toString().equals("tab2")){
            BitmapDrawable bitmapdraw = (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.img_2);
            Bitmap b = bitmapdraw.getBitmap();
            Bitmap smallMarker = Bitmap.createScaledBitmap(b, height, height, false);
            imgViewTabIcon.setImageBitmap(smallMarker);
        }else if(textView.getText().toString().equals("tab3")){
            BitmapDrawable bitmapdraw = (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.img_3);
            Bitmap b = bitmapdraw.getBitmap();
            Bitmap smallMarker = Bitmap.createScaledBitmap(b, height, height, false);
            imgViewTabIcon.setImageBitmap(smallMarker);
        }
        else {
            BitmapDrawable bitmapdraw = (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.img_4);
            Bitmap b = bitmapdraw.getBitmap();
            Bitmap smallMarker = Bitmap.createScaledBitmap(b, height, height, false);
            imgViewTabIcon.setImageBitmap(smallMarker);
        }
    }

    public void SetUnSelectView(TabLayout tabLayout,int position) {

        TabLayout.Tab tab = tabLayout.getTabAt(position);
        View selected = tab.getCustomView();
        TextView textView = (TextView) selected.findViewById(R.id.tab);

        textView.setTextColor(mContext.getResources().getColor(R.color.white));

        ImageView imgViewTabIcon = selected.findViewById(R.id.imgViewTabIcon);

        if(textView.getText().toString().equals("tab1")){
            imgViewTabIcon.setImageResource(R.drawable.img_11);
        }else if(textView.getText().toString().equals("tab2")){
            imgViewTabIcon.setImageResource(R.drawable.img_22);
        }else if(textView.getText().toString().equals("tab3")){
            imgViewTabIcon.setImageResource(R.drawable.img_33);
        }else{
            imgViewTabIcon.setImageResource(R.drawable.img_44);
        }

    }
    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }


    @Override
    public int getItemPosition(Object object){
        return ViewPagerAdapter.POSITION_NONE;
    }

}

“标签的自定义布局”

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/custom_tab_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:background="@drawable/tab_color_selector"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center">

        <ImageView
            android:paddingTop="5dp"
            android:paddingBottom="5dp"
            android:paddingLeft="2dp"
            android:paddingRight="2dp"
            android:id="@+id/imgViewTabIcon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@drawable/img_11" />

        <TextView
            android:id="@+id/tab"        
            android:paddingTop="5dp"
            android:paddingBottom="5dp"
            android:paddingLeft="2dp"
            android:paddingRight="2dp"
            android:padding="5dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="A"
            android:textColor="#FFFFFF"
            android:layout_gravity="center"
            android:textSize="12sp"
            android:textStyle="bold" />
    </LinearLayout>
</LinearLayout>

“MainActivity XML文件”

<?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:background="@drawable/bg"
    android:orientation="vertical"
    tools:context=".activity.MainActivity">


    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="35dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="5dp"
        app:tabBackground="@drawable/tab_color_selector"
        app:tabGravity="fill"
        app:tabIndicatorColor="@color/gps_btn"
        app:tabMode="fixed"
        app:tabSelectedTextColor="#ffffff"
        app:tabTextColor="#ffffff" />

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</LinearLayout>