I have a recyclerView I just want to disable the animation when an item is clicked
Any help is welcome
thanks in advance
You can disable it through recyclerView.setItemAnimator(null);
in your viewHolder class
public static class ViewHolder extends RecyclerView.ViewHolder {
View rootView;
public ViewHolder(View itemView) {
super(itemView);
rootView = itemView;
}
}
#Override
and then in onBindViewHolder
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.rootView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//overridden default onClick
}
});
}
Set the clickable property to true on your item's layout's root node.
Example item_layout.xml:
<LinearLayout
...
android:clickable="true"
...
>
...
</LinearLayout
I think you are talking about touch ripple effect.
To disable this effect, just set a background color to the root layout of your RecyclerView's item layout's XML.
For example:
// recyclerview_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_aaa"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff0000">
................
.......................
</RelativeLayout>
Hope this will help~
Related
I have a RecyclerView which I'm populating with items as described by the layout below.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/cv_item_display"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:weightSum="9">
.
.
.
</LinearLayout>
</android.support.v7.widget.CardView>
Whenever the CardView referenced by cv_item_display is clicked inside RecyclerView, I see ripple effect on Card BUT when I attach an OnClickListener to the card, I don't get ripple effect any more. Below is the code for ViewHolder where I attach OnClickListener.
public class RosterViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
TextView tvStudentCount, tvRosterName;
ImageView ivDelete, ivView;
CardView cvItemDisplay;
public RosterViewHolder(View itemView) {
super(itemView);
tvStudentCount = (TextView) itemView.findViewById(R.id.tv_question_student_count);
tvRosterName = (TextView) itemView.findViewById(R.id.tv_questionnaire_roster_name);
ivDelete = (ImageView) itemView.findViewById(R.id.iv_delete);
ivView = (ImageView) itemView.findViewById(R.id.iv_view);
cvItemDisplay = (CardView) itemView.findViewById(R.id.cv_item_display);
cvItemDisplay.setOnClickListener(this);
ivDelete.setOnClickListener(this);
ivView.setOnClickListener(this);
}
#Override
public void onClick(View view) {
final int position = getAdapterPosition();
switch (view.getId()) {
case R.id.iv_delete:
mRealm.executeTransaction(new Realm.Transaction() {
#Override
public void execute(Realm realm) {
mRealm.where(RosterEntry.class).equalTo("rosterId", mRealmResults.get(position).getId()).findAll().deleteAllFromRealm();
mRealmResults.deleteFromRealm(position);
}
});
break;
case R.id.iv_view:
Utils.startRosterItemsActivity(view.getContext(), mRealmResults.get(position).getName(), mRealmResults.get(position).getId(), false);
break;
case R.id.cv_item_display:
startAttendanceActivity(mRealmResults.get(position).getId());
break;
}
}
}
EDIT: I also identified some weird behavior - ripple effect shows in landscape mode but not in portrait mode.
I use same adapter for two different item layouts. One is for grid, one is for linear display. I'm passing layout id with itemLayout to adapter. However, I wasn't able to add data binding properly. Could you help me out, please?
Here is my adapter effort so far:
public class OyuncuAdapter extends RecyclerView.Adapter<OyuncuAdapter.ViewHolder> {
ArrayList<Oyuncu> oyuncuListesi;
Context context;
int itemLayout;
public OyuncuAdapter(ArrayList<Oyuncu> oyuncuListesi, Context context, int itemLayout) {
this.oyuncuListesi = oyuncuListesi;
this.context=context;
this.itemLayout = itemLayout;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(context).inflate(itemLayout, parent, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
Oyuncu oyuncu = oyuncuListesi.get(position);
holder.binding.setVariable(BR.oyuncu, oyuncu);
holder.binding.executePendingBindings();
}
#Override
public int getItemCount() {
return oyuncuListesi.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder{
private ViewDataBinding binding;
public ViewHolder(View itemView) {
super(itemView);
binding = DataBindingUtil.bind(itemView);
}
}
}
Here is the item layout:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="oyuncu"
type="com.figengungor.suits.model.Oyuncu" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:padding="#dimen/itemPadding"
tools:background="#color/darkgrey">
<ImageView
android:id="#+id/foto"
android:layout_width="150dp"
android:layout_height="150dp"
android:gravity="center"
android:scaleType="centerCrop"
app:imageResource="#{oyuncu.foto}"
tools:src="#drawable/gabrielmacht" />
<TextView
android:id="#+id/isim"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#android:color/white"
android:text="#{oyuncu.isim}"
tools:text="Gabriel Macht" />
</LinearLayout>
</layout>
The error I'm getting is not really helpful:
Caused by: java.lang.RuntimeException: view tag isn't correct on view:null
My bad. This part works perfectly. My problem is extending BaseActivity which also uses DataBinding. The crash is related to that. After removing extending BaseActivity from my activity, it worked. However, I opened another question realted to that. If you could look at that, I'll be grateful. DataBinding: How to use BaseActivity
My RecyclerView:
<android.support.v7.widget.RecyclerView
android:background="?android:selectableItemBackground"
app:layoutManager="android.support.v7.widget.LinearLayoutManager"
android:id="#+id/crash_course_item_dates"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:clickable="true"
/>
ClickListener:
holder.crashCoursesDateRecyclerView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
crashCourseListener.onCrashCourseClick(crashCourse);
}
});
However it's not working when I click on the recyclerview. I thought it might be that the clicklistener is being triggered by the items inside the recyclerview. So what I tried is setting clickable=false in the recyclerview Items' layout and settings the layout's onClickListener(null) but it't not working.
update
Thanks for all your answers, I should have explained better, i have a recyclerview inside another recyclerview item, each item of the latter has a recyclerview inside. I fixed this problem by putting the recyclerview in a frame layout and adding another layout 'on top' of the recyclervjew and setting the onClickListener on that layout.
As Recycler view doesnot support on itemClick Listener ,Implement a simplelistener interface and call that interface in the bindview and pass it to the main class.Please find the piece of code below for
public interface OnItemClickListener {
public void onItemClick(View view, int position);
}
Adapter Constructor
public ContentAdapter(List<ContentItem> items, OnItemClickListener listener) {
this.items = items;
this.listener = listener;
}
in Bind View Holder
holder.setOnClickListener(new View.OnClickListener() {
#Override public void onClick(View v) {
listener.onItemClick(item);
}
});
Implement your activity with the OnItemClickListener and in the callbacks add your required code.
You just simple call in your onBindViewHolder() method at where you have may be your own holder class.
So use holder.itemView.setOnClickListener(new setOn....){
At where itemView is the view which is inflated in onCreateViewHolder method.
As per my understanding you want click of complete Recycler view,
Try this,
RecyclerView rv = (RecyclerView) findViewById(R.id.recycler_view);
rv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//your action
}
});
first of all your question is not clear to me but still what i have understood u want you perform click listener on the each item of the try this
in your recyclerview's item layout
<LinearLayout
android:id="#+id/linearlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
><TextView
android:id="#+id/linearlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Demo"
android:clickable="false"
/>
</LinearLayout>
Now in item ItemHolder class
public class Demo extends ItemHolder {
LinearLayout ll;
public Demo(View view) {
ll=(LinearLayout)view.findViewById(R.id.linearlayout);
ll.setOnClickListener(View View){
}
I have a RecyclerView where the user has to click the elements to cast a vote to that specific player.
Current Layout
As it's not that much intuitive, I'd like to add Buttons on the right side of each element to let the user understand that he needs to click if he wants to cast a vote.
Question
How do I make a custom layout like that? Probably using a GridLayout? And mostly, how do I get the Button's element's position when the button (and it only) gets clicked?
Code
list_players.xml | http://pastebin.com/F7Ei07x9
two_line_list_item_horizontal.xml | http://pastebin.com/s8qvwqt4
CourseAdapter.java | http://pastebin.com/qZJeimfe
Replace your CoursesAdapter's onBindViewHolder with this.
#Override
public void onBindViewHolder(CoursesViewHolder holder, int position) {
Player player = mArrayCourses.get(position);
holder.name.setText(player.getName());
holder.counter.setText(String.valueOf(player.getCount()));
holder.voteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Do your work here
}
});
}
You can use LinearLayout to add button to your Recyclerview item by giving them weight.
You can handle onClicklistener of that button in ViewHolder class and you can get position of clicked button by getAdapterPosition() in ViewHolder class.
As per your request :
XML :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="5">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="ABCD"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="A"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Vote"/>
</LinearLayout>
Adapter :
public class Holder extends RecyclerView.ViewHolder{
Button btnVote;
public Holder(View itemView) {
super(itemView);
btnVote = (Button) itemView.findViewById(R.id.btn_vote);
btnVote.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//list.get(getAdapterPosition()); Use for get the data on selected item
}
});
}
}
I have implemented Horizontal RecyclerView for Images. Horizontal navigation work fine but unwanted space is drawn between two items of RecyclerView.
RecyclerView item layout-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/imgView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/ic_launcher" />
</LinearLayout>
My RecyclerView layout-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
RecyclerView Setup-
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
HlistViewAdapter adapter = new HlistViewAdapter(pArray);
recyclerView.setAdapter(adapter);
recyclerView.setItemAnimator(new DefaultItemAnimator());
My RecyclerView Adapter-
public class HlistViewAdapter extends RecyclerView.Adapter<HlistViewAdapter.ViewHolder>{
ArrayList<String> pathArray;
public HlistViewAdapter(ArrayList<String> pathArray){
this.pathArray = pathArray;
}
#Override
public HlistViewAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View itemLayoutView = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.hlist_item_layout, null);
// create ViewHolder
ViewHolder viewHolder = new ViewHolder(itemLayoutView);
return viewHolder;
}
#Override
public void onBindViewHolder(HlistViewAdapter.ViewHolder viewHolder, int i) {
viewHolder.imgViewIcon.setImageBitmap(BitmapFactory.decodeFile(pathArray.get(i)));
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public ImageView imgViewIcon;
public ViewHolder(View itemLayoutView) {
super(itemLayoutView);
imgViewIcon = (ImageView) itemLayoutView.findViewById(R.id.imgView);
}
}
#Override
public int getItemCount() {
return pathArray.size();
}
}
edited answer after chat with user
the problem was the size of the images being too different to the different android devices, and the impossibility to stretch them to prevent the empty spaces. given that, i recommend the use of a ViewPager instead of a RecyclerView, having that it will provide the same functionality buth with a better controll on how the imageView will react with different sized images.
http://codetheory.in/android-image-slideshow-using-viewpager-pageradapter/