Recyclerview animation not working in activity - android-studio

i have an activity that contains 2 fragments
StepsFragment contains a Recyclerview but the Recyclerview's animation is not working
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="400dp"
android:background="#B3D1DB"
tools:context=".StepsFragment">
<TextView
android:id="#+id/inputEntred"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:visibility="invisible" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/RV"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:layoutAnimation="#anim/layou_animation_fall_down"
android:paddingTop="10dp"
android:paddingBottom="30dp" />
</RelativeLayout>
layou-animation-fall-down
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
android:animation="#anim/fall_down"
android:animationOrder="normal"
android:delay="15%">
fall-down
<set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="6000">
<translate
android:fromYDelta="-20%"
android:interpolator="#android:anim/accelerate_decelerate_interpolator"
android:toYDelta="0"
android:duration="1000" />
<alpha
android:fromAlpha="0"
android:interpolator="#android:anim/accelerate_decelerate_interpolator"
android:toAlpha="3"
android:duration="1000"
/>
<scale
android:fromXScale="105%"
android:fromYScale="105%"
android:interpolator="#android:anim/accelerate_decelerate_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="100%"
android:toYScale="100%" android:duration="1000"
/>
</set>
activity layout
<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=".Sorting_activity">
<RelativeLayout
android:id="#+id/triLayout"
android:layout_width="0dp"
android:layout_height="300dp"
app:layout_constraintBottom_toTopOf="#+id/stepsLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0">
</RelativeLayout>
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:id="#+id/stepsLayout"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/triLayout"></androidx.coordinatorlayout.widget.CoordinatorLayout>
StepsFragment
public class StepsFragment extends Fragment {
private OnFragmentInteractionListener mListener;
private static TextView inputEntred ;
private static RecyclerView RV ;
private Handler mhandler = new Handler();
public StepsFragment() {
// Required empty public constructor
}
public void setTextView(String textentered){
inputEntred.setText(textentered);
String[] numberList = inputEntred.getText().toString().split(",");
final Integer[] numbers = new Integer[numberList.length];
SelectionSort m1 = new SelectionSort();
ArrayList<String> mystepsList = m1.steps(numbers);// returns the steps numbers
final StepListAdapter adapter = new StepListAdapter() ;
RV.setAdapter(adapter);
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
RV.setLayoutManager(llm);
adapter.setList(mystepsList);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view =inflater.inflate(R.layout.fragment_steps, container, false);
inputEntred =view.findViewById(R.id.inputEntred);
RV =view.findViewById(R.id.RV);
return view;
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
// mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}}

Could you try this? I have to remove unnecessary duration in the XML. Thanks
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="250">
<translate
android:fromYDelta="-20%"
android:toYDelta="0"
android:interpolator="#android:anim/decelerate_interpolator"
/>
<alpha
android:fromAlpha="0"
android:toAlpha="1"
android:interpolator="#android:anim/decelerate_interpolator"
/>
<scale
android:fromXScale="105%"
android:fromYScale="105%"
android:toXScale="100%"
android:toYScale="100%"
android:pivotX="50%"
android:pivotY="50%"
android:interpolator="#android:anim/decelerate_interpolator"
/>
</set>

Related

android studio navigtion not woring when movieng with fragments in java

so i wanted to creat a footer navigtion on the main activity and a fragment view above it
but now when i try to use the normal navigtion with the navigtion grafh it just crashes.
mainActivity java code:
package com.example.mywatchlist;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button homeBtn = findViewById(R.id.homeBtn);
Button profileBtn = findViewById(R.id.profileBtn);
Button socialBtn = findViewById(R.id.theSocialSearchBtn);
Button contentBtn = findViewById(R.id.contentContentSearchBtn);
homeBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
homeFragment home = new homeFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainerView,home).commit();
}
});
profileBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
userProfileFragment profile = new userProfileFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainerView,profile).commit();
}
});
socialBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
socialSearchFragment social = new socialSearchFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainerView,social).commit();
}
});
contentBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
contentSearchFragment contentSearch = new contentSearchFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainerView,contentSearch).commit();
}
});
}
}
main activity.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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.fragment.app.FragmentContainerView
android:id="#+id/fragmentContainerView"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="632dp"
app:defaultNavHost="true"
app:navGraph="#navigation/navigation" />
<LinearLayout
android:id="#+id/footer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="#+id/homeBtn"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Go to\nHome"
android:textSize="12dp" />
<Button
android:id="#+id/profileBtn"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="My\nProfile"
android:textSize="12dp" />
<Button
android:id="#+id/theSocialSearchBtn"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Search for\nPeople"
android:textSize="12dp" />
<Button
android:id="#+id/contentContentSearchBtn"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Search for\nContent"
android:textSize="12dp" />
</LinearLayout>
</LinearLayout>
and now it crashes when i try to move in this fragment:
fragment to move from java code:
package com.example.mywatchlist;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.navigation.Navigation;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
/**
* A simple {#link Fragment} subclass.
* Use the {#link contentSearchFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class contentSearchFragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
public contentSearchFragment() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* #param param1 Parameter 1.
* #param param2 Parameter 2.
* #return A new instance of fragment contentSearchFragment.
*/
// TODO: Rename and change types and number of parameters
public static contentSearchFragment newInstance(String param1, String param2) {
contentSearchFragment fragment = new contentSearchFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_content_search,container, false);
// Inflate the layout for this fragment
Button searchBtn = view.findViewById(R.id.contentContentSearchBtn);
searchBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Navigation.findNavController(view).navigate(R.id.action_contentSearchFragment_to_contentFragment);
}
});
return view;
}
}
and this is the xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".contentSearchFragment" >
<TextView
android:id="#+id/contentSearchTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#C4FF80"
android:gravity="center"
android:text="Content search"
android:textSize="30dp" />
<Button
android:id="#+id/contentSearchAddNewContent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add new content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFEB3B"
android:orientation="horizontal">
<EditText
android:id="#+id/contentSearchBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#C8C8C8"
android:ems="10"
android:hint="Search for content"
android:inputType="textPersonName"
android:minHeight="48dp" />
<Button
android:id="#+id/contentContentSearchBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Search" />
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="502dp">
<TextView
android:id="#+id/contentSearchData"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="test\ntest\ntest\ntest\ntest\ntest\XXXXXXXXXXXXXXXXXXntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\nXXXXXXXXXXXXXXXXX test\ntest\nXXXXXXXXXXXXXXXX\ntest\ntest\ntest\ntest\ntesttest\ntest\ntest\ntest\ntest\ntest\XXXXXXXXXXXXXXXXXXntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\nXXXXXXXXXXXXXXXXX test\ntest\nXXXXXXXXXXXXXXXX\ntest\ntest\ntest\ntest\ntesttest\ntest\ntest\ntest\ntest\ntest\XXXXXXXXXXXXXXXXXXntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\nXXXXXXXXXXXXXXXXX test\ntest\nXXXXXXXXXXXXXXXX\ntest\ntest\ntest\ntest\ntesttest\ntest\ntest\ntest\ntest\ntest\XXXXXXXXXXXXXXXXXXntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\nXXXXXXXXXXXXXXXXX test\ntest\nXXXXXXXXXXXXXXXX\ntest\ntest\ntest\ntest\ntesttest\ntest\ntest\ntest\ntest\ntest\XXXXXXXXXXXXXXXXXXntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\nXXXXXXXXXXXXXXXXX test\ntest\nXXXXXXXXXXXXXXXX\ntest\ntest\ntest\ntest\ntesttest\ntest\ntest\ntest\ntest\ntest\XXXXXXXXXXXXXXXXXXntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\nXXXXXXXXXXXXXXXXX test\ntest\nXXXXXXXXXXXXXXXX\ntest\ntest\ntest\ntest\ntesttest\ntest\ntest\ntest\ntest\ntest\XXXXXXXXXXXXXXXXXXntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\nXXXXXXXXXXXXXXXXX test\ntest\nXXXXXXXXXXXXXXXX\ntest\ntest\ntest\ntest\ntesttest\ntest\ntest\ntest\ntest\ntest\XXXXXXXXXXXXXXXXXXntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\nXXXXXXXXXXXXXXXXX test\ntest\nXXXXXXXXXXXXXXXX\ntest\ntest\ntest\ntest\ntest" />
</ScrollView>
</LinearLayout>
and this is the navigtion xml:
<?xml version="1.0" encoding="utf-8"?>
<navigation 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:id="#+id/navigation"
app:startDestination="#id/startFragment">
<fragment
android:id="#+id/startFragment"
android:name="com.example.mywatchlist.startFragment"
android:label="fragment_start"
tools:layout="#layout/fragment_start" >
<action
android:id="#+id/action_startFragment_to_logInFragment"
app:destination="#id/logInFragment" />
<action
android:id="#+id/action_startFragment_to_registerFragment"
app:destination="#id/registerFragment" />
</fragment>
<fragment
android:id="#+id/logInFragment"
android:name="com.example.mywatchlist.logInFragment"
android:label="fragment_log_in"
tools:layout="#layout/fragment_log_in" >
<action
android:id="#+id/action_logInFragment_to_homeFragment"
app:destination="#id/homeFragment" />
</fragment>
<fragment
android:id="#+id/registerFragment"
android:name="com.example.mywatchlist.registerFragment"
android:label="fragment_register"
tools:layout="#layout/fragment_register" >
<action
android:id="#+id/action_registerFragment_to_homeFragment"
app:destination="#id/homeFragment" />
</fragment>
<fragment
android:id="#+id/homeFragment"
android:name="com.example.mywatchlist.homeFragment"
android:label="fragment_home"
tools:layout="#layout/fragment_home" />
<fragment
android:id="#+id/userProfileFragment"
android:name="com.example.mywatchlist.userProfileFragment"
android:label="fragment_user_profile"
tools:layout="#layout/fragment_user_profile" />
<fragment
android:id="#+id/contentFragment"
android:name="com.example.mywatchlist.contentFragment"
android:label="fragment_content"
tools:layout="#layout/fragment_content" />
<fragment
android:id="#+id/contentSearchFragment"
android:name="com.example.mywatchlist.contentSearchFragment"
android:label="fragment_content_search"
tools:layout="#layout/fragment_content_search" >
<action
android:id="#+id/action_contentSearchFragment_to_contentFragment"
app:destination="#id/contentFragment" />
</fragment>
<fragment
android:id="#+id/socialSearchFragment"
android:name="com.example.mywatchlist.socialSearchFragment"
android:label="fragment_social_search"
tools:layout="#layout/fragment_social_search" >
<action
android:id="#+id/action_socialSearchFragment_to_userProfileFragment"
app:destination="#id/userProfileFragment" />
</fragment>
</navigation>
crashing:
--------- beginning of crash
2022-07-01 14:32:49.986 25363-25363/com.example.mywatchlist E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mywatchlist, PID: 25363
java.lang.IllegalStateException: View com.google.android.material.button.MaterialButton{3bedc11 VFED..C.. ...P.... 921,5-1440,173 #7f080237 app:id/contentContentSearchBtn} does not have a NavController set
at androidx.navigation.Navigation.findNavController(Navigation.kt:71)
at com.example.mywatchlist.contentSearchFragment$1.onClick(contentSearchFragment.java:73)
at android.view.View.performClick(View.java:7448)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1194)
at android.view.View.performClickInternal(View.java:7425)
at android.view.View.access$3600(View.java:810)
at android.view.View$PerformClick.run(View.java:28305)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
2022-07-01 14:32:50.027 25363-25363/com.example.mywatchlist I/Process: Sending signal. PID: 25363 SIG: 9
thank you to all the helpers
can you please share the crashing error? It's hard to figure out the error. If you can also add how to reproduce the crash it can be very helpful
You forgot to initialize NavHostFragent
so kindly Initiliaze In your OnCreate
NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager()
.findFragmentById(R.id. fragmentContainerView);
NavController navController = navHostFragment.getNavController();
and for the navigate one fragment to another fragment use navigate
homeBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {navController.navigate(R.id. action_startFragment_to_registerFragment
}
});)

Two recyclerview not scrollable in one layout

I created a View that has two recyclerviews inside.
Everything works correctly but I get that the two recyclerviews are scrollable.
I would like the two recyclerview to be shown in its maximum length to show all lines and for the scrolling of the view to work directly
if I set android: nestedScrollingEnabled = "false" the recyclerview is not shown totally long
what do i have to change?
Thanks
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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="wrap_content"
android:fillViewport="true"
android:background="#color/fond"
tools:context=".dossier.FicheDossier">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="115dp"
android:layout_gravity="center|top"
android:gravity="center|top">
<TextView
android:id="#+id/txtNomUsageDossierFragment"
android:layout_width="match_parent"
android:layout_height="86dp"
android:background="#drawable/fond_jaune_top"
android:fontFamily="#font/montserrat_bold"
android:gravity="center_horizontal"
android:onClick="backList"
android:paddingTop="21dp"
android:text="Dossiers"
android:textColor="#color/white"
android:textSize="18sp"
android:textStyle="normal" />
<ImageView
android:id="#+id/btn_Back"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="23dp"
android:layout_marginTop="23dp"
app:srcCompat="#drawable/icone_arrow_left_blanc" />
<EditText
android:id="#+id/txtFindDocument"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="24dp"
android:layout_marginTop="62dp"
android:layout_marginRight="24dp"
android:layout_marginBottom="4dp"
android:height="48dp"
android:background="#drawable/textedit_blanc_rounded_corners"
android:drawableLeft="#drawable/icone_recherche_jaune"
android:drawablePadding="6dp"
android:elevation="4dp"
android:ems="10"
android:hint="#string/txt_input_recherche"
android:inputType="text"
android:maxLines="1"
android:outlineSpotShadowColor="#color/jauneSombre"
android:paddingLeft="10dp"
android:scrollHorizontally="true"
android:textStyle="italic" />
<ProgressBar
android:id="#+id/progressBarTopListDocument"
style="?android:attr/progressBarStyle"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_alignParentRight="true"
android:layout_marginTop="23dp"
android:layout_marginRight="23dp"
android:theme="#style/colorProgressBarTop"
/>
</RelativeLayout>
<TextView
android:id="#+id/msgListDocument"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginTop="32dp"
android:layout_marginBottom="2dp"
android:fontFamily="#font/montserrat"
android:text="#string/txt_msg_dossiers"
android:textColor="#color/neutre"
android:textStyle="bold"
android:visibility="gone" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/list_classeurs_dossier"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginTop="12dp"
android:layout_marginRight="24dp"
android:divider="#drawable/rectangle_border_bottom"
android:dividerHeight="1dp"
android:isScrollContainer="false"
android:nestedScrollingEnabled="true" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/list_docs_dossier"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:divider="#drawable/rectangle_border_bottom"
android:dividerHeight="1dp"
android:isScrollContainer="false"
android:nestedScrollingEnabled="false" />
</LinearLayout>
You have to use only one RecyclerView and use the type to manage which card you want to recall
public class RecyclerViewAdapterClasseursDocs extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private final List<Document> documents;
private final List<Classeur> classeurs;
private final Context context;
public RecyclerViewAdapterClasseursDocs(List<Classeur> classeurs, List<Document> documents, Context context) {
this.classeurs = classeurs;
this.context = context;
this.documents = documents;
}
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
if (viewType == 0) {
return new ViewHolderClasseur(LayoutInflater.from(context.getApplicationContext()).inflate(R.layout.classeur, parent, false));
}
return new ViewHolderDoc(LayoutInflater.from(context.getApplicationContext()).inflate(R.layout.docs, parent, false));
}
#Override
public int getItemViewType(int position) {
if (position < this.classeurs.size()){
return 0;
} else {
return 1;
}
}
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder holder, int position) {
//Doc
if (holder.getItemViewType() == 0) {
//Classeur
ViewHolderClasseur c = (ViewHolderClasseur) holder;
final Classeur classeur = classeurs.get(position);
c.titleClasseur.setText(classeur.getNom());
} else {
ViewHolderDoc d = (ViewHolderDoc) holder;
final Document document = documents.get(position - classeurs.size());
d.titleDocument.setText(document.getIntitule());
}
}
#Override
public int getItemCount() {
return classeurs.size()+documents.size();
}
public static class ViewHolderClasseur extends RecyclerView.ViewHolder {
private final TextView titleClasseur;
private final View linearLayoutClasseur;
public ViewHolderClasseur(#NonNull View itemView) {
super(itemView);
linearLayoutClasseur = itemView.findViewById(R.id.rowClasseur);
titleClasseur = itemView.findViewById(R.id.txtClasseurTitle);
}
}
public static class ViewHolderDoc extends RecyclerView.ViewHolder {
private final TextView titleDocument;
public ViewHolderDoc(#NonNull View itemView) {
super(itemView);
itemView.findViewById(R.id.rowDocument);
titleDocument = itemView.findViewById(R.id.txtDocTitle);
itemView.findViewById(R.id.txtDocData);
}
}
}

After starting another Activity and inflating new Layout, Layout of MainActivity still visible

I want to use a Navigation Drawer within my App by using activities instead of fragments. By using Layout inflater in the different activities I tried to keep the same navigation drawer as in the MainActivity, but to change the inner layout. My problem is that somehow the layout of the MainActivity cant be replaced. When clicking on a item in the navigation drawer the new Activtiy opens and starts the layoutinflater. But the Layout Of the Main Activity remains, while the layout of the other Activity is just added.
public class MainActivity extends AppCompatActivity implements
TabLayout.OnTabSelectedListener{
public static String TAG = "MainActivity";
DrawerLayout drawerLayout;
ActionBarDrawerToggle actionBarDrawerToggle;
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "CREATED");
NavigationView navigationView = (NavigationView)
findViewById(R.id.navigation_view);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, ^
toolbar, R.string.drawer_open, R.string.drawer_closed);
drawerLayout.setDrawerListener(actionBarDrawerToggle);
navigationView.setNavigationItemSelectedListener(new
NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.Mensa_in_der_Nähe:
Intent anIntent = new Intent(getApplicationContext(),
LocationActivity.class);
startActivity(anIntent);
drawerLayout.closeDrawers();
break;
}
return false;
}
});
Then the xml file of main activity:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:focusableInTouchMode="true">
<android.support.design.widget.CoordinatorLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/overview_coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/content_frame">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="enterAlways|scroll"
app:popupTheme="#style/AppTheme.PopupOverlay" />
<android.support.design.widget.TabLayout
android:id="#+id/menu_tabs"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:tabMode="fixed" />
<android.support.design.widget.TabLayout
android:id="#+id/canteen_tabs"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:tabMode="scrollable" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/dish_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="visible"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
</android.support.v4.view.ViewPager>
</FrameLayout>
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:layout_marginTop="?attr/actionBarSize"
android:background="#android:color/white"
app:menu="#menu/drawer_menu" />
the other activity:
public class LocationActivity extends MainActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FrameLayout contentFrameLayout = (FrameLayout)
findViewById(R.id.content_frame);
getLayoutInflater().inflate(R.layout.activity_location,
contentFrameLayout);
}}
And the xml file of the other activity:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TimePicker
android:id="#+id/timePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</FrameLayout>

Android - Recycler View Focuses Wrong Holder on EditText click

I have a Recycler View to show a list of data and have edit text inside of Recycler View to allow user edit some number.
My app also resizing activity when the keyboard is shown/hidden.
The app works great unless I want to edit items which are placed at the bottom. When I click to edit text, it resizes the screen, shows keyboard, focuses holder but wrong one: usually one before and sometimes 2.
I tried to track issue, and understand that LinearLayoutMnager's onFocusSearchFailed function returns the wrong view.
Any idea why could it be?
Adapter
private final LinearLayoutManager linearLayoutManager;
public Observable<double[][]> observable;
private ObservableEmitter<double[][]> observe;
List<Invoice> invoices = Collections.emptyList();
double[][] amounts;
Context context;
public AdjustInvoiceAdapter(List<Invoice> list, Context context, LinearLayoutManager linearLayoutManager) {
this.invoices = list;
this.context = context;
this.linearLayoutManager = linearLayoutManager;
amounts = new double[invoices.size()][1];
for (int i = 0; i < list.size(); i++) {
amounts[i][0] = invoices.get(i).getAmountDue();
}
observable = Observable.create(new ObservableOnSubscribe<double[][]> (){
#Override
public void subscribe(ObservableEmitter<double[][]> emitter) throws Exception {
observe = emitter;
}
});
}
#Override
public InvoiceHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_adjust_invoice_payment_amount, parent, false);
InvoiceHolder holder = new InvoiceHolder(view);
return holder;
}
#Override
public void onBindViewHolder(InvoiceHolder holder, final int position) {
holder.update(invoices.get(position), amounts[position][0]);
holder.amount.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
if (s.length() == 0 || (s.length() == 1 && (s.toString().charAt(0)) == '-'))
amounts[position][0] = 0.0;
else
amounts[position][0] = Double.parseDouble(s.toString());
observe.onNext(amounts);
}
});
}
#Override
public int getItemViewType(int position) {
return position;
}
#Override
public int getItemCount() {
return invoices.size();
}
Holder
public class InvoiceHolder extends RecyclerView.ViewHolder {
EditText amount;
TextView invoiceAmountDue;
TextView invoiceId;
TextView invoiceDueDate;
TextInputLayout textInputLayout;
private NumberFormat numberFormat = NumberFormat.getCurrencyInstance(Locale.US);
public InvoiceHolder(View itemView) {
super(itemView);
invoiceId = (TextView) itemView.findViewById(R.id.invoice_number_text_view);
invoiceDueDate = (TextView) itemView.findViewById(R.id.invoice_due_text_view);
invoiceAmountDue = (TextView) itemView.findViewById(R.id.invoice_amount_due_text_view);
amount = (EditText) itemView.findViewById(R.id.editText);
textInputLayout = (TextInputLayout) itemView.findViewById(R.id.text_input_layout);
}
void update(Invoice invoice, double currentAmount) {
textInputLayout.setHint("(" + numberFormat.format(invoice.getAmountDue())+")");
invoiceId.setText(invoice.getinvoiceId());
invoiceDueDate.setText(invoice.getInvoiceDueDate());
amount.setText(currentAmount);
}
}
Layout (RecyclerView doesn't have any attribute)
<?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="wrap_content"
android:focusableInTouchMode="true"
android:focusable="true"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground"
>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:gravity="center_vertical">
<LinearLayout
android:id="#+id/linearLayout4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="4dp"
android:orientation="vertical"
android:paddingLeft="#dimen/activity_main_margin"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.5">
<TextView
android:id="#+id/invoice_number_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="1"
android:text="TextView"
android:textColor="#android:color/black"
android:textSize="18sp"
tools:layout_editor_absoluteX="7dp"
tools:layout_editor_absoluteY="0dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal">
<ImageView
android:id="#+id/red_warning_image_view"
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_marginRight="5dp"
android:backgroundTint="#color/ebiz_red"
android:tint="#color/ebiz_red"
android:visibility="gone"
app:srcCompat="#drawable/ic_error_black_24dp" />
<TextView
android:id="#+id/invoice_due_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:maxLines="1"
android:text="TextView"
android:textColor="#color/ebiz_gray_medium"
android:textSize="12sp"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="0dp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusableInTouchMode="true"
android:layout_marginBottom="8dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintLeft_toRightOf="#+id/linearLayout4"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.511">
<android.support.design.widget.TextInputLayout
android:id="#+id/text_input_layout"
android:layout_width="100dp"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="143dp"
tools:layout_editor_absoluteY="0dp">
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="numberDecimal"
android:text="$100"
android:focusableInTouchMode="true"
android:textColor="#android:color/black"
android:textSize="18sp" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
</android.support.constraint.ConstraintLayout>

Android Scrollview and layout problems

I have a question regarding android layouts. I cannot achieve what I am looking for and I need some direction.
I am looking for a layout that will be inside a Scroll View layout. its hard to explain but a picture is worth thousand words.enter image description here
I want this inside a scroll view, can any help please?
If you do not want to add images dynamically than this is the layout you want Try this.
activity_main.xml :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:id="#+id/sv"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/linMain"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<RelativeLayout
android:id="#+id/lin1"
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_margin="10dp">
<ImageView
android:id="#+id/imgView1"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#android:color/holo_purple" />
<ImageView
android:id="#+id/imgView2"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_below="#id/imgView1"
android:layout_marginTop="20dp"
android:background="#android:color/holo_red_light" />
<ImageView
android:id="#+id/imgView3"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_below="#id/imgView2"
android:layout_marginTop="20dp"
android:background="#android:color/holo_green_light" />
<ImageView
android:id="#+id/imgView4"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_below="#id/imgView3"
android:layout_marginTop="20dp"
android:background="#android:color/holo_blue_light" />
</RelativeLayout>
<LinearLayout
android:id="#+id/lin2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:orientation="vertical">
<WebView
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="#id/linImages"></WebView>
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
Here i am attaching screenshot of this layout UI.
Try this,I have made two layouts ..
1) activity_main that contains gridview and webview.
2)activity_gridview which contains imageview which will inflate in gridview.
I have added images dynamically to Gridview using BaseAdapter.
In MainActivity i have set that Adapter to GridView. Try this code once.
activity_main.xml :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:id="#+id/scrollView"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:fillViewport="false"
android:orientation="vertical">
<LinearLayout
android:id="#+id/linImages"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/grid"
android:layout_width="100dp"
android:layout_height="match_parent"
android:numColumns="1"></GridView>
<WebView
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="#id/linImages"></WebView>
</LinearLayout>
</ScrollView>
</RelativeLayout>
activity_gridview.xml :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/imgView1"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#drawable/star2" />
<ImageView
android:id="#+id/imgView2"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#drawable/star1" />
<ImageView
android:id="#+id/imgView3"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#drawable/star3" />
<ImageView
android:id="#+id/imgView4"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#drawable/star1" />
</LinearLayout>
CustomAdapter.java :
public class CustomAdapter extends BaseAdapter {
Context context;
int flags[];
LayoutInflater inflter;
public CustomAdapter(Context applicationContext, int[] flags) {
this.context = applicationContext;
this.flags = flags;
inflter = (LayoutInflater.from(applicationContext));
}
#Override
public int getCount() {
return flags.length;
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = inflter.inflate(R.layout.activity_gridview, null);
ImageView imgView1 = (ImageView) view.findViewById(R.id.imgView1);
ImageView imgView2 = (ImageView) view.findViewById(R.id.imgView2);
ImageView imgView3 = (ImageView) view.findViewById(R.id.imgView3);
ImageView imgView4 = (ImageView) view.findViewById(R.id.imgView4);
imgView1.setImageResource(flags[0]);
imgView2.setImageResource(flags[1]);
imgView3.setImageResource(flags[2]);
imgView4.setImageResource(flags[3]);
return view;
}
}
MainActivity.java :
public class MainActivity extends AppCompatActivity {
int images[] = {R.drawable.star2, R.drawable.star1, R.drawable.star3, R.drawable.star1};
GridView grid;
CustomAdapter customAdapter;
WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView= (WebView) findViewById(R.id.webView);
grid = (GridView) findViewById(R.id.grid);
customAdapter = new CustomAdapter(getApplicationContext(), images);
grid.setAdapter(customAdapter);
}
}

Resources