Actionbars not setting on first 2 children of a RecyclerView - android

I am trying to make an app which shows list of Orders in a RecyclerView.
Each Order has its own toolbar (with actionbar attached to it) but whenever I start that activity the first 2 Orders do not attach the actionbar to the toolbar...
Following are some of my code snippets:
onBindViewHolder cod -
#Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
String customer_name=mDataset[position].getCustomer().getFirstName()+" "+mDataset[position].getCustomer().getLastName();
String customer_phone=mDataset[position].getCustomer().getPhone()+"";
//Dates
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy");
//
String date=sdf.format(mDataset[position].getBookingDate() );
String booking_date="Booking Date\n"+date;
//
date=sdf.format(mDataset[position].getPickUpDate());
String pickup_date="Pick Up Date\n"+date;
//
date=sdf.format(mDataset[position].getDropDate());
String drop_date="Drop Date\n"+date;
holder.order_id_oh.setText(mDataset[position].getOrderCode());
holder.booking_date_oh.setText(booking_date);
holder.pickup_date_oh.setText(pickup_date);
holder.order_status_oh.setText(mDataset[position].getStatus().toString());
holder.drop_date_oh.setText(drop_date);
holder.customer_name_oh.setText(customer_name);
holder.phone_no_oh.setText(customer_phone);
holder.pickup_locaion_oh.setText(mDataset[position].getAddress());
holder.drop_location_oh.setText(mDataset[position].getCustomer().getAddress());
//
//
((AppCompatActivity)orderAdapter_ctx).setSupportActionBar(holder.menu_toolbar_oh);
//
holder.phone_no_oh.setTextColor(Color.parseColor("#845a9a"));
//
}
Parameterized Constructor for getting Context -
public OrderAdapter(Order[] myDataset,Context ctx) {
orderAdapter_ctx=ctx;
mDataset = myDataset;
}
Part of the Activity related to Recycler -
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_order_history);
//
mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view_order_history);
// use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView
mRecyclerView.setHasFixedSize(true);
// use a linear layout manager
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
mAdapter = new OrderAdapter(orders,this);
mRecyclerView.setAdapter(mAdapter);
Here are 2 screenshots:
First 2 Orders don't have the actionbar attached (3 dots are missing)
Here the actionbar is attached (3 dots are present)
Please help me with this...

Related

Not able to set data in the recycler view

I wanted to set the data(result)into the recycler view, but when i run, the data is not displayed on the screen
RPCRequest.OnResponseListener orderListener = new RPCRequest.OnResponseListener<ArrayList<Person>>() {
#Override
public void onSuccessResponse(RPCRequest request, ArrayList<Person> response) {
ArrayList<Person> dups=new ArrayList<>();
for(int i=0;i<response.size();i++){
if(compare(response.get(i).getDate(),Person.getInstance().getDate()))
dups.add(response.get(i));
}
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.form);
View layout=(View) dialog.findViewById(R.id.row);
layout.setVisibility(View.GONE);
Toolbar toolbar = (Toolbar) dialog.findViewById(R.id.toolbar);
toolbar.setTitle(" Duplicates");
final RecyclerView dupOrders = (RecyclerView) dialog.findViewById(R.id.recycler_view);
dupOrders.setAdapter(adapter);
adapter.setData(dups);
dialog.show();
This is my code where i want to set the data in recycler list view. but when i debug i get the value in data. but its not displayed while executing. Can anyone please help.
You need to set a LayoutManager for the RecyclerView.
Add this line:
dupOrders.setLayoutManager(new LinearLayoutManager(context));
You should probably do this just once, so you might want to make your dupOrders variable into a field of your Activity and initialize it and set the LayoutManager in onCreate().

add recyclerView inside another recyclerView

I have a list of data to be displayed in a recycler view and that is working fine. Now I have another dynamic list of data to be displayed inside the parent recycler-view. So I tried using another recycler-view inside the parent recycler-view, that is not working fine. It will be good if I get some idea of using recycler-view inside another one. Thanks in advance..!
I have illustrated my problem with an example:
For eg: I have a parent recyclerView with five linearLayout and I have created a child recyclerView inside the Linearlayout with visibility GONE. Now when I click the first Linearlayout I am changing the visibility of child recyclerView for the first Linearlayout to VISIBLE and attaching a separate view to it and same concept for all the other Linearlayouts. What happens is when I click first, second, third and fourth linearLayout the child recyclerView is not displaying date which I pass to it, all those first, sec, third and fourth data are accumulated and displayed in the last (i.e) inside fifth linearLayout.
Here is my parent recyclerview code:
class CardAdapter extends RecyclerView.Adapter<CardAdapter.MyViewHolder>
{
RecyclerView insideCardRecyclerView,recyclerView;
List<String> monthsWeek = new ArrayList<>();
List<String> dealers = new ArrayList<>();
List<String> dealersList = new ArrayList<>();
List<String> date = new ArrayList<>();
HashSet<String> dealersListHash = new HashSet<>();
public CardAdapter(List<String> monthsWeek,List<String> dealers,List<String> date)
{
this.monthsWeek = monthsWeek;
this.dealers = dealers;
this.date = date;
}
public class MyViewHolder extends RecyclerView.ViewHolder
{
ProgressBar progressBar;
RecyclerView recyclerView;
TextView period;
LinearLayout linearLayoutParent,linearLayoutCardDetails;
public MyViewHolder(View view)
{
super(view);
linearLayoutParent = (LinearLayout) view.findViewById(R.id.card_view_linear_parent_layout);
linearLayoutCardDetails = (LinearLayout) view.findViewById(R.id.linear_card_layout_details);
period = (TextView) view.findViewById(R.id.period_summary_graph_card);
insideCardRecyclerView = (RecyclerView) view.findViewById(R.id.summary_graph_card_view_recycler_view);
}
}
#Override
public CardAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
View itemView = LayoutInflater.from(getActivity())
.inflate(R.layout.summary_card_view,parent,false);
recyclerView = (RecyclerView) parent;
return new CardAdapter.MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(final MyViewHolder holder, final int position)
{
holder.period.setText(monthsWeek.get(position));
holder.linearLayoutParent.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view)
{
if(searchClick)
{
for (String date1 : date)
{
if(Objects.equals(date1,monthsWeek.get(position)))
{
Log.e("Summary123 date..///", date1);
dealersList.add(dealers.get(date.indexOf(date1)));
}
}
searchClick = false;
holder.linearLayoutCardDetails.setVisibility(View.VISIBLE);
dealersListHash.addAll(dealersList);
dealersList.clear();
dealersList.addAll(dealersListHash);
//if the condition is true i am attaching another recyclerview inside this.
cardAdapterList = new CardAdapterList(dealersList);
LinearLayoutManager mLayoutManager1 = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL,true);
mLayoutManager1.setReverseLayout(false);
insideCardRecyclerView.setLayoutManager(mLayoutManager1);
insideCardRecyclerView.setItemAnimator(new DefaultItemAnimator());
insideCardRecyclerView.setAdapter(cardAdapterList);
}
else
{
searchClick = true;
holder.linearLayoutCardDetails.setVisibility(View.GONE);
}
}
});
}
#Override
public int getItemCount() {
return monthsWeek.size();
}
}
I've been in trouble sometimes with RecyclerView when multiple lists are needed to be shown in a single page of the application. Its not a very good idea actually to have multiple lists in a single layout but however, the idea of having a ScrollView and the lists inside that ScrollView is even worse.
I had to implement a ListView inside a ScrollView once and yes it was not a very good experience. Firstly, my list was not scrolling at all. Then I had to add some code to disable the scrolling when the touch is detected inside the list. It was not a very good idea of solving the actual problem. I had another problem of having a fixed height of the ListView. In case of list items with dynamic heights, the solution failed.
Having two lists in the layout, one after one is not a good idea either. As the first list need to have a fixed height.
So, after searching for suggestions about how can I implement two lists in a single layout file, I found most of the developers suggests of having a single list with a header and footer if necessary. Later, I could manage to show two lists in a single RecyclerView using my custom Adapter. I thought I should save some of my code for future use and hence, you see this note.
You can refer this sample code.

Android - Getting and modifying the same List from two different activities

In my App I have crated a MyList.class which looks like this:
public class MyList {
private ArrayList<Obj> objList = new ArrayList<>();
//adds object to list
public void addObjToList(Obj obj) {
objList.add(obj);
}
//gets the whole list
public static ArrayList getObjList() {return objList;}
//gets the size of the list
public static int getObjListSize() {return objList.size();}
//removes obj from list based on his position
public void removeObj(int pos) {
objList.remove(pos);
}
}
From the CreateObj.class where I create the Obj I have this code to add it to the objList:
// creates the new object
Obj newObj = new Obj("Name", 3 /*int*/ );
// creates a new List
MyList myList = new MyList();
// adds the obj into the list
myList.addObjToList(newObj);
It successfully adds the obj to the list. Now from my Main_Activity.class I need to retrieve it and inflate it into a recyclerView, which I do in the onCreate() method like this:
currentObjList = MyList.getObjList();
//puts list into recycler
recyclerView = (RecyclerView) findViewById(R.id.recycler);
recyclerView.setLayoutManager(new LinearLayoutManager(this,
LinearLayoutManager.VERTICAL, false));
adapter = new RecyclerAdapter(this, currentObjList);
recyclerView.setAdapter(adapter);
Notice that I do not set MyList myList = new MyList() in my Main_Activity because I want the list to be the one created in the CreateObj class.
Obviously this is not the right way to do it, because if, let's say I want to delete an element from the recyclerView, I need to remove it from the objList (in the MyList.class) too, and that's not possible since I cannot access the MyList.class methods without setting a new MyList(), and if I do set it to new, It would not keep the Obj added from the CreateObj class.
In short: How can I have the same objList to be both accessible and modifiable from both, CreateObj.class and Main_Activity.class.
Following my comment this is a draft of what I was suggesting.
Please note I haven't ran this code so it must have errors and typos, it is just to reflect the idea of what I was proposing.
The Activity that receives the input holds a referenct to the class that creates the object and to the class that holds the ArrayList.
Upon user input, the activity asks the object creator to create an ojbect and passes it back to the activity. Then the activity adds it to the list.
And finally it notifies the recycler adapter that the data has changed.
In MainActivity:
private CreateObj createObj;
private MyList myList;
//Other memeber variables for Input elements on the screen
//used in createObje.create() to build the new object.
public void onCreate(...){
...
createObj = new CreateObj();
myList = new MyList();
currentObjList = MyList.getObjList();
//puts list into recycler
recyclerView = (RecyclerView) findViewById(R.id.recycler);
recyclerView.setLayoutManager(new LinearLayoutManager(this,
LinearLayoutManager.VERTICAL, false));
adapter = new RecyclerAdapter(this, currentObjList);
recyclerView.setAdapter(adapter);
...
aUserConfirmInputElement.setOnClickListener(new OnClickListener()){
public void onClick(){
Obj obj = createObj.create();
myList.addObjectToList(obj);
adapter.notifyDataSetChanged();
}
}
...
}

Call a function after the Recyclerview is created

I would like to insert a tutorial on my list of Cardview using a library. For this library I need to give a view (element pointed) but my mRecyclerView.findViewHolderForItemId(mAdapter.getItemId(1)).itemView always returns null. Why does this return null? Because the recycle view is not yet created?
I also try to replace it with : mRecyclerView.findViewHolderForItemId(mAdapter.getItemId(1)).itemView)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_players);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view_list_player);
// use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView
mRecyclerView.setHasFixedSize(true);
// use a linear layout manager
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
// specify an adapter (see also next example)
mDataset = Data.bdd.get_list_players();
mAdapter = new CardsViewAdapter(mDataset);
mRecyclerView.setAdapter(mAdapter);
// Tutorial
new MaterialIntroView.Builder(this)
.enableDotAnimation(true)
.enableIcon(true)
.setFocusGravity(FocusGravity.CENTER)
.setFocusType(Focus.MINIMUM)
.setDelayMillis(500)
.enableFadeAnimation(true)
.setInfoText("Hi There! Click this card and see what happens.")
.setShape(ShapeType.RECTANGLE)
.setTarget(mRecyclerView.findViewHolderForItemId(mAdapter.getItemId(1)))
.show();
}
I think your CardsViewAdapter library uses the default getItemId (which returns -1). You can use RecyclerView.getChild() instead.
The easiest way for me was to modify my builder like this and place my function directly in my adapter
CardsViewAdapter(List<Player> myDataset, Activity activity) {
mDataset = myDataset;
mActivity = activity;
}

Add fragments dynamically

I need to show text below image but with horizontal swipe functionality. That is every time a person swipes horizontally image and text is updated. But the problem is I don't know the number of images with text beforehand as they are returned by server. How can I proceed with this?
Use a ViewPageAdapter to achieve this functionality. The first step is to obtain the images and the corresponding text from the server. I would prefer to customize the server to return it json array.
With the obtained value from server, write an adapter something similar to below. Here myValues will be the array where all the values in the server will be stored.
Create a Fragment (below MyFragment) with a static method that will return a new instance of the fragment with desired values set.
Now as you swipe, the method getItem(pos) will be called and a new instance of fragment will be shown to the user.
private class MyPagerAdapter extends FragmentPagerAdapter {
ArrayList<Values> myValues = new ArrayList<Values>();
public MyPagerAdapter(FragmentManager fm,ArrayList<Values> val) {
super(fm);
this.myValues = val;
}
#Override
public Fragment getItem(int pos) {
return MyFragment.newInstance("text","image");
}
#Override
public int getCount() {
return myValues.length();
}
}
}
Hope this helps. :)
use a RecyclerView with Horizontal Scroll.
recyclerview = (RecyclerView) view.findViewById(R.id.recyclerview);
layoutManager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
recyclerview.setLayoutManager(layoutManager);
recyclerview.setItemAnimator(new DefaultItemAnimator());
recyclerview.setHasFixedSize(true);
recyclerview.setAdapter(adapter);
You need to update textView (either in separate fragment or in the same activity) inside onSwipeLeft or onSwipeRight.
To have these callbacks you need to create your custom listener that extends the TouchListener.
Great example of doing this is here.

Categories

Resources