问题
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