Call a function after the Recyclerview is created - android

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;
}

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().

recyclerview.setLayoutManager()

What is the use of recyclerview.setLayoutManager() in this code?
Please elaborate it in detail.I know about recyclerview but i am confused on what is the use of setLayoutManager() here?
public class MainActivity extends AppCompatActivity {
public static final int NEW_WORD_ACTIVITY_REQUEST_CODE = 1;
private WordViewModel mWordViewModel;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
RecyclerView recyclerView = findViewById(R.id.recyclerview);
final WordListAdapter adapter = new WordListAdapter(this);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
More than a year late but the idea behind the setLayoutManager is to set the layout of the contents, i.e. list of repeating views in the recycler view. If you scroll down to the documentation here, it will tell you that there are several strategies for lists and grids so that should give you a clue. Furthermore, it tells you that without it, RecyclerView will not function i.e. there is no out of the box default.
So if you want e.g. to set the LinearLayoutto be horizontal (by default it is vertical) then you have to specify that.
LinearLayoutManager layoutManager
= new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
RecyclerView myItems = findViewById(R.id.my_recycler_view);
myItems.setLayoutManager(layoutManager);

More info inside expandable recycler view

i hope that you could help me.
i want to use a expandablerecyclerview but i dont need to expand a list of other elements like the lib says
library example
I just need that the item expand and show more info of the item inside the recycler
You can use advanced recyclerview's expandable future
public class ExpandableWithHeaderFooterExampleActivity extends AppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_demo_minimal);
OnListItemClickMessageListener clickListener = new OnListItemClickMessageListener() {
#Override
public void onItemClicked(String message) {
View container = findViewById(R.id.container);
Snackbar.make(container, message, Snackbar.LENGTH_SHORT).show();
}
};
RecyclerView recyclerView = findViewById(R.id.recycler_view);
// Setup expandable feature and RecyclerView
RecyclerViewExpandableItemManager expMgr = new RecyclerViewExpandableItemManager(null);
// Create wrapped adapter: MyItemAdapter -> expMgr.createWrappedAdapter -> MyHeaderFooterAdapter
RecyclerView.Adapter adapter;
adapter = new SimpleDemoExpandableItemAdapter(expMgr, clickListener);
adapter = expMgr.createWrappedAdapter(adapter);
adapter = new DemoHeaderFooterAdapter(adapter, clickListener);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
// NOTE: need to disable change animations to ripple effect work properly
((SimpleItemAnimator) recyclerView.getItemAnimator()).setSupportsChangeAnimations(false);
expMgr.attachRecyclerView(recyclerView);
}
}

How to get reference to a View inside CardView from an activity?

I have got CardView with Views inside a RecyclerView. I have created adapter in which assets are attached to Views and everything works. Now, I would like to change these Views from my activity. Is it any simple way to do that?
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
private List<Offer>offers;
TextView timer; //timer inside CardView
private void getViewReferences() {
recyclerView = (RecyclerView)findViewById(R.id.mainRecyclerView);
timer = (TextView)findViewById(R.id.timer);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getViewReferences();
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
initializeData();
initializeAdapter();
timer.setText("13:16"); //NullPointerException here
}
private void initializeData(){
offers = new ArrayList<>();
offers.add(new Offer("godzina", R.drawable.zdj, 200));
offers.add(new Offer("godzina", R.drawable.zdj));
}
private void initializeAdapter() {
RecyclerViewAdapter adapter = new RecyclerViewAdapter(offers);
recyclerView.setAdapter(adapter);
}
}
So you don't want to change the Views but you want to update the text values of the views in your list.
You can't directly do that by trying to find the Views in the recyclerView and change the text. The adapter is responsible for giving values to your list. So you need to update the dataset in the adapter and call one of the notifyDataSetChanged methods on the adapter to update the recyclerView.
The big advantage of this design is that it places Views in Cache in order to reuse them multiple times without having to inflate and construct them again. This is a huge performance improvement.
You need to read the docs and some tutorials to understand the adapter design pattern better.

Actionbars not setting on first 2 children of a RecyclerView

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...

Categories

Resources