I've been playing with bindings (in actions and fragments) until I managed to get it (partially) working. I am binding six variables (and they are properly shown in the application), but generated BR file has only three definitions (and only one of them corresponds to one of my variables)
My main layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</com.google.android.material.appbar.AppBarLayout>
<include layout="#layout/content_main" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
content main:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<fragment
android:id="#+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="#navigation/nav_graph" />
</androidx.constraintlayout.widget.ConstraintLayout>
The layout of the fragment:
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<data>
<variable
name="data"
type="Data" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".FragmentFloor0">
<TextView
android:id="#+id/label_floor0"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/margin_small"
android:layout_marginTop="#dimen/margin_small"
android:layout_marginEnd="#dimen/margin_small"
android:text="#string/label_floor0"
android:textSize="#dimen/textSizeRegular"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/imageGroundFloor"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="#dimen/margin_small"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/label_floor0"
app:layout_constraintVertical_bias="0.0"
app:srcCompat="#drawable/ground_floor_v2"
android:scaleType="centerInside"
android:contentDescription="#string/blueprint_0th_floor" />
<TextView
android:id="#+id/liviTempStr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#{data.fl0_liviTempStr}"
android:textSize="#dimen/textSizeRegular"
app:layout_constraintBottom_toBottomOf="#id/imageGroundFloor"
app:layout_constraintEnd_toEndOf="#id/imageGroundFloor"
app:layout_constraintHorizontal_bias="0.788"
app:layout_constraintStart_toStartOf="#id/imageGroundFloor"
app:layout_constraintTop_toTopOf="#id/imageGroundFloor"
app:layout_constraintVertical_bias="0.32" />
<TextView
android:id="#+id/liviTempTel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#{data.fl0_liviTempTel}"
android:textSize="#dimen/textSizeRegular"
app:layout_constraintBottom_toBottomOf="#id/imageGroundFloor"
app:layout_constraintEnd_toEndOf="#id/imageGroundFloor"
app:layout_constraintHorizontal_bias="0.95"
app:layout_constraintStart_toStartOf="#id/imageGroundFloor"
app:layout_constraintTop_toTopOf="#id/imageGroundFloor"
app:layout_constraintVertical_bias="0.65" />
<TextView
android:id="#+id/kitcTempSin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#{data.fl0_kitcTempSin}"
android:textSize="#dimen/textSizeRegular"
app:layout_constraintBottom_toBottomOf="#id/imageGroundFloor"
app:layout_constraintEnd_toEndOf="#id/imageGroundFloor"
app:layout_constraintHorizontal_bias="0.32"
app:layout_constraintStart_toStartOf="#id/imageGroundFloor"
app:layout_constraintTop_toTopOf="#id/imageGroundFloor"
app:layout_constraintVertical_bias="0.65" />
<TextView
android:id="#+id/bathTempSin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#{data.fl0_bathTempSin}"
android:textSize="#dimen/textSizeRegular"
app:layout_constraintBottom_toBottomOf="#id/imageGroundFloor"
app:layout_constraintEnd_toEndOf="#id/imageGroundFloor"
app:layout_constraintHorizontal_bias="0.95"
app:layout_constraintStart_toStartOf="#id/imageGroundFloor"
app:layout_constraintTop_toTopOf="#id/imageGroundFloor"
app:layout_constraintVertical_bias="0.8" />
<TextView
android:id="#+id/hallTempSin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#{data.fl0_hallTempEx1}"
android:textSize="#dimen/textSizeRegular"
app:layout_constraintBottom_toBottomOf="#id/imageGroundFloor"
app:layout_constraintEnd_toEndOf="#id/imageGroundFloor"
app:layout_constraintHorizontal_bias="0.62"
app:layout_constraintStart_toStartOf="#id/imageGroundFloor"
app:layout_constraintTop_toTopOf="#id/imageGroundFloor"
app:layout_constraintVertical_bias="0.39" />
<TextView
android:id="#+id/hallTempEx1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#{data.fl0_hallTempEx1}"
android:textSize="#dimen/textSizeRegular"
app:layout_constraintBottom_toBottomOf="#id/imageGroundFloor"
app:layout_constraintEnd_toEndOf="#id/imageGroundFloor"
android:rotation="90"
app:layout_constraintHorizontal_bias="0.65"
app:layout_constraintStart_toStartOf="#id/imageGroundFloor"
app:layout_constraintTop_toTopOf="#id/imageGroundFloor"
app:layout_constraintVertical_bias="0.55" />
<TextView
android:id="#+id/hallHummEx1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#{data.fl0_hallHummEx1}"
android:rotation="90"
android:textSize="#dimen/textSizeRegular"
app:layout_constraintBottom_toBottomOf="#id/imageGroundFloor"
app:layout_constraintEnd_toEndOf="#id/imageGroundFloor"
app:layout_constraintHorizontal_bias="0.65"
app:layout_constraintStart_toStartOf="#id/imageGroundFloor"
app:layout_constraintTop_toTopOf="#id/imageGroundFloor"
app:layout_constraintVertical_bias="0.65" />
<Button
android:id="#+id/button_floor0_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="#dimen/margin_small"
android:layout_marginBottom="#dimen/margin_small"
android:text="#string/button_next"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
Data binding class:
import androidx.databinding.BaseObservable;
import androidx.databinding.Bindable;
public class HomematicData extends BaseObservable {
private float fl0_liviTempStr;
private float fl0_liviTempTel;
private float fl0_kitcTempSin;
private float fl0_bathTempSin;
private float fl0_hallTempEx1;
private float fl0_hallHummEx1;
#Bindable
public float getFl0_liviTempStr() { return fl0_liviTempStr; }
public float getFl0_liviTempTel() { return fl0_liviTempTel; }
public float getFl0_kitcTempSin() { return fl0_kitcTempSin; }
public float getFl0_bathTempSin() { return fl0_bathTempSin; }
public float getFl0_hallTempEx1() { return fl0_hallTempEx1; }
public float getFl0_hallHummEx1() { return fl0_hallHummEx1; }
public void setFl0_liviTempTel(float fl0_liviTempTel) { this.fl0_liviTempTel = fl0_liviTempTel; /* notifyPropertyChanged(BR.fl0_liviTempTel); */ }
public void setFl0_liviTempStr(float fl0_liviTempStr) { this.fl0_liviTempStr = fl0_liviTempStr; /* notifyPropertyChanged(BR.fl0_liviTempStr); */ }
public void setFl0_kitcTempSin(float fl0_kitcTempSin) { this.fl0_kitcTempSin = fl0_kitcTempSin; /* notifyPropertyChanged(BR.fl0_kitcTempSin); */ }
public void setFl0_bathTempSin(float fl0_bathTempSin) { this.fl0_bathTempSin = fl0_bathTempSin; /* notifyPropertyChanged(BR.fl0_ballTempSin); */ }
public void setFl0_hallTempEx1(float fl0_hallTempEx1) { this.fl0_hallTempEx1 = fl0_hallTempEx1; /* notifyPropertyChanged(BR.fl0_hallTempEx1); */ }
public void setFl0_hallHummEx1(float fl0_hallHummEx1) { this.fl0_hallHummEx1 = fl0_hallHummEx1; /* notifyPropertyChanged(BR.fl0_hallTempEx1); */ }
Main activity:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<fragment
android:id="#+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="#navigation/nav_graph" />
</androidx.constraintlayout.widget.ConstraintLayout>
Fragment Activity:
public class FragmentFloor0 extends Fragment {
private FragmentFloor0Binding binding;
#Override
public View onCreateView(
LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState ) {
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_floor0, container, false);
View rootView = binding.getRoot();
HomematicData hd = new HomematicData();
hd.setFl0_liviTempStr(1.0f);
hd.setFl0_liviTempTel(2.0f);
hd.setFl0_bathTempSin(3.0f);
hd.setFl0_kitcTempSin(4.0f);
hd.setFl0_hallTempEx1(5.0f);
hd.setFl0_hallHummEx1(6.0f);
binding.setData(hd);
return rootView;
}
public void onViewCreated(#NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
view.findViewById(R.id.button_floor0_next).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
NavHostFragment.findNavController(FragmentFloor0.this)
.navigate(R.id.action_FragmentFloor0_to_FragmentFloor1);
}
});
}
}
Binding Adaptor:
public class BindingUtils {
#BindingAdapter("android:text")
public static void setFloat(TextView view, float value) {
if (Float.isNaN(value)) view.setText("");
else view.setText(String.format("%.1f", value));
}
#InverseBindingAdapter(attribute = "android:text")
public static float getFloat(TextView view) {
String num = view.getText().toString();
if(num.isEmpty()) return 0.0F;
try {
return Float.parseFloat(num);
} catch (NumberFormatException e) {
return 0.0F;
}
}
and generated BR:
package androidx.databinding.library.baseAdapters;
public class BR {
public static final int _all = 0;
public static final int data = 1;
public static final int fl0_liviTempStr = 2;
}
where are all other constants?!
I have clean-build and rebuild project, as well as invalidated the cache and rebuild the project.
It looks to me that #Bindable needs to be added before every getter
Related
This question already has answers here:
RecyclerView items don't fill width
(3 answers)
Recycler view showing single item
(8 answers)
Closed 2 years ago.
I need to create a Recyclerview. For that I have created an cardview model to be displayed in a recyclerview. Even though i have given match parent to the layouts, the width of cardview is not fitting the parent. Its width is showing approximately up to half of the screen(not matching parent) Do anyone can support?.please advise better way to do this.
Attached the xml code below
<?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">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
app:cardCornerRadius="15dp"
android:layout_height="wrap_content"
android:foreground="#drawable/border_creatives"
android:layout_marginHorizontal="6dp"
android:layout_marginVertical="4dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.AppCompatImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:id="#+id/dficon"
app:layout_constraintBottom_toTopOf="#id/guideline19"
app:layout_constraintStart_toStartOf="#id/guideline23"
app:layout_constraintTop_toBottomOf="#id/guideline18"
app:layout_constraintEnd_toStartOf="#id/guideline50"/>
<TextView
android:id="#+id/dname"
android:layout_width="0dp"
android:layout_height="0dp"
android:fontFamily="#font/amiko_semibold"
android:textColor="#color/black"
android:textSize="18sp"
app:layout_constraintBottom_toTopOf="#+id/guideline24"
app:layout_constraintEnd_toStartOf="#+id/ddownbutton"
app:layout_constraintStart_toStartOf="#+id/guideline50"
app:layout_constraintTop_toTopOf="#+id/guideline18" />
<TextView
android:id="#+id/ddate"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:fontFamily="#font/amiko_semibold"
android:textColor="#color/black"
android:textSize="13sp"
app:layout_constraintBottom_toTopOf="#+id/guideline19"
app:layout_constraintEnd_toStartOf="#id/barrier7"
app:layout_constraintStart_toStartOf="#id/guideline50"
app:layout_constraintTop_toTopOf="#+id/guideline24" />
<TextView
android:id="#+id/dfileextension"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="30dp"
android:fontFamily="#font/amiko_semibold"
android:textColor="#color/black"
android:textSize="13sp"
app:layout_constraintBottom_toTopOf="#+id/guideline19"
app:layout_constraintStart_toEndOf="#id/barrier7"
app:layout_constraintTop_toTopOf="#+id/guideline24" />
<ImageButton
android:id="#+id/ddownbutton"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#drawable/mins_customselector"
android:clickable="true"
android:src="#drawable/filedownload_icon"
app:layout_constraintBottom_toTopOf="#+id/guideline19"
app:layout_constraintEnd_toStartOf="#+id/guideline22"
app:layout_constraintTop_toTopOf="#+id/guideline18"
android:focusable="true" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline18"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.10" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline19"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.90" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline17"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.8275" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline22"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.97" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline23"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.045" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline24"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.57" />
<androidx.constraintlayout.widget.Barrier
android:id="#+id/barrier7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:barrierDirection="right"
app:constraint_referenced_ids="ddate" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline50"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.20" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
main layout having recyclerview
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".Download_Activity">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.textfield.TextInputLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="#+id/toolbar"
app:layout_constraintStart_toStartOf="#+id/guideline49"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="#id/toolbar">
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="start"
android:text="#string/downloads"
android:textColor="#FCFFFD"
android:textSize="30sp"
app:fontFamily="#font/carter_one"/>
</com.google.android.material.textfield.TextInputLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/drecyclerdownloads"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#id/toolbar" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline49"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.20" />
</androidx.constraintlayout.widget.ConstraintLayout>
Adapter class
public class DownloadsAdapter extends RecyclerView.Adapter<Downloadsviewholder> {
Download_Activity download_activity;
ArrayList<Downloadsmodel> downloadsmodels;
public DownloadsAdapter(Download_Activity download_activity, ArrayList<Downloadsmodel> downloadsmodels) {
this.download_activity = download_activity;
this.downloadsmodels = downloadsmodels;
}
#NonNull
#Override
public Downloadsviewholder onCreateViewHolder(#NonNull ViewGroup parent, int i) {
LayoutInflater layoutInflater = LayoutInflater.from(download_activity.getBaseContext());
View view = layoutInflater.inflate(R.layout.downloads_elements,null,false);
return new Downloadsviewholder(view);
}
#Override
public void onBindViewHolder(#NonNull final Downloadsviewholder downloadsviewholder, final int i) {
downloadsviewholder.dName.setText(downloadsmodels.get(i).getName());
downloadsviewholder.dUploaddate.setText(downloadsmodels.get(i).getUploaddate());
downloadsviewholder.dExtension.setText(downloadsmodels.get(i).getFileextension());
Picasso.get().load(downloadsmodels.get(i).getIcon()).into(downloadsviewholder.dIcon);
downloadsviewholder.dButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (ConnectionCheck.checkconnetion(download_activity.getBaseContext())) {
dowloadFile(downloadsviewholder.dName.getContext(), downloadsmodels.get(i).getName(), downloadsmodels.get(i).getFileextension()
, DIRECTORY_DOWNLOADS, downloadsmodels.get(i).getLink());
} else {
Toast.makeText(download_activity.getBaseContext(),"Check your internet connection", Toast.LENGTH_SHORT).show();
}
}
});
}
public void dowloadFile (Context context,String filename, String fileextension, String destinationdirectory, String url){
DownloadManager downloadManager=(DownloadManager) context.getSystemService(context.DOWNLOAD_SERVICE);
Uri uri=Uri.parse(url);
DownloadManager.Request request=new DownloadManager.Request(uri);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalFilesDir(context, destinationdirectory, filename+"."+fileextension);
downloadManager.enqueue(request);
}
#Override
public int getItemCount() {
return downloadsmodels.size();
}
}
Viewholder
public class Downloadsviewholder extends RecyclerView.ViewHolder {
TextView dName;TextView dLink;TextView dUploaddate; TextView dExtension;
ImageView dIcon;
ImageButton dButton;
public Downloadsviewholder(#NonNull View itemView) {
super(itemView);
dName = itemView.findViewById(R.id.dname);
dUploaddate=itemView.findViewById(R.id.ddate);
dExtension=itemView.findViewById(R.id.dfileextension);
dButton=itemView.findViewById(R.id.ddownbutton);
dIcon=itemView.findViewById(R.id.dficon);
}
}
I'm using the umano SlidingUpPanel library and I used an imagebutton in the panel but nothing happens when I click the imagebutton, I tried the imagebutton in another activity which worked perfectly. "next" is supposed to be printed to the logs when the button is pressed. I've searched all over but can't find any solution
Here's my code:
#SuppressLint("Registered")
public class NowPlaying extends Activity {
ImageButton play, pause, play_main, pause_main, next, imgbtn;
private MusicService musicSrv;
#SuppressLint("WrongViewCast")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.now_playing);
play = findViewById(R.id.play_button);
imgbtn = findViewById(R.id.imgbtn);
imgbtn.setOnClickListener(new View.OnClickListener() {
#SuppressLint("ShowToast")
#Override
public void onClick(View v) {
Log.i("oladapos", "next");
}
});
SlidingUpPanelLayout now_playing =
findViewById(R.id.slideup_nowplaying);
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
playNext();
}
});
}
//play next
private void playNext(){
musicSrv.playNext();
}
//play previous
private void playPrev(){
musicSrv.playPrev();
}
#Override
public void start() {
}
#Override
public void pause() {
}
#Override
public int getDuration() {
return 0;
}
#Override
public int getCurrentPosition() {
return 0;
}
#Override
public void seekTo(int pos) {
}
#Override
public boolean isPlaying() {
return false;
}
#Override
public int getBufferPercentage() {
return 0;
}
#Override
public boolean canPause() {
return false;
}
#Override
public boolean canSeekBackward() {
return false;
}
#Override
public boolean canSeekForward() {
return false;
}
#Override
public int getAudioSessionId() {
return 0;
}
}
xml:
<RelativeLayout 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"
android:id="#+id/slideup_nowplaying"
android:layout_height="match_parent"
android:layout_width="match_parent"
tools:context=".Fragments.songsFragment">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:layout_alignParentTop="true"
android:scaleType="centerCrop"
android:src="#drawable/songs_cover"
tools:ignore="ContentDescription" />
<LinearLayout
android:id="#+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="50dp"
android:clickable="true"
android:orientation="horizontal"
android:focusable="true">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/songs_cover_one"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="10dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:src="#drawable/songs_cover"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="ContentDescription" />
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="202dp"
android:layout_height="match_parent"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/linearLayout3"
app:layout_constraintStart_toEndOf="#+id/songs_cover_one"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/songs_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-condensed"
android:lines="1"
android:text="#string/havana_camila_cabello_song"
android:textColor="#FFFFFF"
android:textSize="22sp" />
<TextView
android:id="#+id/songs_artist_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:lines="1"
android:text="#string/camila_cabello" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout3"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:gravity="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.974"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:orientation="horizontal">
<Button
android:id="#+id/play_button"
android:layout_width="50dp"
android:layout_height="50dp"
android:clickable="true"
android:focusable="true"
android:scaleType="centerInside"
android:src="#drawable/round_play_arrow_black_48dp" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/forword_button"
android:id="#+id/imgbtn"/>
</RelativeLayout>
</RelativeLayout>
This question is been asked multiple times, but everyone has his specific usage of listview adapters (ArrayAdapter or BaseAdapter).
I searched a lot before asking this question, and this same problem happened to me before and i solved it using Holder class, and cleaning code to make it the minimum possible.
But now, i am creating another interface which contains a listview and needs a BaseAdapter to fill it using a List of Object (List).
The weird thing in my case is, if my listview contains only 4 items ( 4 rows ) then everything is ok, but if it contains more than 4, first items will be duplicated, and if i scroll down/up fast , items got wrong positions.
Here is my code :
public class OrdersAdapter extends BaseAdapter {
List<Order> ordersList;
FragmentActivity activity;
private int height;
public OrdersAdapter(FragmentActivity activity, List<Order> ordersList){
this.activity = activity;
this.ordersList = ordersList;
height = Constants.getOneThirdScreenHight(activity);
}
#Override
public int getCount() {
return ordersList.size();
}
#Override
public Order getItem(int position) {
return ordersList.get(position);
}
#Override
public long getItemId(int position) {
return ordersList.hashCode();
}
private class MyHolder{
TextView orderNumber, orderDate, menuCount, orderPrice, orderStatus, orderTelephone, orderStoreName;
ImageView orderDelete, menuPicture;
Order order, mOrder;
String numberText = activity.getResources().getString(R.string.order_number);
HashMap<String, String> map;
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();
}
#Override
public View getView (final int position, View convertView, ViewGroup parent) {
View vi;
final MyHolder holder;
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater)activity.getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
holder = new MyHolder();
vi = layoutInflater.inflate(R.layout.sent_order_item, null);
holder.order = getItem(position);
holder.mOrder = new Order();
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
layoutParams.height = height;
vi.findViewById(R.id.order_history_layout).setLayoutParams(layoutParams);
holder.orderNumber = (TextView) vi.findViewById(R.id.order_number);
holder.orderDate = (TextView) vi.findViewById(R.id.order_date);
holder.orderDelete = (ImageView) vi.findViewById(R.id.order_delete);
holder.orderPrice = (TextView) vi.findViewById(R.id.order_history_price);
holder.orderStatus = (TextView) vi.findViewById(R.id.order_history_status);
holder.orderStoreName = (TextView) vi.findViewById(R.id.order_history_store);
holder.orderTelephone = (TextView) vi.findViewById(R.id.order_history_phone);
holder.menuCount = (TextView) vi.findViewById(R.id.order_menu_count1) ;
holder.menuPicture = (ImageView) vi.findViewById(R.id.order_picture);
vi.setTag(holder);
}else{
vi = convertView;
holder = (MyHolder) vi.getTag();
}
//Getting Restaurants infos ( name and telephone ) from firebase
holder.mDatabase.child(Constants.STORES_PROFILES + "/" + holder.order.getOwner_uid() + "/" + holder.order.getStore_id())
.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(holder.map==null){
holder.map = (HashMap<String, String>)dataSnapshot.getValue();
}
if(dataSnapshot.getValue()!=null) {
holder.orderStoreName.setText(holder.map.get(Constants.NAME));
holder.orderTelephone.setText(holder.map.get(Constants.TELEPHONE));
}
//notifyDataSetChanged();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
//checking Order's status (received, validated, in progress, delivered)
holder.mDatabase.child(Constants.ORDERS + "/" + holder.order.getOwner_uid() + "/" + holder.order.getStore_id() + "/" + holder.order.getOrderId())
.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.getValue() != null) {
try {
holder.mOrder = dataSnapshot.getValue(Order.class);
if (!holder.order.getStatus().equals(holder.mOrder.getStatus())) {
holder.order.setStatus(holder.mOrder.getStatus());
//update status value for the user
holder.mDatabase.child(Constants.USERS+"/"+ Authentication.getCurrentUser().getUid()+"/"+Constants.SUBMITTED_ORDERS_KEY
+"/"+holder.order.getOrderId()+"/"+Constants.STATUS).setValue(holder.mOrder.getStatus());
//notifyDataSetChanged();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
displayOrder(holder);
return vi;
}
public void displayOrder(MyHolder holder){
holder.orderStatus.setText(holder.order.getStatus());
holder.orderNumber.setText(holder.numberText + holder.order.getOrderNumber());
holder.orderDate.setText(Utils.parseDateToDisplay((Long)holder.order.getCreated_at()));
holder.menuCount.setText(holder.order.getMyMealsList().get(0).getQuantity()+"");
holder.orderPrice.setText(holder.order.getTotal_price_formatted());
Glide.with(activity)
.load(holder.order.getMyMealsList().get(0).getMain_image())
.asBitmap()
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.into(holder.menuPicture);
if(OrdersContainerFragment.pDialog.isShowing()){
OrdersContainerFragment.pDialog.dismiss();
}
}
}
Here is my "xml" file :
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/order_history_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="100">
<LinearLayout
android:id="#+id/save_order_linear"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="25"
android:background="#fcf5ef"
android:orientation="horizontal"
android:weightSum="100">
<TextView
android:id="#+id/order_number"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="50"
android:gravity="center"
android:text="#string/order_number"
android:textColor="#color/colorPrimary"
android:textStyle="bold"
android:typeface="serif"/>
<TextView
android:id="#+id/order_date"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="50"
android:gravity="center"
android:text="00/00/0000"
android:textColor="#color/colorPrimary"
android:typeface="serif"/>
</LinearLayout>
<LinearLayout
android:id="#+id/save_order_pics"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_below="#+id/save_order_linear"
android:layout_marginLeft="5dp"
android:layout_marginTop="15dp"
android:layout_weight="74"
android:orientation="horizontal"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:weightSum="100">
<android.support.v7.widget.CardView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="33">
<ImageView
android:id="#+id/order_picture"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<ProgressBar
android:id="#+id/order_progressBar1"
style="#android:style/Widget.DeviceDefault.Light.ProgressBar.Large"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:indeterminate="true"
android:indeterminateDrawable="#drawable/my_progressbar"
android:padding="22dp"
android:visibility="gone"/>
<TextView
android:id="#+id/order_menu_count1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="false"
android:layout_alignParentEnd="false"
android:layout_alignParentRight="false"
android:gravity="bottom|right"
android:paddingBottom="15dp"
android:paddingRight="15dp"
android:text="ABCD"
android:textColor="#color/gray"
android:textSize="22dp"
android:typeface="serif"/>
</android.support.v7.widget.CardView>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="67"
android:orientation="vertical"
android:paddingLeft="15dp"
android:weightSum="100">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="25"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_alignParentTop="false"
android:layout_centerHorizontal="false"
android:layout_weight="1"
android:text="#string/order_price"
android:textColor="#color/colorPrimary"
android:typeface="monospace"/>
<TextView
android:id="#+id/order_history_price"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_alignParentTop="false"
android:layout_centerHorizontal="false"
android:layout_weight="1"
android:text="..."
android:textColor="#color/colorPrimary"
android:typeface="monospace"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="25"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:text="#string/order_status"
android:textColor="#color/colorPrimary"
android:typeface="monospace"/>
<TextView
android:id="#+id/order_history_status"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:text="..."
android:textColor="#color/colorAccent"
android:typeface="monospace"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="25"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:text="#string/Phone"
android:textColor="#color/colorPrimary"
android:typeface="monospace"/>
<TextView
android:id="#+id/order_history_phone"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:text="..."
android:textColor="#color/colorPrimary"
android:typeface="monospace"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="25"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:text="#string/order_store"
android:textColor="#color/colorPrimary"
android:typeface="monospace"/>
<TextView
android:id="#+id/order_history_store"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:text="..."
android:textColor="#color/colorPrimary"
android:typeface="monospace"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/save_order_pics"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:background="#fcf5ef"
android:orientation="horizontal">
</LinearLayout>
</LinearLayout>
I am using a TextView under android.support.v7.widget.CardView. However, whenever I try to set a long text, TextView is not giving the full result.
List<User> userList = new ArrayList<>();
userList.add(new User(R.mipmap.ic_launcher,
"Amit", "9988776655",
"amit#sonevalley.comamit#sonevalley.comamit#sonevalley.comamit#sonevalley.comamit#sonevalley.comamit#sonevalley.com"));
This is my java code(as an example) and this shows in the app like this:
for this first one 'Amit'.
How to solve this? If the text is long then it will automatically set it to next line.
Here is my full Cardview.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<android.support.v7.widget.CardView
android:id="#+id/cvSingleUser"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="12dp">
<ImageView
android:id="#+id/ivProfilePic"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginRight="16dp" />
<TextView
android:id="#+id/tvProfileName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_toRightOf="#id/ivProfilePic" />
<TextView
android:id="#+id/tvPhoneNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/tvProfileName"
android:layout_toRightOf="#id/ivProfilePic" />
<TextView
android:id="#+id/tvEmailId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="false"
android:scrollHorizontally="true"
android:layout_below="#id/tvPhoneNumber"
android:layout_toRightOf="#id/ivProfilePic" />
</RelativeLayout>
</android.support.v7.widget.CardView>
and this is the snapshot of the java code:Java Snap Shot
this is the java full code
public class AllUsersAdapter extends RecyclerView.Adapter<AllUsersAdapter.UserViewHolder>{
private List<MainActivity.User> userList;
private Context context;
public AllUsersAdapter(List<MainActivity.User> userList, Context context) {
this.userList = userList;
this.context = context;
}
#Override
public UserViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.single_cardview_layout, null);
UserViewHolder userViewHolder = new UserViewHolder(view);
return userViewHolder;
}
#Override
public void onBindViewHolder(UserViewHolder holder, int position) {
MainActivity.User user = userList.get(position);
String a=user.getEmailId().toString();
holder.tvProfileName.setText(user.getProfileName());
holder.tvPhoneNumber.setText(user.getPhoneNumber());
holder.tvEmailId.setText(a);
}
#Override
public int getItemCount() {
return userList.size();
}
public static class UserViewHolder extends RecyclerView.ViewHolder {
TextView tvProfileName;
TextView tvPhoneNumber;
TextView tvEmailId;
public UserViewHolder(View itemView) {
super(itemView);
tvProfileName = (TextView) itemView.findViewById(R.id.tvProfileName);
tvPhoneNumber = (TextView) itemView.findViewById(R.id.tvPhoneNumber);
tvEmailId = (TextView) itemView.findViewById(R.id.tvEmailId);
}
}
}
Thank you.
use this :
yourTextView.append("\n anotherTextPart")
First of all - if your string will not have white spaces TextView will not able to properly arange text in multi line.
Try to set following attributes :
<TextView
...
android:maxLines="4"/>
Update here is my test that works even without flag above:
MainActivity.java:
private class ViewHolder extends RecyclerView.ViewHolder {
public TextView email;
public ViewHolder(View itemView) {
super(itemView);
email = (TextView) itemView.findViewById(R.id.tvEmailId);
}
}
private class Adapter extends RecyclerView.Adapter<ViewHolder> {
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View card = LayoutInflater.from(parent.getContext()).inflate(R.layout.user_card, parent, false);
return new ViewHolder(card);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.email.setText("test#gmail.comtest#gmail.comtest#gmail.comtest#gmail.comtest#gmail.comtest#gmail.comtest#gmail.comtest#gmail.com");
}
#Override
public int getItemCount() {
return 5;
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
.....
RecyclerView view = (RecyclerView) findViewById(R.id.recyclerView);
view.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
view.setAdapter(new Adapter());
}
user_card.xml (changed layout_height to wrap_content):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp">
<android.support.v7.widget.CardView
android:id="#+id/cvSingleUser"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="12dp">
<ImageView
android:id="#+id/ivProfilePic"
android:layout_width="60dp"
android:src="#drawable/user"
android:layout_height="60dp"
android:layout_marginRight="16dp" />
<TextView
android:id="#+id/tvProfileName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="test test test"
android:layout_toRightOf="#id/ivProfilePic" />
<TextView
android:id="#+id/tvPhoneNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="test"
android:layout_below="#id/tvProfileName"
android:layout_toRightOf="#id/ivProfilePic" />
<TextView
android:id="#+id/tvEmailId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="false"
android:text="test#gmail.comtest#gmail.comtest#gmail.comvtest#gmail.com"
android:scrollHorizontally="true"
android:layout_below="#id/tvPhoneNumber"
android:layout_toRightOf="#id/ivProfilePic" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
main_activity :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
Result:
Solved by using
TableLayout
and using specific width.full code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp">
<android.support.v7.widget.CardView
android:id="#+id/cvSingleUser"
android:layout_marginRight="2dp"
android:layout_marginLeft="2dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tableLayout1"
android:layout_width="270dip"
android:layout_height="wrap_content">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:layout_gravity="center_horizontal">
<TextView
android:id="#+id/tvProfileName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="test test test"/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:layout_gravity="center_horizontal">
<TextView
android:id="#+id/tvPhoneNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10sp"
android:text="test"
android:layout_below="#id/tvProfileName" />
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
<TextView
android:id="#+id/tvEmailId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="test#gmail.comtest#gmail.comtest#gmail.comvtest#gmail.comtest#gmail.comtest#gmail.comtest#gmail.comvtest#gmail.comtest#gmail.comtest#gmail.comtest#gmail.comvtest#gmail.comtest#gmail.comtest#gmail.comtest#gmail.comvtest#gmail.com"
android:layout_below="#id/tvPhoneNumber" />
</TableRow>
</TableLayout>
</android.support.v7.widget.CardView>
I have very popular question - why the data not showing in the RecycleView.
That is my Adapter:
public class CommentsAdapter extends RecyclerView.Adapter<CommentsAdapter.ViewHolder> {
private static final String TAG = "CommentsAdapter";
public ImageView mThumbView;
private List<PostComment> postCommentList;
public CommentsAdapter(List<PostComment> postCommentList) {
this.postCommentList = postCommentList;
}
#Override
public CommentsAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.post_comment_item, parent, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(CommentsAdapter.ViewHolder holder, int position) {
PostComment record = postCommentList.get(position);
holder.mAuthorTextView.setText(record.getUser().getFull_name());
holder.mDateTextView.setText(record.getCreated_at());
holder.mTextTextView.setText(record.getText());
}
#Override
public int getItemCount() {
Log.i(TAG + " comments size", Integer.toString(postCommentList.size()));
return postCommentList.size();
}
public void addItems(List<PostComment> postCommentList) {
this.postCommentList.addAll(postCommentList);
}
public class ViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView mAuthorTextView;
public TextView mDateTextView;
public TextView mTextTextView;
public ViewHolder(View v) {
super(v);
mAuthorTextView = (TextView) itemView.findViewById(R.id.comment_author);
mDateTextView = (TextView) itemView.findViewById(R.id.comment_date);
mTextTextView = (TextView) itemView.findViewById(R.id.comment_text);
}
}
}
That is my view:
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/container"
....>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp">
<ImageView
android:id="#+id/comment_image"
android:layout_width="50dp"
android:layout_height="50dp"
android:scaleType="centerInside"
android:src="#drawable/cat"/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/middleLayout"
android:layout_toRightOf="#+id/comment_image"
android:layout_marginTop="8dp">
<TextView
android:id="#+id/comment_author"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:singleLine="true"
android:text="Dark Plastic"
android:textColor="?attr/colorPrimary"
android:textSize="14dp" />
<TextView
android:id="#+id/comment_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="8dp"
android:layout_toRightOf="#+id/comment_author"
android:text="21 minutes"
android:textSize="14dp" />
</RelativeLayout>
<TextView
android:id="#+id/comment_text"
android:layout_width="wrap_content"
android:layout_toRightOf="#+id/comment_image"
android:layout_height="wrap_content"
android:maxLines="3"
android:layout_marginTop="8dp"
android:layout_below="#id/middleLayout"
android:text="sd"
android:textColor="#android:color/black"
android:textSize="16dp" />
</RelativeLayout>
<View
android:id="#+id/divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_gravity="bottom"
android:paddingLeft="16dp"
android:paddingRight="16dp"></View>
That is how I set up the adapter:
mLayoutManager = new LinearLayoutManager(this);
mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
post_details_comments_list.setLayoutManager(mLayoutManager);
mCommentListAdapter = new CommentsAdapter(new ArrayList<PostComment>());
post_details_comments_list.setAdapter(mCommentListAdapter);
After I set up the adapter, I the getItemCount returns the quantity of the items - so that is not null.
RecycleView layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.gms.maps.MapView
android:id="#+id/post_details_mapview"
android:layout_width="wrap_content"
android:layout_height="200dp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/post_details_comments_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/TextLabelStyle"/>
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/post_details_comments_list"
android:layout_width="match_parent"
android:scrollbars="vertical"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/edit_comment"
android:hint="#string/hint_type_comment"/>
</LinearLayout>
I make the request to the server and here is the callback, where I get the results and set up the adapter:
#Override
public void onAllPostCommentsCallback(AllComments allComments) {
Log.i(TAG + " I've the comments", allComments.getPostCommentList().toString());
mCommentListAdapter.addItems(allComments.getPostCommentList());
post_details_comments_list.setAdapter(mCommentListAdapter);
mCommentListAdapter.notifyDataSetChanged();
}
But the list is not displaying.
What can be the reason?
put null inplace of parent in View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.post_comment_item, parent, false);
I've already solved the issue. The problem was that I had RecycleView inside the NestedScrollView
Answer in this post helped me a lot:
How to use RecyclerView inside NestedScrollView?