How to select and deselect each item in horizontal carousel in android?

房东的猫 提交于 2020-01-06 19:56:45

问题


I need to show one item as highlighted in horizontal carousel and when the user selected another item in the horizontal carousel i want to highlight that item(removing the earlier and highlight the currently selected). For that i am using the code in the activity page.

public void setemploye(){

    final DisplayMetrics displayMetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    final int imageWidth = (int) (displayMetrics.widthPixels / INITIAL_ITEMS_COUNT);
    Log.i("imagewidthhhh", String.valueOf(imageWidth));

    imageItem = new ImageView(this);
    imageItem.setLayoutParams(new LinearLayout.LayoutParams(150, 150));

    Log.i("genderrrrrrr",gender);

    if(gender.equals("MALE")){
        // Set the image view resource
        imageItem.setBackgroundResource(R.mipmap.avatar);
    }
    else{
        imageItem.setBackgroundResource(R.mipmap.woman);
    }

    Log.i("teammemmm iddcarod",team_member_id);

    mCarouselContainer.addView(imageItem);

    imageItem.setOnClickListener(new View.OnClickListener() {
        @Override
        // On click function
        public void onClick(View view) {

            TextView tvname = (TextView) findViewById(R.id.emp_name);
            tvname.setText("Selected Employee: " + firstname);
            //
            //socialMedia();
            Log.i("String firstname", String.valueOf(tvname));
            gettimeslots();

        }
    });


}

This is the code in xml

 <HorizontalScrollView
            android:id="@+id/horizontalscroll"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scrollbars="none"
            android:layout_below="@+id/employee"
            android:layout_centerHorizontal="true"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:fillViewport="false">

            <!-- Carousel items container - lays out items horizontally -->
            <LinearLayout android:id="@+id/carousel"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:orientation="horizontal">
            </LinearLayout>

        </HorizontalScrollView>

来源:https://stackoverflow.com/questions/37559984/how-to-select-and-deselect-each-item-in-horizontal-carousel-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!