I want to use the grid view in which first image is constant and on the click want to launch addProductActivity for all user , and after that the images from server will be displayed. How to achieve this please help.
You can achieve this by recyclerview where you can give layout manager as gridlayout manager :
RecyclerView mRecyclerView = (RecyclerView) view.findViewById(R.id.fridID);
Adapter gridAdapter = new Adapter(yourProductArray, getContext(), this, this);
GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 3);
mRecyclerView.setLayoutManager(gridLayoutManager);
mRecyclerView.setAdapter(gridAdapter);
and at the recyclerview Adapter when position is zero in onBindViewHolder(Adapter.ViewHolder holder, int position), always show the constant image
You can set onClickListener to the ImageView and when the position is zero and onClickListener is triggered show the add product screen
when a new product is added add the product to the yourProductArray and use gridAdapter.notifyDataSetChanged()
Related
As shown in the screenshot, I have the list of colors in recyclerView.
I want to create a category like below, when the user clicks on each category, recyclerview shows those range of colors.
How can i create this kind of color category?
I'd use a GridLayoutManager, so that your recycler view is rendered as a Grid, in its constructor parameter you set the number of columns, e.g.:
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mLayoutManager = new GridLayoutManager(mContext, 7);
mRecyclerView.setLayoutManager(mLayoutManager);
// Initialize a new instance of RecyclerView Adapter instance
mAdapter = new MyAdapter(mContext, myData);
// Set the adapter for RecyclerView
mRecyclerView.setAdapter(mAdapter);
Full example here
HIH
On button click I want to change list view to gridview as on shopping-cart pages but my layout looks like link shown below.
//For displaying the row as linear list view
case R.id.ivGrid:
ivList.setVisibility(View.VISIBLE);
ivGrid.setVisibility(View.GONE);
LinearLayoutManager llm = new LinearLayoutManager(context);
rcvProducts.setLayoutManager(llm);
break;
//For displaying the row as gridview
case R.id.ivList:
ivList.setVisibility(View.GONE);
ivGrid.setVisibility(View.VISIBLE);
GridLayoutManager glm = new GridLayoutManager(this,2,GridLayoutManager.VERTICAL, false);
rcvProducts.setLayoutManager(glm);
break;
GRIDview
LISTview
Just use a GridLayoutManager and change the span count from 1 to 2 and back again.
view.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
GridLayoutManager layoutManager = (GridLayoutManager) grid.getLayoutManger();
layoutMananger.setSpanCount(layoutManager.getSpanCount() == 2 ? 1 : 2);
}
})
Maintain 2 adapters, one for ListView and one for GridView.
On button click, change the adapter to RecyclerView.
Listview.setAdapter(gridAdapter) (or) ListView.setAdapter(listAdapter)
Within RecyclerView, any tips on loading a different viewholder onitemclick? i'm having trouble wrapping my head around loading viewholder 2 when item is clicked in viewholder 1 at that position. both viewholder 1 and 2 are a list of cards. so say for example. vh1 contains a sample hotel image. When you click that vh2 shows you contact details of that hotel - at that position, while retaining vh1 through out the other un-clicked contents.
I did ask a question like this before and i think it was a bit vague so people suggested i change information based onitem click. The problem is vh1 and 2 are different layouts completely so this wouldn't work for what i'm working on.
is this even possible?
Thanks
You can create another adapter for your RecyclerView. Create a new Java class, call it whatever you want. In your main activity, or the activity where you're setting your adapter, you can create a function like this:
public void setNewAdapter() {
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.your_recyclerview_id);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
NewAdapter adapter = new NewAdapter(// any arguments);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
}
In your adapter, you can create a new instance of your activity. For example, if you're activity is called MainActivity, you can add this code in your onItemClick:
MainActivity mainActivity = new MainActivity();
mainActivity.setNewAdapter();
See if that works.
I want to be able to automatically scroll inside an horizontal RecyclerView and put the selected item in middle of the screen
In the attached image the selected item is "my apps" so i want to automatically scroll the RV so that "my apps" will show on center of screen
I know How to scroll to speciffic position:
linearLayoutManager.scrollToPositionWithOffset(2, 20);
How can i get the relative position of the item inside the recyclerView so i can scroll to it?
My RecyclerView def:
CategoriesAdapter adapter = new CategoriesAdapter(list, this);
catagoriesRV.setAdapter(adapter);
LinearLayoutManager llm = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
catagoriesRV.setLayoutManager(llm);
I'm creating an app with a list of questions in a RecyclerView. In the same parent layout, I have a static TextView above the RecyclerView that displays instruction for a set of questions. What I want is to change the text in the TextView whenever a specific question reaches the top of the RecyclerView when scrolled. How can I accomplish this?
You would need to listen to the scrolling of your RecyclerView and get the first item displayed onto the screen.
Thanksfully, Android let us set a custom ScrollListener to a RecyclerView and with your LayoutManager you are able to know which item is shown first :
// initialize your RecyclerView and your LayoutManager
mLinearLayoutManager = new LinearLayoutManager(getContext());
mRecyclerView.setLayoutManager(mLinearLayoutManager);
// set a custom ScrollListner to your RecyclerView
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
// Get the first visible item
int firstVisibleItem = mLayoutManager.findFirstVisibleItemPosition();
//Now you can use this index to manipulate your TextView
}
});
EDIT : added Mateus Gondim clearance about the LayoutManager
You should override the default Recycler.OnScrollListener.
Have a look at this Code : https://gist.github.com/mipreamble/b6d4b3d65b0b4775a22e