In my project i have a recycler view which contains a text view in every row. when i select a text view in a row text views in some random rows are also selected and also whatever is typed into it, it is also showing in those rows.Here is my code.
onBindViewHolder
public void onBindViewHolder(final MainContentViewHolder holder, int position) {
Log.e("Sony","onBindViewHolder");
Item currentItem = items.get(position);
if (currentItem.imageURL != null) {
imageLoader.get(currentItem.imageURL, new ImageLoader.ImageListener() {
#Override
public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
Log.e("Sony","onResponseImage");
holder.itemImage.setImageBitmap(response.getBitmap());
}
#Override
public void onErrorResponse(VolleyError error) {
Log.e("Sony","onErrorImage");
holder.itemImage.setImageResource(R.drawable.default_product);
}
});
}
//holder.itemImage.setImageBitmap(BitmapFactory.decodeStream((InputStream) new URL().getContent());
holder.itemPrice.setText(currentItem.price + "");
holder.itemName.setText(currentItem.itemName);
}
ViewHolder
public MainContentViewHolder(View itemView) {
super(itemView);
itemImage = (ImageView) itemView.findViewById(R.id.imgItemPic);
itemName = (TextView) itemView.findViewById(R.id.lblItemName);
itemPrice = (TextView) itemView.findViewById(R.id.lblItemPrice);
txtQty = (EditText) itemView.findViewById(R.id.txtQty);
btnAddToCart = (Button) itemView.findViewById(R.id.btnAddToCart);
btnAddToCart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Item item = items.get(getPosition());
CartMessageHandler.showToastMessage(context, item.itemName + " : " + item.price, Toast.LENGTH_LONG);
}
});
}
activity onCreate
mainListView = (RecyclerView) findViewById(R.id.recyclerList);
mainListView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
recyclerViewListAdapter = new MainContentRecyclerAdapter(this);
mainListView.setAdapter(recyclerViewListAdapter);
getData("fruits%20&%20vegetables");
mainListView.setLayoutManager(new LinearLayoutManager(this));
Custom Row for recyclerView
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/cardItemHolderRoot"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imgItemPic"
android:layout_width="#dimen/itemImageDimen"
android:layout_height="#dimen/itemImageDimen"
android:layout_centerVertical="true"
android:contentDescription="#string/productImage"
android:src="#drawable/default_product"
android:elevation="2dp"
android:focusableInTouchMode="false"
android:layout_marginTop="#dimen/loginWidgetTopMargin" />
<LinearLayout
android:layout_toRightOf="#+id/imgItemPic"
android:paddingLeft="10dp"
android:id="#+id/cardItemHolder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/lblItemName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/loginWidgetTopMargin"
android:text="Item Name"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/lblItemPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/loginWidgetTopMargin"
android:text="Rs.250"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/primaryColor" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/loginWidgetTopMargin"
android:orientation="horizontal">
<TextView
android:id="#+id/lblItemQty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="#string/quantity"
android:textAppearance="?android:attr/textAppearanceSmall" />
<EditText
android:id="#+id/txtQty"
android:layout_width="75dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
</LinearLayout>
<Button
android:id="#+id/btnAddToCart"
android:layout_width="wrap_content"
android:layout_marginLeft="5dp"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/cart_a16"
android:text="#string/addToCart" />
</LinearLayout>
what is wrong in my code? Can anyone suggest a way to fix this
RecyclerView reuses row views that was created earlier. That's why you see earlier typed text in random rows of your RecyclerView: you simply don't reset row view stuff state in onBindViewHolder.
So to avoid bug you should reset components state in onBindViewHolder: clear txtQty and etc. In case of expensive or heavyweight data documentation is recommended releasing this data with setRecyclerListener(..).
Related
I can't click on items in RecyclerView. For example, as shown in the code, I gave Tittle a click feature, but I can't click right now. In the same way I gave deleteimage a click feature, but I can't click. What do I need to do to make the items clickable? Where is the problem? I can't click on the items in RecyclerView, that's the problem.
XML Source Code:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:orientation="horizontal">
<ImageView
android:id="#+id/productImage"
android:layout_width="80dp"
android:layout_height="80dp"
android:src="#drawable/roomlocation" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:layout_marginTop="9dp"
android:text="productAdi"
android:textSize="19dp"
android:clickable="true"
android:textStyle="bold" />
<TextView
android:id="#+id/productDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:layout_marginTop="3dp"
android:text="Cihaza ait eklenmiş odalar"
android:textSize="14dp"
android:textStyle="italic" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:orientation="vertical">
<ImageView
android:id="#+id/deleteproduct"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="5dp"
android:clickable="true"
android:src="#drawable/ic_delete_yellow" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
tools:context="stechome.berkeylen.firebasedatabase.RoomsActivity">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar" />
<android.support.v7.widget.RecyclerView
android:id="#+id/rvRooms"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</android.support.constraint.ConstraintLayout>
RecyclerView Class Source:
public class RoomsAdapter extends RecyclerView.Adapter<RoomsAdapter.MyViewHolder> {
DatabaseReference ref = mDatabase.child("0").child("Rooms");
public class MyViewHolder extends RecyclerView.ViewHolder {
public MyViewHolder(View view) {
super(view);
productImage = (ImageView)
itemView.findViewById(R.id.productImage);
title = (TextView) view.findViewById(R.id.title);
deleteImage = (ImageView) view.findViewById(R.id.deleteproduct);
ref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()){
String data = snapshot.getValue(String.class);
Rooms.add(data);
title.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int position = 0;
position = getAdapterPosition();
if (getAdapterPosition() == position){
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference().child("0");
rootRef.child("titles").child("0").child("places").setValue(Rooms.get(position));
}
}
});
}
deleteImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (v == deleteproduct) {
deleteproduct(getLayoutPosition());
}
}
});
}
Set click listeners on recycler items by overriding onBindViewHolder method of adapter:
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.myTextView.setOnClickListener(...);
}
here you can find an example: https://developer.android.com/guide/topics/ui/layout/recyclerview
I had the same problem. After trying many things, the only one that worked was to add an atribute on the XML of the item layout:
onClick = the name of the method / function you want to trigger
I used a working RecyclerView as a template for another, but I made some sort of mistake in its implementation and I can't find it. Here is the code that initializes the view:
mMessageThreadList = new ArrayList<>();
mRecyclerView = findViewById(R.id.chat_list_recycler_view);
private void initRecyclerView() {
Log.d(TAG, "initRecyclerView Called");
//Initializes and sets up adapter
mAdapter = new ChatListRecyclerViewAdapter(mThreadList);
mRecyclerView.setAdapter(mAdapter);
Query query = mThreadCollection;
query.addSnapshotListener(new EventListener<QuerySnapshot>() {
#Override
public void onEvent(#Nullable QuerySnapshot queryDocumentSnapshots, #Nullable FirebaseFirestoreException e) {
for (DocumentChange documentChange : queryDocumentSnapshots.getDocumentChanges()) {
switch (documentChange.getType()) {
case ADDED:
Thread thread = documentChange.getDocument().toObject(Thread.class);
Log.d(TAG, "Last Message: " + thread.getLastMessage().getMessage());
mThreadList.add(thread);
mAdapter.notifyDataSetChanged();
}
}
}
});
}
Note that the print log statement in the SnapshotListener does contain a value when printed so the information is returning from Firebase, it's just not displaying. Which makes me think my problem is with my Adapter/Viewholder:
public class ChatListRecyclerViewAdapter extends RecyclerView.Adapter<ChatListRecyclerViewAdapter.ChatListViewHolder> {
private List<Thread> threadList;
ChatListRecyclerViewAdapter(List<Thread> threadList) {
this.threadList = threadList;
}
#NonNull
#Override
public ChatListViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_list_row, parent, false);
return new ChatListViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ChatListViewHolder holder, int position) {
// This method fills fields with data for each list item
Log.d(TAG, "onBindViewholder called");
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMM hh:mm a");
final Thread thread = threadList.get(position);
char initial = thread.getPartnerName().charAt(0);
char uppercaseInitial = Character.toUpperCase(initial);
Log.d(TAG, thread.getLastMessage() + " " + thread.getPartnerID());
holder.partnerName.setText(thread.getPartnerName());
holder.authorInitial.setText(uppercaseInitial);
holder.lastMessageText.setText(thread.getLastMessage().getMessage());
holder.lastMessageTime.setText(simpleDateFormat.format(thread.getLastMessage().getTimestamp()));
}
#Override
public int getItemCount() {
return threadList.size();
}
//Viewholder stores the information about the layout and content of each list item, and serves as a template for each item of a RecyclerView
public class ChatListViewHolder extends RecyclerView.ViewHolder {
TextView partnerName;
TextView authorInitial;
TextView lastMessageText;
TextView lastMessageTime;
LinearLayout listTextHolder;
public ChatListViewHolder(View itemView) {
super(itemView);
partnerName = itemView.findViewById(R.id.partner_name);
authorInitial = itemView.findViewById(R.id.partner_initial);
lastMessageText = itemView.findViewById(R.id.last_message);
lastMessageTime = itemView.findViewById(R.id.last_message_time);
listTextHolder = itemView.findViewById(R.id.list_text_holder);
}
}
}
chat_list_row.xml file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/single_thread_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:background="#color/colorPrimaryDark">
<TextView
android:id="#+id/partner_initial"
android:background="#drawable/chat_list_circle"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentStart="true"
android:gravity="center"
android:text="A"
android:textSize="36sp"
tools:ignore="HardcodedText" />
<TextView
android:id="#+id/last_message_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:padding="5dp"
android:fontFamily="#font/nunito_extralight"
android:text="#string/sample_time"
android:textColor="#android:color/darker_gray"
android:textSize="12sp"
android:typeface="normal" />
<LinearLayout
android:id="#+id/list_text_holder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="#id/partner_initial"
android:layout_centerVertical="true"
android:orientation="vertical">
<TextView
android:id="#+id/partner_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:layout_marginStart="5dp"
android:fontFamily="#font/nunito"
android:text="#string/sample_name"
android:textColor="#color/white"
android:textSize="14sp"
android:textStyle="bold"
android:typeface="sans" />
<TextView
android:id="#+id/last_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:fontFamily="#font/nunito_extralight"
android:gravity="start"
android:text="#string/sample_message"
android:textColor="#android:color/darker_gray"
android:textSize="12sp"
android:typeface="normal" />
</LinearLayout>
</RelativeLayout>
Edit: Layout of the Activity that RecyclerView appears in. I also edit above code to show where the variable is attached to the view:
<android.support.constraint.ConstraintLayout 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="fill_parent"
android:layout_height="fill_parent"
android:background="#color/colorPrimaryDark"
android:paddingLeft="0dp"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingRight="0dp"
android:paddingBottom="0dp"
tools:context="org.andrewedgar.theo.ChatListActivity">
<include
android:id="#+id/chat_list_toolbar"
layout="#layout/toolbar" />
<android.support.v7.widget.RecyclerView
android:id="#+id/chat_list_recycler_view"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/chat_list_toolbar" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/new_check_in_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:src="#drawable/ic_person_pin_circle_black_24dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</android.support.constraint.ConstraintLayout>
Seems like you have missed to add layout manager of RecyclerView. Would you please add this and try again
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
Your recyclerview is not showing any data because the list in adapter is still empty. After fetching data from firebase you are adding to mThreadList, which is still with activity and not adapter. To update the list in adapter you can add a method in adapter class and pass the list to adapter and notifyDataseChanged there.
Activity:
//after firebase task...
mAdapter.updateData(mThreadList);
Adapter: add new method
void updateData(List<Thread> threadList){
this.threadList = threadList;
notifyDatasetChanged(); //notify adapter that new list is added
}
How can I delete the space when I hide a CardView? I doing an app with android and Firebase and I show the information from firebase in cardviews, but when the info isn't correct the cardview must disappear. The point is that the cardview disappear, but still uses a space in the layout, I've tried the answers in
setVisibility(GONE) view becomes invisible but still occupies space But it doesn't work for me.
#Override
protected void onStart() {
super.onStart();
firebaseAuth.addAuthStateListener(firebaseAuthListener);
final FirebaseRecyclerAdapter<CursosDB, MainActivity.CursosPViewHolder> firebaseRecyclerAdapter =
new FirebaseRecyclerAdapter<CursosDB, MainActivity.CursosPViewHolder>(
CursosDB.class,
R.layout.design_row_cursos,
MainActivity.CursosPViewHolder.class,
myRef)
{
#Override
protected void populateViewHolder(MainActivity.CursosPViewHolder viewHolder, final CursosDB Cmodel, int position)
{
String estado = Cmodel.getSTATUS().toString();
if (estado.equals("OK")){
viewHolder.setPhotoURL(getApplicationContext(), Cmodel.getURI());
viewHolder.setTitle(Cmodel.getTITLE());
viewHolder.setCiudad(Cmodel.getPLACE());
viewHolder.setLevel(Cmodel.getLEVEL());
viewHolder.setDur(Cmodel.getDURACION());
viewHolder.setPrice(Cmodel.getCOSTO());
}else{
RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) viewHolder.mView.getLayoutParams();
layoutParams.setMargins(0,0,0,0);
viewHolder.mView.setLayoutParams(layoutParams);
viewHolder.mView.setVisibility(View.INVISIBLE);
viewHolder.mView.setVisibility(View.GONE);
//viewHolder.mView.setVisibility(View.GONE);
//Toast.makeText(getApplicationContext(), "Ningún curso aprobado aún", Toast.LENGTH_SHORT).show();
}
viewHolder.mView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Toast.makeText(getApplicationContext(), Cmodel.getUSERID(), Toast.LENGTH_LONG).show();
//Toast.makeText(getApplicationContext(), Cmodel.getTITLE(), Toast.LENGTH_SHORT).show();
//Toast.makeText(UsersList.this, user_key, Toast.LENGTH_LONG).show();
}
});
}
};
mCourses.setAdapter(firebaseRecyclerAdapter);
}
This is my layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/cardview_cursos"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:onClick="positionAction"
android:padding="4dp"
android:paddingBottom="4dp"
android:paddingEnd="4dp"
android:paddingStart="4dp"
android:paddingTop="4dp"
app:cardElevation="4dp">
<LinearLayout android:id="#+id/layout_to_hide"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/Cimg"
android:layout_width="match_parent"
android:layout_height="165dp"
android:scaleType="center"
app:srcCompat="#drawable/knowit_logo" />
<TextView
android:id="#+id/Ctitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/Cimg"
android:layout_marginLeft="14dp"
android:layout_marginStart="14dp"
android:paddingTop="8dp"
android:text="Titulo"
android:textStyle="bold" />
<TextView
android:id="#+id/Clugar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/Ctitle"
android:layout_alignStart="#+id/Ctitle"
android:layout_below="#+id/Ctitle"
android:layout_marginTop="10dp"
android:text="Lugar" />
<TextView
android:id="#+id/textView29"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/Clugar"
android:layout_alignStart="#+id/Clugar"
android:layout_below="#+id/Clugar"
android:layout_marginTop="10dp"
android:text="$" />
<TextView
android:id="#+id/Cprice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/textView29"
android:layout_marginBottom="4dp"
android:layout_toEndOf="#+id/textView29"
android:layout_toRightOf="#+id/textView29"
android:paddingLeft="5dp"
android:text="0.0" />
<TextView
android:id="#+id/Cdur"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/Clugar"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginEnd="126dp"
android:layout_marginRight="126dp"
android:text="Duración" />
<TextView
android:id="#+id/Cnivel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/Cprice"
android:layout_alignLeft="#+id/Cdur"
android:layout_alignStart="#+id/Cdur"
android:layout_marginBottom="0dp"
android:text="Nivel" />
</RelativeLayout>
</LinearLayout> </android.support.v7.widget.CardView>
This is the mView:
public static class CursosPViewHolder extends RecyclerView.ViewHolder {
View mView;
public CursosPViewHolder(final View itemView) {
super(itemView);
mView = itemView;
}
public void setTitle(String title){
TextView post_title = (TextView)mView.findViewById(R.id.Ctitle);
post_title.setText(title);
}
public void setCiudad (String ciudad){
TextView post_city = (TextView)mView.findViewById(R.id.Clugar);
post_city.setText(ciudad);
}
public void setPhotoURL(Context ctx, String pgotouserurl) {
ImageView post_image =(ImageView)mView.findViewById(R.id.Cimg);
Picasso.with(ctx).load(pgotouserurl).into(post_image);
} }
I hope you can help me.
First the XML code you've posted ils wrong closed,(must finish with a CardView tag)
On addition be sure you're pointing the CardView layout with mView field ans Only use GONE or INVISIBLE,
INVISIBLE change your Panel ( Root view ) structure but GONE don't
I have more items to show in the cardView horizontally inside a recyclerView and I have more cards veritically.
I tried to place cardView inside a HorizontalScrollView, It worked to scroll the idividual card. I wanted to scroll the entire RecyclerView to be scrolled to see right end items.
I tried with the RecyclerView inside a HorizontalScroolView not worked. RecyclerView inside a NestedScrollView not worked.
The RecyclerView is in a fragment. Inside the viewPager tabLayout, this is one of the fragment
fragment 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"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<android.support.v7.widget.RecyclerView
android:id="#+id/record_recycler"
android:layout_width="550dp"
android:scrollbars="horizontal"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
Adapter:
public class TestAdapter extends RecyclerView.Adapter{
private List<Model> items;
Context context;
public TestAdapter(Context con, List<Model> itemslist) {
context=con;
this.items = itemslist;
}
#Override
public int getItemViewType(int position) {
return items.get(position).getUnique();
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if(viewType==0)
return new MyViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.cricketrank_record_valuecard, parent, false));
else
return new MyViewHolder1(LayoutInflater.from(parent.getContext()).inflate(R.layout.cricketrank_record_titlecard, parent, false));
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof MyViewHolder)
try{
((MyViewHolder)holder).bindViewHolder(position);
}catch (Exception e){
Log.e(Constant.Tag,e.toString());
}
else if(holder instanceof MyViewHolder1)
try{
((MyViewHolder1)holder).bindViewHolder(position);
}catch (Exception e){
Log.e(Constant.Tag,e.toString());
}
}
#Override
public int getItemCount() {
return items.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder{
public TextView text,text1,text2,text3,text4,text5;
public View mCardView;
public MyViewHolder(View view) {
super(view);
text1 = (TextView) view.findViewById(R.id.text1);
text2 = (TextView) view.findViewById(R.id.text2);
text3 = (TextView) view.findViewById(R.id.text3);
text4 = (TextView) view.findViewById(R.id.text4);
}
public void bindViewHolder(int position) {
text1.setText(items.get(position).getTeam());
text2.setText(items.get(position).getRank());
text3.setText(items.get(position).getMatches());
text4.setText(items.get(position).getPoints());
}
}
}
public class MyViewHolder1 extends RecyclerView.ViewHolder{
public TextView text,text1,text2,text3,text4,text5;
public RelativeLayout rl;
public MyViewHolder1(View view) {
super(view);
text = (TextView) view.findViewById(R.id.text);
text1 = (TextView) view.findViewById(R.id.text1);
text2 = (TextView) view.findViewById(R.id.text2);
text3 = (TextView) view.findViewById(R.id.text3);
text4 = (TextView) view.findViewById(R.id.text4);
rl = (RelativeLayout) view.findViewById(R.id.rl);
}
public void bindViewHolder(int position) {
text1.setText(items.get(position).getTeam());
text2.setText(items.get(position).getRank());
text3.setText(items.get(position).getMatches());
text4.setText(items.get(position).getPoints());
}
}
}
MainActivity:
RecyclerView record_recycler= (RecyclerView) view.findViewById(R.id.record_recycler);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
record_recycler.setLayoutManager(mLayoutManager);
record_recycler.setItemAnimator(new DefaultItemAnimator());
TestAdapter adapter = new TestAdapter(getActivity(), list);
record_recycler.setAdapter(adapter);
I think you are trying to scroll the recyclerview in two direction. Vertical is usual scroll of recylerview and in horizontally, the scrolling view if it is not fit with the mentioned width. Right?
If you are trying this, then I think it is not possible to scroll like you think.
You can scroll the recycler view in both the directions with different views holding on them.
As you said the scrolling of cardview is possible, by default it won't have any scrolling properly normally. So it will scroll to show the card content completely to the user if they are not seeing completely and you set the scrolling view to scroll.
The possible way is, keep the views vertically with their field name and value. If you want to create more, then add one more row to accommodate the values down.
Have a look below:
<android.support.v7.widget.CardView
android:id="#+id/card"
android:layout_marginTop="2dp"
android:layout_marginBottom="4dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/layout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/layout1"
android:orientation="horizontal">
<TextView
android:text="One"
android:textSize="#dimen/font_itemsrow_13dp"
android:layout_weight=".11"
android:paddingLeft="3dp"
android:paddingRight="3dp"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<TextView
android:text="Two"
android:textSize="#dimen/font_itemsrow_13dp"
android:layout_weight=".11"
android:paddingRight="3dp"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<TextView
android:text="Three"
android:textSize="#dimen/font_itemsrow_13dp"
android:layout_weight=".24"
android:paddingRight="3dp"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<TextView
android:text="Four"
android:textSize="#dimen/font_itemsrow_13dp"
android:layout_weight=".11"
android:paddingRight="3dp"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<TextView
android:text="Five"
android:textSize="#dimen/font_itemsrow_13dp"
android:layout_weight=".17"
android:paddingRight="3dp"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<TextView
android:text="Six"
android:textSize="#dimen/font_itemsrow_13dp"
android:layout_weight=".26"
android:paddingRight="3dp"
android:layout_width="0dp"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="#+id/layout3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/padding_3dp"
android:layout_below="#+id/layout2"
android:orientation="horizontal">
<TextView
android:id="#+id/one"
android:textSize="#dimen/font_itemsrow_14dp"
android:textColor="#color/black"
android:layout_weight=".11"
android:paddingLeft="3dp"
android:paddingRight="3dp"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/two"
android:textSize="#dimen/font_itemsrow_14dp"
android:textColor="#color/black"
android:layout_weight=".11"
android:paddingRight="3dp"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/three"
android:textSize="#dimen/font_itemsrow_14dp"
android:textColor="#color/black"
android:layout_weight=".24"
android:paddingRight="3dp"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/four"
android:textSize="#dimen/font_itemsrow_14dp"
android:textColor="#color/black"
android:layout_weight=".11"
android:paddingRight="3dp"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/five
android:textSize="#dimen/font_itemsrow_14dp"
android:textColor="#color/black"
android:layout_weight=".17"
android:paddingRight="3dp"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/six"
android:textSize="#dimen/font_itemsrow_14dp"
android:textColor="#color/black"
android:layout_weight=".26"
android:paddingRight="3dp"
android:layout_width="0dp"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
....// Add extra title Row
</LinearLayout>
<LinearLayout
....// Add extra Values Row
</LinearLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
You can archive it with LayoutManager refrence, with 3 arguments, in your case, change MainActivity a little bit, it will look this:
RecyclerView.LayoutManager mLayoutManager
= new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
Like documentation says, second argument define orientation:
/**
* #param context Current context, will be used to access resources.
* #param orientation Layout orientation. Should be {#link #HORIZONTAL} or {#link
* #VERTICAL}.
* #param reverseLayout When set to true, layouts from end to start.
*/
On an Android application I am developing i got this situation.
In a Fragment there is a ListView that contains a list of customers (stored in instances of the Customer_InputOld class) using a customized ArrayAdapter (named ArrayAdapter_ClientiInput). The single row contains a ViewFlipper with two TextViews and seven nearly identical TextViews.
I put a ClickListner and a TouchListner in the Adapter, so on the click (and the touch) of a line the application make something. This works, except for the fact that these events are not triggered on the whole row.
Only the rough 80% of the row is "clickable", while the last textView does not respond to the listner.
Sincerely I don't know what code to post cause I have no idea where the problem could be: I tried to toast the View.getWidth() on the Click of the View itself, but actually the width is 1280 px, the entire device's width.
The single row is inflated with this customer_row.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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="5dp"
tools:context=".MyActivity">
<ViewFlipper
android:id="#+id/capitalViewFlipper"
android:layout_width="75dp"
android:layout_height="75dp">
<TextView
android:id="#+id/customerLogo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ABCDEF"
android:gravity="center"
android:text="D"
android:textColor="#fff"
android:textSize="28pt" />
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FBBB"
android:gravity="center"
android:src="#drawable/ic_selected_done" />
</ViewFlipper>
<TextView
android:id="#+id/ragioneSocialeViewList"
android:layout_width="400dp"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/capitalViewFlipper"
android:layout_gravity="center"
android:layout_toRightOf="#+id/capitalViewFlipper"
android:gravity="center_vertical"
android:padding="10dip"
android:singleLine="true"
android:text="Dummy Customer"
android:textColor="#000"
android:textSize="24dp" />
<TextView
android:id="#+id/canaleViewList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/capitalViewFlipper"
android:layout_alignLeft="#id/ragioneSocialeViewList"
android:layout_gravity="center"
android:layout_marginBottom="-7dp"
android:gravity="center"
android:padding="10dip"
android:text="Dummy type"
android:textColor="#000"
android:textSize="20dp" />
<TextView
android:id="#+id/sollecitiViewList"
android:layout_width="125dp"
android:layout_height="75dp"
android:layout_alignBottom="#+id/capitalViewFlipper"
android:layout_alignTop="#+id/capitalViewFlipper"
android:layout_gravity="center"
android:layout_toRightOf="#+id/ragioneSocialeViewList"
android:gravity="center"
android:padding="10dip"
android:text="2"
android:textColor="#000"
android:textSize="22dp" />
<TextView
android:id="#+id/tipoViewList"
android:layout_width="125dp"
android:layout_height="75dp"
android:layout_alignBottom="#+id/capitalViewFlipper"
android:layout_alignTop="#+id/capitalViewFlipper"
android:layout_toRightOf="#+id/sollecitiViewList"
android:gravity="center"
android:padding="10dip"
android:text="F/S"
android:textColor="#000"
android:textSize="22dp" />
<TextView
android:id="#+id/emailViewList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/telefonoViewList"
android:layout_alignRight="#+id/telefonoViewList"
android:layout_alignTop="#+id/capitalViewFlipper"
android:gravity="center"
android:padding="10dip"
android:text="dummymail#gmail.com"
android:textAlignment="center"
android:textColor="#000"
android:textSize="22dp" />
<TextView
android:id="#+id/telefonoViewList"
android:layout_width="350dp"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/capitalViewFlipper"
android:layout_gravity="center"
android:layout_marginBottom="-7dp"
android:layout_toRightOf="#+id/tipoViewList"
android:gravity="center"
android:padding="10dip"
android:text="tel. 035424344"
android:textColor="#000"
android:textSize="22dp" />
<TextView
android:id="#+id/ultimoOrdineViewList"
android:layout_width="match_parent"
android:layout_height="75dp"
android:layout_alignBottom="#+id/capitalViewFlipper"
android:layout_alignTop="#+id/capitalViewFlipper"
android:layout_gravity="center"
android:layout_toRightOf="#+id/emailViewList"
android:capitalize="words"
android:gravity="center_vertical|right"
android:padding="10dip"
android:singleLine="true"
android:text="21/12/2012"
android:textColor="#000"
android:textSize="22dp" />
</RelativeLayout>
While the Adapter Code is this:
public class ArrayAdapter_ClientiInput extends ArrayAdapter<CustomerInputOld> {
public EventLog.Event ImageTouched;
//public ListView customerListView;
private ArrayList<CustomerInputOld> list;
private Context context;
//this custom adapter receives an ArrayList of RowData objects.
//RowData is my class that represents the data for a single row and could be anything.
public ArrayAdapter_ClientiInput(Context context, int textViewResourceId, ArrayList<CustomerInputOld> rowDataList) {
//populate the local list with data.
super(context, textViewResourceId, rowDataList);
this.list = new ArrayList<CustomerInputOld>();
this.list.addAll(rowDataList);
this.context = context;
}
public View getView(final int position, View convertView, ViewGroup parent) {
//creating the ViewHolder we defined earlier.
ViewHolder_CustomerRow holder = new ViewHolder_CustomerRow();
//creating LayoutInflater for inflating the row layout.
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//inflating the row layout we defined earlier.
convertView = inflater.inflate(R.layout.customers_row, null);
//setting the views into the ViewHolder.
holder.title = (TextView) convertView.findViewById(R.id.ragioneSocialeViewList);
holder.capital = (TextView) convertView.findViewById(R.id.customerLogo);
holder.flipper = (ViewFlipper) convertView.findViewById(R.id.capitalViewFlipper);
//holder.line = (RelativeLayout) convertView.findViewById(R.id.customer_whole_row);
holder.canale = (TextView) convertView.findViewById(R.id.canaleViewList);
holder.solleciti = (TextView) convertView.findViewById(R.id.sollecitiViewList);
holder.email = (TextView) convertView.findViewById(R.id.emailViewList);
holder.tipo = (TextView) convertView.findViewById(R.id.tipoViewList);
holder.numero = (TextView) convertView.findViewById(R.id.telefonoViewList);
holder.ultimoOrdine = (TextView) convertView.findViewById(R.id.ultimoOrdineViewList);
//define an onClickListener for the CheckBox.
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Toast.makeText(getContext(),Integer.toString(v.getWidth()),Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getContext(), CustomerDetail.class);
intent.putExtra("name", list.get(position).getCompany());
getContext().startActivity(intent);
}
});
convertView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent e) {
if (e.getAction() == android.view.MotionEvent.ACTION_DOWN) {
v.setBackgroundColor(0xFAAA);
} else if ( e.getAction() == android.view.MotionEvent.ACTION_UP ||
e.getAction() == android.view.MotionEvent.ACTION_CANCEL) {
isSelected(position, v);
}
//Ritorno false per permettere anche al OnClickListener di agire..
return false;
}
});
holder.flipper.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CustomerListFragment.onRowIconClicked(position, (RelativeLayout) v.getParent(), (ViewFlipper) v, context);
}
});
//setting data into the the ViewHolder.
holder.title.setText(list.get(position).getCompany());
holder.capital.setText(list.get(position).getCompany().substring(0, 1).toUpperCase());
holder.capital.setBackgroundColor(list.get(position).getIconColor());
holder.canale.setText(list.get(position).getChannel());
holder.solleciti.setText(list.get(position).getReminders());
if (Integer.parseInt(list.get(position).getReminders()) >= 3)
holder.solleciti.setTextColor(0xffff0000);
holder.email.setText(list.get(position).getEmail());
holder.numero.setText("tel. " + list.get(position).number);
holder.ultimoOrdine.setText(new SimpleDateFormat("c d MMM ''yy").format(list.get(position).getLastOrderDate()));
if (list.get(position).getInvoiceCustomer() == null) {
if (list.get(position).getDeliveryCustomer() == null) {
holder.tipo.setText("F/S");
} else {
holder.tipo.setText("F");
}
} else {
holder.tipo.setText("S");
}
//modifiche nel caso in cui sia selezionato
if ( isSelected(position,convertView) ) {
holder.flipper.showNext();
}
//return the row view.
return convertView;
}
private boolean isSelected(int position, View v){
if (position == CustomerListFragment.customerSelected) {
v.setBackgroundColor(0xFFE6C86F);
return true;
} else {
v.setBackgroundColor(0xFFFFFFFF);
return false;
}
}
}
Solution by OP.
In the last TextView I put android:capitalize="words", and that is an EditText's attribute, not Textview's! So when I clicked on the last "20%" of screen - coincidentally, that TextBox- tha app thinks that the click was on the TextView and not the line!