Translate

Tuesday, July 14, 2020

Code to display ViewMore for RecyclerView list and click on ViewMore will launch full list Android


RestaurantTabFragment.java:
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.cbrecodingtest.R;
import com.example.cbrecodingtest.adapter.RestaurantsAdapter;
import com.example.cbrecodingtest.model.RestaurantsModel;
import com.example.cbrecodingtest.utils.Utils;

import java.util.ArrayList;

public class RestaurantTabFragment extends Fragment {

private RecyclerView mRecyclerViewOpen , mRecyclerViewClosed;
private RecyclerView.Adapter mRecyclerAdapter;
private RelativeLayout mOpenLayout , mClosedLayout;
private ImageView mOpenArrow , mClosedArrow;
private TextView mViewMoreOpen , mViewMoreClosed;
private ArrayList<RestaurantsModel> openList ,closedList;
private View mLineView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.restaurant_tab_fragment, container, false);
initViewIds(rootView);

openList = (ArrayList<RestaurantsModel>) getArguments().getSerializable(Utils.RESTAURANT_LIST_KEY);
//openList = new ArrayList<>();
if(openList.size() == 0) {//open list is empty
//Hide the open section
mOpenLayout.setVisibility(View.GONE);
mLineView.setVisibility(View.GONE);
} else if(openList.size() > 4 ) {
// if list of items more than 4 rows, then display only 4 rows with ViewMore text ..
mOpenLayout.setVisibility(View.VISIBLE);
mViewMoreOpen.setVisibility(View.VISIBLE);

mRecyclerAdapter = new RestaurantsAdapter(openList.subList(0, 3), getActivity());
mRecyclerViewOpen.setAdapter(mRecyclerAdapter);

} else {
//Show open section and set adapter
mOpenLayout.setVisibility(View.VISIBLE);
mViewMoreOpen.setVisibility(View.GONE);
mRecyclerAdapter = new RestaurantsAdapter(openList, getActivity());
mRecyclerViewOpen.setAdapter(mRecyclerAdapter);
}


closedList = (ArrayList<RestaurantsModel>) getArguments().getSerializable(Utils.RESTAURANT_LIST_KEY);
//closedList = new ArrayList<>(); //For empty list..
if(closedList.size() == 0) {//closed list empty
//Hide the closed section
mClosedLayout.setVisibility(View.GONE);
} else if(closedList.size() > 4) {
// if list of items more than 4 rows, then display only 4 rows with ViewMore text ..
mClosedLayout.setVisibility(View.VISIBLE);
mViewMoreClosed.setVisibility(View.VISIBLE);
mRecyclerAdapter = new RestaurantsAdapter(closedList.subList(0, 3), getActivity());
mRecyclerViewClosed.setAdapter(mRecyclerAdapter);

} else {
//Show closed section and set Adapter
mClosedLayout.setVisibility(View.VISIBLE);
mViewMoreClosed.setVisibility(View.GONE);
mRecyclerAdapter = new RestaurantsAdapter(closedList, getActivity());
mRecyclerViewClosed.setAdapter(mRecyclerAdapter);
}


return rootView;
}

private void initViewIds(View rootView) {

mOpenLayout = rootView.findViewById(R.id.openLayout);
mLineView = rootView.findViewById(R.id.lineView);
mClosedLayout = rootView.findViewById(R.id.closedLayout);

mRecyclerViewOpen = rootView.findViewById(R.id.recycler_view_open);
mRecyclerViewClosed = rootView.findViewById(R.id.recycler_view_closed);

mOpenArrow = rootView.findViewById(R.id.open_arrow);
mOpenArrow.setOnClickListener(mOnClickListener);
mClosedArrow = rootView.findViewById(R.id.closed_arrow);
mClosedArrow.setOnClickListener(mOnClickListener);

mViewMoreOpen = rootView.findViewById(R.id.viewMoreOpen);
mViewMoreOpen.setOnClickListener(mOnClickListener);
mViewMoreClosed = rootView.findViewById(R.id.viewMoreClosed);
mViewMoreClosed.setOnClickListener(mOnClickListener);

mRecyclerViewOpen.setLayoutManager(new LinearLayoutManager(getActivity()));
mRecyclerViewClosed.setLayoutManager(new LinearLayoutManager(getActivity()));
}

private View.OnClickListener mOnClickListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.open_arrow:
if (mRecyclerViewOpen.getVisibility() == View.VISIBLE) {
// Recycler view visible
mRecyclerViewOpen.setVisibility(View.GONE);
mViewMoreOpen.setVisibility(View.GONE);
mOpenArrow.setBackgroundResource(R.drawable.arrow_down);
} else {
// Recycler view gone or invisible
mRecyclerViewOpen.setVisibility(View.VISIBLE);
mViewMoreOpen.setVisibility(View.VISIBLE);
mOpenArrow.setBackgroundResource(R.drawable.arrow_up);
}
break;

case R.id.closed_arrow:
if (mRecyclerViewClosed.getVisibility() == View.VISIBLE) {
// Recycler view visible
mRecyclerViewClosed.setVisibility(View.GONE);
mViewMoreClosed.setVisibility(View.GONE);
mClosedArrow.setBackgroundResource(R.drawable.arrow_down);
} else {
// Recycler view gone or invisible
mRecyclerViewClosed.setVisibility(View.VISIBLE);
mViewMoreClosed.setVisibility(View.VISIBLE);
mClosedArrow.setBackgroundResource(R.drawable.arrow_up);
}
break;

case R.id.viewMoreOpen:
//Hide closed list and display open list in full screen ..
/*mClosedLayout.setVisibility(View.GONE);
mViewMoreOpen.setVisibility(View.GONE);
mLineView.setVisibility(View.GONE);
mRecyclerAdapter = new RestaurantsAdapter(openList, getActivity());
mRecyclerViewOpen.setAdapter(mRecyclerAdapter);*/
launchFullFragment(openList , "open");
break;

case R.id.viewMoreClosed:
//Hide open list and display closed list in full screen ..
/* mOpenLayout.setVisibility(View.GONE);
mViewMoreClosed.setVisibility(View.GONE);
mLineView.setVisibility(View.GONE);
mRecyclerAdapter = new RestaurantsAdapter(closedList, getActivity());
mRecyclerViewClosed.setAdapter(mRecyclerAdapter);*/

launchFullFragment(closedList , "close");
break;
}
}
};


private void launchFullFragment(ArrayList<RestaurantsModel> list , String title) {
//code to launch fragment in fullscreen

Bundle bundle = new Bundle();
bundle.putSerializable("list", list);
bundle.putString("title", title);

ViewMoreFragment nextFrag= new ViewMoreFragment();
nextFrag.setArguments(bundle);
getActivity().getSupportFragmentManager().beginTransaction()
.replace(R.id.mainActivityView, nextFrag, "FragmentTag")
.addToBackStack(null)
.commit();
}
}


restaurant_tab_fragment.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/restTabView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white">

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<RelativeLayout
android:id="@+id/openLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp">

<TextView
android:id="@+id/openViewTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Open"
android:textAppearance="@style/TextAppearance.AppCompat.Headline"
android:textSize="18sp"
android:textStyle="bold" />

<ImageView
android:id="@+id/open_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginTop="10dp"
android:background="@drawable/arrow_up" />

<!-- A RecyclerView with some commonly used attributes -->
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view_open"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/openViewTitle"
android:layout_marginTop="10dp"
android:scrollbars="vertical" />

<TextView
android:id="@+id/viewMoreOpen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="View More"
android:textStyle="bold"
android:visibility="gone"
android:textColor="@color/red"
android:gravity="center_horizontal"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_below="@+id/recycler_view_open"
android:textAppearance="@style/TextAppearance.AppCompat.Headline"/>
</RelativeLayout>


<View
android:id="@+id/lineView"
android:layout_width="match_parent"
android:layout_height="8dp"
android:layout_marginTop="10dp"
android:background="#cecdcb" />


<RelativeLayout
android:id="@+id/closedLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp">

<TextView
android:id="@+id/closedViewTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Closed"
android:textAppearance="@style/TextAppearance.AppCompat.Headline"
android:textSize="18sp"
android:textStyle="bold" />

<ImageView
android:id="@+id/closed_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginTop="10dp"
android:background="@drawable/arrow_up" />

<!-- A RecyclerView with some commonly used attributes -->
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view_closed"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/closedViewTitle"
android:layout_marginTop="10dp"
android:scrollbars="vertical" />

<TextView
android:id="@+id/viewMoreClosed"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="View More"
android:textStyle="bold"
android:visibility="gone"
android:textColor="@color/red"
android:gravity="center_horizontal"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_below="@+id/recycler_view_closed"
android:textAppearance="@style/TextAppearance.AppCompat.Headline"/>
</RelativeLayout>


</LinearLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>


ViewMoreFragment.java:
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.cbrecodingtest.MainActivity;
import com.example.cbrecodingtest.R;
import com.example.cbrecodingtest.adapter.RestaurantsAdapter;
import com.example.cbrecodingtest.model.RestaurantsModel;

import java.util.ArrayList;

public class ViewMoreFragment extends Fragment{

private RecyclerView mRecyclerView;
ArrayList<RestaurantsModel> arrayList;
RecyclerView.LayoutManager mLayoutManager;
private RecyclerView.Adapter mRecyclerAdapter;
private Toolbar toolbar;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.view_more_fragment, container, false);

toolbar = rootView.findViewById(R.id.toolbar);

((MainActivity) getActivity()).setSupportActionBar(toolbar);

((MainActivity) getActivity()).getSupportActionBar().setDisplayShowHomeEnabled(true);
((MainActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// remove the back button
((MainActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(false);
((MainActivity) getActivity()).getSupportActionBar().setDisplayShowHomeEnabled(false);
getActivity().onBackPressed();
}
});

if(getArguments() != null) {
arrayList = (ArrayList<RestaurantsModel>)getArguments().getSerializable("list");
toolbar.setTitle(getArguments().getString("title"));
}

initViewIds(rootView);

return rootView;
}

private void initViewIds(View rootView) {
mLayoutManager = new LinearLayoutManager(getActivity());
mRecyclerView = rootView.findViewById(R.id.recycler_view);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerAdapter = new RestaurantsAdapter(arrayList, getActivity());
mRecyclerView.setAdapter(mRecyclerAdapter);
}
}

view_more_fragment.xml :
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@color/white">

<include
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="@layout/app_bar"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintLeft_toLeftOf="parent"/>

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/closedViewTitle"
android:layout_marginTop="10dp"
android:scrollbars="vertical"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/toolbar"/>

</androidx.constraintlayout.widget.ConstraintLayout>


app_bar.xml :
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
app:theme="@style/MyToolbarTheme"
app:title="@string/title" />







No comments:

Post a Comment

Could not identify launch activity: Default activity not found : Error while Launching activity

Problem : I got this Error , When I tried to create an application without any Activity . Basically like to develop an Android Headless appl...