I have to make a scrolling images inside a recycler view with dot as indicator. Please have a look at attached screenshot for the same. Circled image is the dot indicator.
So far i have used the RecyclerView and using the GridLayoutManager. Inside Adapter i have made another item which is HEADER_TYPE i.e for scrolling images. For scrolling i am using this library and have successfully imported to my project.
As i am new to android i am not able to move further i do not have any idea how can i use this inside my project.
Also please suggest whether its the right path to achieve the same result or suggest some another way of doing the same.
My code so far:
public static final int TYPE_HEADER = 1;
public static final int TYPE_ITEM = 0;
List<CatetoryListModel> data = Collections.emptyList();
LayoutInflater inflater;
Context context;
public CategoryRecyclerAdapter(Context context, List<CatetoryListModel> data) {
inflater = LayoutInflater.from(context);
this.data = data;
this.context = context;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == TYPE_HEADER) {
View view = inflater.inflate(R.layout.recycler_header, parent, false);
MyViewHolderHeader myViewHolder = new MyViewHolderHeader(view, context);
return myViewHolder;
} else {
View view = inflater.inflate(R.layout.recycler_custom_row, parent, false);
MyViewHolder myViewHolder = new MyViewHolder(view, context);
return myViewHolder;
}
}
#Override
// public void onBindViewHolder(MyViewHolder holder, int position) {
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
// StaggeredGridLayoutManager.LayoutParams layoutParams = (StaggeredGridLayoutManager.LayoutParams) holder.itemView.getLayoutParams();
// layoutParams.setFullSpan(true);
if (holder instanceof MyViewHolder) {
CatetoryListModel current = data.get(position - 1); //potion now becomes 1 and data start from 0 index
//holder.title.setText(current.getCategoryName());
// holder.desp.setText(current.getDescription());
((MyViewHolder) holder).icon.setImageResource(current.getImgSrc());
} else {
// ((MyViewHolderHeader) holder).icon.setImageResource(R.drawable.banner);
}
}
#Override
public int getItemViewType(int position) {
if (position == 0)
return TYPE_HEADER;
return TYPE_ITEM;
}
//Returns the total number of items in the data set hold by the adapter.
//no of items to be rendered by adapter
#Override
public int getItemCount() {
return data.size() + 1;
}
class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView title;
TextView desp;
ImageView icon;
Context cntxt;
public MyViewHolder(View itemView, Context c) {
super(itemView);
cntxt = c;
itemView.setClickable(true);
itemView.setOnClickListener(this);
// title = (TextView)itemView.findViewById(R.id.category);
// desp = (TextView)itemView.findViewById(R.id.description);
icon = (ImageView) itemView.findViewById(R.id.imgsrc);
}
#Override
public void onClick(View v) {
Toast.makeText(cntxt, "Hello", Toast.LENGTH_LONG).show();
}
}
class MyViewHolderHeader extends RecyclerView.ViewHolder {
//ImageView icon;
// ViewPager mPager;
// CirclePageIndicator mIndicator;
// TestFragmentAdapter mAdapter;
public MyViewHolderHeader(View itemView, Context c) {
super(itemView);
// mAdapter = new TestFragmentAdapter(getSupportFragmentManager());
// mPager = (ViewPager)itemView.findViewById(R.id.pager);
// mPager.setAdapter(mAdapter);
// mIndicator = (CirclePageIndicator)itemView.findViewById(R.id.indicator);
// mIndicator.setViewPager(mPager);
}
}
recycler_header.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
<com.viewpagerindicator.CirclePageIndicator
android:id="#+id/indicator"
android:padding="10dip"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
/>
recycler_custom_row.xml
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
card_view:cardCornerRadius="4dp"
android:id="#+id/card_view"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/imgsrc"
android:src="#drawable/close" />
</RelativeLayout>
Related
I've got a RecyclerView which populates from an ArrayList. The output is a CardView layout.
In the Cardview, there are 2 buttons amongst other Views.
They only have to read the current value of a TextView, which by default is 1, and increase or decrease it.
The Arraylist contains 8 items.
When I run the app the UI works fine. Trouble is when I try to modify the value of the TextView.
The value is correctly increased and decreased on the CardView I'm working on, but ALSO the value is modified on another CardView. And in that second CardView, modifying its TextView value, also modifies the first one.
So, what am I doing wrong?
This is my Fragment:
public class Fragment_rosas extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.layout_rosas,container,false);
RecyclerView recyclerview_rosas;
RecyclerView.Adapter adaptador_rv_rosas;
RecyclerView.LayoutManager lm_rosas;
List rosas = new ArrayList();
rosas.add(new Tropa(1,R.drawable.minibarbaro, getResources().getString(R.string.barbaro),7,1));
recyclerview_rosas = (RecyclerView) view.findViewById(R.id.recyclerView_tropasRosas);
recyclerview_rosas.setHasFixedSize(true);
lm_rosas = new LinearLayoutManager(getContext());
recyclerview_rosas.setLayoutManager(lm_rosas);
adaptador_rv_rosas = new AdaptadorTropa(rosas);
recyclerview_rosas.setAdapter(adaptador_rv_rosas);
return view;
}
}
And here the part of code on my Adapter:
#Override
public void onBindViewHolder(final TropaViewHolder viewHolder, int i) {
viewHolder.imagen.setImageResource(items.get(i).getImagen());
viewHolder.nombre.setText(items.get(i).getNombre());
viewHolder.maxnivel.setText(String.valueOf(items.get(i).getNivelMax()));
viewHolder.espacioencamp.setText((String.valueOf(items.get(i).getEspacioEnCamp())));
final String nombre = items.get(i).getNombre();
final int maxnivel = items.get(i).getNivelMax();
viewHolder.nivelmas.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String niveltemp = viewHolder.nivel.getText().toString();
String nivelmaxtemp = viewHolder.maxnivel.getText().toString();
int nivel = Integer.parseInt(niveltemp);
int maxxnivel = Integer.parseInt(nivelmaxtemp);
int nuevonivel = nivel+1 ;
if (nuevonivel<=maxxnivel) {
viewHolder.txtv_nivel.setText(String.valueOf(nuevonivel));
}
}
});
My OnCreateViewHolder (nothing really happens here):
#Override
public TropaViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.cardview, viewGroup, false);
return new TropaViewHolder(v);
}
Here is the solution, as mentioned in the comment above, it addresses two problems:
1. positiontoValueMap - saves current value for each position
2. onclicklistener is passed to the ViewHolder in onCreateViewHolder
Adapter Class
public class MyAdapter extends RecyclerView.Adapter {
private Context context;
private List<String> dataList;
private Map<Integer, Integer> positionToValueMap = new HashMap<>();
public MyAdapter(Context context, List<String> dataList) {
this.context = context;
this.dataList = dataList;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.recycler_view_item, null, false);
return new MyViewHolder(view, new OnRecyclerItemClickListener());
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
((MyViewHolder) holder).onRecyclerItemClickListener.updatePosition(position);
((MyViewHolder) holder).position.setText("" + position);
((MyViewHolder) holder).title.setText(dataList.get(position));
int valueToDisplay = 1;
if(positionToValueMap.containsKey(position)) {
valueToDisplay = positionToValueMap.get(position);
} else {
positionToValueMap.put(position, valueToDisplay);
}
((MyViewHolder) holder).valueView.setText("value: " + valueToDisplay);
}
#Override
public int getItemCount() {
return dataList.size();
}
private class MyViewHolder extends RecyclerView.ViewHolder {
private OnRecyclerItemClickListener onRecyclerItemClickListener;
private TextView position;
private TextView title;
private TextView valueView;
public MyViewHolder(View itemView, OnRecyclerItemClickListener onRecyclerItemClickListener) {
super(itemView);
itemView.setOnClickListener(onRecyclerItemClickListener);
this.onRecyclerItemClickListener = onRecyclerItemClickListener;
this.position = (TextView) itemView.findViewById(R.id.position);
this.title = (TextView) itemView.findViewById(R.id.title);
this.valueView = (TextView) itemView.findViewById(R.id.value_view);
}
}
private class OnRecyclerItemClickListener implements View.OnClickListener {
private int position = -1;
public void updatePosition(int position) {
this.position = position;
}
#Override
public void onClick(View v) {
int oldValue = positionToValueMap.get(position); // get current value
oldValue++; // increment
positionToValueMap.put(position, oldValue); // save current value
notifyItemChanged(position); // update clicked view so that it picks up the new saved value from the positionToValueMap in onBindViewHolder
}
}
}
RecyclerView item layout
<TextView
android:id="#+id/position"
android:layout_width="30dp"
android:layout_height="50dp"
android:textColor="#android:color/white"
android:gravity="center"
android:background="#android:color/holo_green_light"
android:layout_alignParentLeft="true"/>
<TextView
android:id="#+id/title"
android:layout_width="50dp"
android:layout_height="50dp"
android:textColor="#android:color/white"
android:gravity="center"
android:background="#android:color/holo_green_dark"
android:layout_toRightOf="#id/position" />
<TextView
android:id="#+id/value_view"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textColor="#android:color/white"
android:gravity="center"
android:background="#android:color/holo_green_light"
android:layout_toRightOf="#id/title"
android:layout_alignParentRight="true"/>
</RelativeLayout>
And Activity to test it out
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
recyclerView.setAdapter(new MyAdapter(getApplicationContext(), getSampleData()));
}
private static List<String> getSampleData() {
List<String> dataList = new ArrayList<>();
dataList.add("zero");
dataList.add("one");
dataList.add("two");
dataList.add("three");
dataList.add("four");
dataList.add("five");
dataList.add("six");
dataList.add("seven");
dataList.add("eight");
dataList.add("nine");
dataList.add("ten");
dataList.add("eleven");
dataList.add("twelve");
dataList.add("thirteen");
dataList.add("fourteen");
dataList.add("fifteen");
dataList.add("sixteen");
dataList.add("seventeen");
dataList.add("eighteen");
dataList.add("nineteen");
dataList.add("twenty");
return dataList;
}
}
activity layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/root_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"/>
</RelativeLayout>
I just want to display a list using recycle view inside the view pager. (ex. just like android home page icons)
But here list is not showing and without view pager it works.
Please can anyone tell me where is the problem...
Thank you..
activity.xml
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
obj_tile.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp">
<ImageView
android:id="#+id/iv_icon"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#android:drawable/alert_light_frame" />
<TextView
android:id="#+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="2"
android:text="Group Title" />
</LinearLayout>
-fragment_screen_slide_page.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="PageViewer Title" />
</RelativeLayout>
MainActivity.java
public class MainActivity extends FragmentActivity {
private static final int NUM_PAGES = 5;
private ViewPager mPager;
private PagerAdapter mPagerAdapter;
List<TilePojo> tilePojoList=new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Instantiate a ViewPager and a PagerAdapter.
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
/*Context context=MainActivity.this;
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.fragment_screen_slide_page, null);
RecyclerView rvTiles = (RecyclerView) findViewById(R.id.my_recycler_view);
for (int i = 0; i < 10; i++) {
TilePojo tilePojo = new TilePojo(R.drawable.apple, "Button Title " + i);
tilePojoList.add(tilePojo);
}
TileAdapter adapter = new TileAdapter(tilePojoList);
if(adapter!=null) {
rvTiles.setAdapter(adapter);
rvTiles.setLayoutManager(new GridLayoutManager(this, 2));
}
*/
}
#Override
public void onBackPressed() {
if (mPager.getCurrentItem() == 0) {
// If the user is currently looking at the first step, allow the system to handle the
// Back button. This calls finish() on this activity and pops the back stack.
super.onBackPressed();
} else {
// Otherwise, select the previous step.
mPager.setCurrentItem(mPager.getCurrentItem() - 1);
}
}
/**
* A simple pager adapter that represents 5 ScreenSlidePageFragment objects, in
* sequence.
*/
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return new ScreenSlidePageFragment();
}
#Override
public int getCount() {
return NUM_PAGES;
}
}
}
-ScreenSlidePageFragment.java
public class ScreenSlidePageFragment extends Fragment {
List<TilePojo> tilePojoList=new ArrayList<>();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.fragment_screen_slide_page, container, false);
View v = inflater.inflate(R.layout.fragment_screen_slide_page, null);
RecyclerView rvTiles = (RecyclerView) v.findViewById(R.id.my_recycler_view);
for (int i = 0; i < 10; i++) {
TilePojo tilePojo = new TilePojo(R.drawable.apple, "Button Title " + i);
tilePojoList.add(tilePojo);
}
TileAdapter adapter = new TileAdapter(tilePojoList);
if(adapter!=null) {
rvTiles.setAdapter(adapter);
rvTiles.setLayoutManager(new GridLayoutManager(this.getActivity(), 2));
}
return rootView;
}
}
TileAdapter.java
public class TileAdapter extends RecyclerView.Adapter<TileAdapter.ViewHolder> {
private List<TilePojo> list;
public TileAdapter(List<TilePojo> list) {
this.list = list;
}
#Override
public TileAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
// Inflate the custom layout
View tileView = inflater.inflate(R.layout.obj_tile, parent, false);
// Return a new holder instance
ViewHolder viewHolder = new ViewHolder(tileView);
return viewHolder;
}
#Override
public void onBindViewHolder(TileAdapter.ViewHolder holder, int position) {
TilePojo tilePojo=list.get(position);
holder.imageView.setImageResource(tilePojo.getSrc());
holder.nameTextView.setText(tilePojo.getTitle());
}
#Override
public int getItemCount() {
return list.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
// Your holder should contain a member variable
// for any view that will be set as you render a row
public TextView nameTextView;
public ImageView imageView;
// We also create a constructor that accepts the entire item row
// and does the view lookups to find each subview
public ViewHolder(View itemView) {
// Stores the itemView in a public final member variable that can be used
// to access the context from any ViewHolder instance.
super(itemView);
nameTextView = (TextView) itemView.findViewById(R.id.tv_title);
imageView = (ImageView) itemView.findViewById(R.id.iv_icon);
}
}
}
I got the answer..
I just replace the following (#ScreenSlidePageFragment.java)
rootView.findViewById(R.id.my_recycler_view);
Thank you.
I have a RecyclerView with items that have the following layout:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/text_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"/>
<View style="#style/Divider"/>
<GridLayout
android:id="#+id/grid_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
GridLayout could contains 1 or more(30) views depends on recyclerview item.
Currently, in onBindViewHolder(recyclerview adapter) I add dynamically the views in the GridLayout.
How could I improve the performance? Beside that, the first items don't display the views added in GridLayout, but after scrolling they appear.
Thank you.
public static class MyAdapter extends RecyclerView.Adapter<MyAdapter.CustomiewHolder> {
private Context mContext;
private List<Items> mItems;
public MyAdapter(Context context, List<Items> items) {
this.mContext = context;
this.mItems = items;
}
#Override
public CustomiewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).
inflate(R.layout.item_card, parent, false);
return new CustomiewHolder(itemView);
}
#Override
public void onBindViewHolder(CustomiewHolder holder, int position) {
Item item = mItems.get(position);
holder.mName.setText(item.getName());
holder.mGridLayout.removeAllViews();
holder.mGridLayout.setColumnCount(COLUMN_COUNT);
for (int i = 0; i < item.getTiles().getItemCount(); i++) {
View child = getView(holder.mRootLayout, item.getTiles().get(i));
holder.mGridLayout.addView(child);
}
}
public View getView(ViewGroup parent, Tile tile) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.tile_card, parent, false);
((TextView) view.findViewById(R.id.tile_name)).setText(tile.getName());
view.findViewById(R.id.line).setBackgroundResource(tile.getType().getDrawableResId());
return view;
}
#Override
public int getItemCount() {
return mItems.size();
}
public static class CustomiewHolder extends RecyclerView.ViewHolder {
#Bind(R.id.root_layout)
public CardView mRootLayout;
#Bind(R.id.station_name)
public TextView mName;
#Bind(R.id.routes_layout)
public GridLayout mGridLayout;
public CustomiewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
}
}
I want to make RecyclerView like this one:
But in my case, child view doesn't set as width="wrap_content" or it RecyclerView doesn't set as width="wrap_content" and on "center"
Here is RecyclerView in activity_layout.xml:
<RelativeLayout 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.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
Here is setting Adapter onCreate:
public void initRecyclerView(List<Invitation> invitationList) {
recyclerView.setHasFixedSize(true);
GridLayoutManager manager = new GridLayoutManager(this, 4);
recyclerView.setLayoutManager(manager);
recyclerView.setAdapter(new ItemGridAdapter(getApplicationContext(), invitationList));
}
here is my Adapter:
public class ItemGridAdapter extends RecyclerView.Adapter<ItemGridAdapter.ViewHolderItem> {
private final Context context;
private final List<Invitation> list;
private final DrawableHelper drawableHelper;
public ItemGridAdapter(Context context, List<Invitation> list) {
this.context=context;
this.list=list;
this.drawableHelper = new DrawableHelper();
}
#Override
public ItemGridAdapter.ViewHolderItem onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_grid, parent, false);
return new ItemGridAdapter.ViewHolderItem(context,view);
}
#Override
public void onBindViewHolder(final ItemGridAdapter.ViewHolderItem viewHolder, int position) {
Invitation invitation = list.get(position);
viewHolder.position=position;
Picasso.with(context)
.load(invitation.getCustomUser().getAvatar())
.transform(new CircleTransformation())
.placeholder(drawableHelper.getDrawableForName(invitation.getCustomUser().getFullName()))
.into(viewHolder.userIcon);
if (invitation.getYelpID()!=null&&invitation.getYelpID().length()>0){
viewHolder.votedIcon.setVisibility(View.VISIBLE);
}else{
viewHolder.votedIcon.setVisibility(View.GONE);
}
}
#Override
public int getItemCount() {
return list.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public static class ViewHolderItem extends RecyclerView.ViewHolder {
public ImageView votedIcon;
public ImageView userIcon;
Context mContext;
int position;
public ViewHolderItem(Context mContext,View itemView) {
super(itemView);
this.mContext = mContext;
userIcon=(ImageView)itemView.findViewById(R.id.userIcon);
votedIcon = (ImageView)itemView.findViewById(R.id.votedIcon);
}
}
}
here is layout of item R.layout.item_grid for adapter :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center">
<ImageView
android:background="#drawable/white_circle"
android:padding="3dp"
android:id="#+id/userIcon"
android:layout_width="50dp"
android:layout_height="50dp"/>
<ImageView
android:id="#+id/votedIcon"
android:layout_width="20dp"
android:layout_height="20dp"
android:src="#drawable/icon_voted"/>
</RelativeLayout>
Android Support Library as of 23.2 supports this WRAP_CONTENT in RecyclerView by default.
I am trying to create a recyclerview in a nav drawer with the header showing the profile information. I would like to have a header height more than the other row elements. below is my header layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#color/green"
android:orientation="vertical"
android:weightSum="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="56dp"
android:orientation="vertical"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:textColor="#ffffff"
android:text="XXXXX"
android:textSize="32sp"
android:textStyle="bold"
/>
</LinearLayout>
</RelativeLayout>
When I set this to the header of the recycler view , the 200dp height is not reflecting in the UI
RecyclerView.Adapter<NavDrawerListViewHolder> adapter = new NavDrawerListAdapter(this,TITLES,this);
navList.setAdapter(adapter);
Below is the adapter for the recyclerview :
public class NavDrawerListAdapter extends RecyclerView.Adapter<NavDrawerListViewHolder> {
private static final int TYPE_HEADER = 0;
private static final int TYPE_ITEM = 1;
Context mContext;
String[] mCharacterList;
IListItemClickListener mListener;
int holderViewType=0;
public NavDrawerListAdapter(Context context, String[] characters, IListItemClickListener clickListener) {
mContext = context;
mCharacterList = characters;
mListener = clickListener;
}
#Override
public NavDrawerListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
NavDrawerListViewHolder viewHolder;
holderViewType = viewType;
if(viewType == TYPE_HEADER) {
View v = LayoutInflater.from(mContext).inflate(R.layout.header,null);
viewHolder = new NavDrawerListViewHolder(v,TYPE_HEADER,mListener);
} else {
View v = LayoutInflater.from(mContext).inflate(R.layout.nav_item_row,null);
viewHolder = new NavDrawerListViewHolder(v,TYPE_ITEM,mListener);
}
return viewHolder;
}
#Override
public void onBindViewHolder(NavDrawerListViewHolder holder, int position) {
if(holderViewType == TYPE_HEADER) {
Typeface typeface = Typeface.createFromAsset(mContext.getAssets(), "Roboto-Thin.ttf");
holder.name.setTypeface(typeface);
} else {
holder.characterName.setText(mCharacterList[position - 1]);
}
}
#Override
public int getItemViewType(int position) {
if (position == 0) {
return TYPE_HEADER;
}
return TYPE_ITEM;
}
#Override
public int getItemCount() {
return mCharacterList.length + 1;
}
Replace:
LayoutInflater.from(mContext).inflate(R.layout.header,null);
with:
LayoutInflater.from(mContext).inflate(R.layout.header, parent, false);
and the same for all your other inflate() calls. If you know the parent container for the layout being inflated, always supply it to the inflate() call. In particular, RelativeLayout needs this, if it is the root view of the layout being inflated.
Also, consider using getLayoutInflater() on the Activity instead of LayoutInflater.from(). It may be that if you pass the Activity into from() that you will get equivalent results. However, in general, you always want to use a LayoutInflater that knows about the Activity and its theme, so you get the right results.