Want to add Spacing between the cards of CardView without using cardUseCompatPadding, How can i achieve that ?
Set Padding on rows so that all the items set spacing between each items
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="#FFF"
android:padding="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
// here your views
</LinearLayout>
or, Use like this by java to saperate items
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setNestedScrollingEnabled(false);
recyclerView.setItemAnimator(new DefaultItemAnimator());
int spacingInPixels = getResources().getDimensionPixelSize(R.dimen.spacing);
recyclerView.addItemDecoration(new SpacesItemDecoration(spacingInPixels));
Class
public class SpacesItemDecoration extends RecyclerView.ItemDecoration {
private int space;
public SpacesItemDecoration(int space) {
this.space = space;
}
#Override
public void getItemOffsets(Rect outRect, View view,
RecyclerView parent, RecyclerView.State state) {
outRect.left = space;
outRect.right = space;
outRect.bottom = space;
// Add top margin only for the first item to avoid double space between items
if (parent.getChildLayoutPosition(view) == 0) {
outRect.top = space;
} else {
outRect.top = 0;
}
}
}
If you are using data binding, you may prefer to inspect <Space /> component in your list item for horizontal RecyclerView spacing
like below;
<data>
<variable
name="data"
type="com.foo.Entity" />
<variable
name="position"
type="int" />
<import type="android.view.View" />
</data>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="45dp"
android:gravity="center"
android:orientation="horizontal">
<Space
android:layout_width="6dp"
android:layout_height="6dp"
android:visibility="#{position == 0 ? View.GONE : View.VISIBLE}" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#drawable/gray_border">
<androidx.appcompat.widget.AppCompatTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_margin="8dp"
android:background="#color/white"
android:gravity="center"
android:lineSpacingExtra="4sp"
android:maxLines="1"
android:singleLine="true"
android:text="#{data.text}"
android:textColor="#6b778d"
android:textSize="12sp"
android:textStyle="normal"
tools:text="Foo" />
</RelativeLayout>
</LinearLayout>
And you will see;
Related
Here is my code.i want to put 3 recyclerViews in my layout but it is very slow on device while running. If there is 2 recyclerview it works smooth but if 3 slow very :
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:id="#+id/scroll_view"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".fragments.MainFragment">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textColor="#color/blacker"
android:textStyle="bold"
android:textSize="18sp"
android:text="Техника для офиса"
android:layout_marginTop="20dp"
android:layout_marginLeft="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp" />
<android.support.v7.widget.RecyclerView
android:nestedScrollingEnabled="false"
android:fillViewport="true"
android:layout_marginBottom="15dp"
android:layout_marginTop="15dp"
android:id="#+id/recyclerView_popular_goods"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
<TextView
android:textColor="#color/blacker"
android:textStyle="bold"
android:textSize="18sp"
android:text="Компьютерная техника"
android:layout_marginTop="20dp"
android:layout_marginLeft="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp" />
<android.support.v7.widget.RecyclerView
android:nestedScrollingEnabled="false"
android:fillViewport="true"
android:layout_marginBottom="15dp"
android:layout_marginTop="15dp"
android:id="#+id/recyclerView_competitive_goods"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
<TextView
android:textColor="#color/blacker"
android:textStyle="bold"
android:textSize="18sp"
android:text="Канцелярские товары"
android:layout_marginTop="20dp"
android:layout_marginLeft="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp" />
<android.support.v7.widget.RecyclerView
android:visibility="gone"
android:fillViewport="true"
android:layout_marginBottom="15dp"
android:layout_marginTop="15dp"
android:id="#+id/recyclerView_stationery_goods"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
</LinearLayout>
Please if somebody had such kind of problem please help me to fix my issue.
I tried Nested scrollview and setNestedScrollingEnabled = false.but nothing helps
Here is my Activity
#SuppressLint("ValidFragment")
public class MainFragment extends BasicFragment implements
MainFragmentView, PopularGoodsAdapter.ProductListener {
#BindView(R.id.scroll_view)
NestedScrollView mScrollView;
#BindView(R.id.recyclerView_stationery_goods)
RecyclerView mRecyclerViewStationeryGoods;
#BindView(R.id.recyclerView_popular_goods)
RecyclerView mRecyclerViewPopularGoods;
#BindView(R.id.recyclerView_competitive_goods)
RecyclerView mRecyclerViewCompetitiveGoods;
#BindView(R.id.imageSlider)
SliderView mSliderView;
#Inject
Navigator mNavigator;
#Inject
#Named(DISPLAY_WIDTH)
int mDisplayWidth;
#InjectPresenter
MainFragmentPresenter mPresenter;
List<Product> list1;
List<Product> list2;
List<Product> list3;
private PopularGoodsAdapter mAdapterPopularGoods;
private PopularGoodsAdapter mAdapterPopularGoods2;
private PopularGoodsAdapter mAdapterPopularGoods3;
private LayoutAnimationController layoutAnimationController;
private List<String> images;
#SuppressLint("ValidFragment")
public MainFragment() {
}
private void initView(View view) {
ButterKnife.bind(this,view);
images = new LinkedList<>();
images.add("https://images.pexels.com/photos/747964/pexels-photo-747964.jpeg?auto=compress&cs=tinysrgb&h=750&w=1260");
images.add("https://images.pexels.com/photos/929778/pexels-photo-929778.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260");
images.add("https://images.pexels.com/photos/218983/pexels-photo-218983.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260");
mSliderView.setSliderAdapter(new SliderAdapterExample(getContext(), images, "mainPage"));
LinearLayoutManager layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);
LinearLayoutManager layoutManager2 = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);
LinearLayoutManager layoutManager3 = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);
mRecyclerViewPopularGoods.setLayoutManager(layoutManager);
layoutAnimationController = AnimationUtils.loadLayoutAnimation(getContext(),R.anim.layout_item_from_left);
list1 = new LinkedList<>();
mRecyclerViewPopularGoods.setLayoutAnimation(layoutAnimationController);
mRecyclerViewCompetitiveGoods.setLayoutManager(layoutManager2);
list2 = new LinkedList<>();
mRecyclerViewCompetitiveGoods.setLayoutAnimation(layoutAnimationController);
mRecyclerViewStationeryGoods.setLayoutManager(layoutManager3);
list3 = new LinkedList<>();
mRecyclerViewStationeryGoods.setLayoutAnimation(layoutAnimationController);
mRecyclerViewPopularGoods.setAdapter(mAdapterPopularGoods = new PopularGoodsAdapter(getContext(),list1,mDisplayWidth,"popular",this));
mRecyclerViewCompetitiveGoods.setAdapter(mAdapterPopularGoods2 = new PopularGoodsAdapter(getContext(),list2,mDisplayWidth,"competitive",this));
mRecyclerViewStationeryGoods.setAdapter(mAdapterPopularGoods3 = new PopularGoodsAdapter(getContext(),list3,mDisplayWidth,"stationery",this));
mRecyclerViewPopularGoods.setNestedScrollingEnabled(false);
mRecyclerViewCompetitiveGoods.setNestedScrollingEnabled(false);
mRecyclerViewStationeryGoods.setNestedScrollingEnabled(false);
getLoadingDialog().showDialog(getFragmentManager());
mPresenter.GET_TOP_HOME_VIEW();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup
container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, container,
false);
initView(view);
return view;
}
#OnClick({R.id.item_sellers,
R.id.item_price_request,R.id.item_price_competitions})
void Onclick(View view) {
switch (view.getId()) {
case R.id.item_sellers:
mNavigator.toSellersActivty(getContext());
break;
case R.id.item_price_request:
mNavigator.toPriceRequestActivty(getContext());
break;
case R.id.item_price_competitions:
mNavigator.toCompetitionsActivty(getContext());
break;
}
}
#Override
public void initTopHomeView(List<HomeViewObject> result) {
if (null != result ) {
if (result.size() >= 1 && null != result.get(0)) {
list1.addAll(result.get(0).getTopProducts());
mAdapterPopularGoods.notifyDataSetChanged();
mRecyclerViewPopularGoods.setLayoutAnimation(layoutAnimationController);
}
if (result.size() >= 2 && null != result.get(1)) {
list2.addAll(result.get(1).getTopProducts());
mAdapterPopularGoods2.notifyDataSetChanged();
mRecyclerViewPopularGoods.setLayoutAnimation(layoutAnimationController);
}
if (result.size() >= 3 && null != result.get(2)) {
list3.addAll(result.get(2).getTopProducts());
mAdapterPopularGoods3.notifyDataSetChanged();
mRecyclerViewStationeryGoods.setLayoutAnimation(layoutAnimationController);
}
}
}
#Override
public void stopProgress() {
getLoadingDialog().hideDialog();
}
#Override
public void onProductClicked(Product product) {
mNavigator.toProductActivity(getContext(),product);
}
}
Is there any mistake?
Try the following -
Try to put all the views inside NestedScrollView i.e, root view.
Set recyclerView.setNestedScrollingEnabled(false); for both the recyclerViews in your xml.
Change your recycler view's android:layout_height="match_parent" to android:layout_height="wrap_content" and then use Nested scrollView with recyclerView.setNestedScrollingEnabled(false);
try to change your ScrollView with NestedScrollViewand Change your recycler view's android:layout_height="match_parent" to android:layout_height="wrap_content" :
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
android:id="#+id/scroll_view"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".fragments.MainFragment">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textColor="#color/blacker"
android:textStyle="bold"
android:textSize="18sp"
android:text="Техника для офиса"
android:layout_marginTop="20dp"
android:layout_marginLeft="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp" />
<android.support.v7.widget.RecyclerView
android:nestedScrollingEnabled="false"
android:fillViewport="true"
android:layout_marginBottom="15dp"
android:layout_marginTop="15dp"
android:id="#+id/recyclerView_popular_goods"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:textColor="#color/blacker"
android:textStyle="bold"
android:textSize="18sp"
android:text="Компьютерная техника"
android:layout_marginTop="20dp"
android:layout_marginLeft="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp" />
<android.support.v7.widget.RecyclerView
android:nestedScrollingEnabled="false"
android:fillViewport="true"
android:layout_marginBottom="15dp"
android:layout_marginTop="15dp"
android:id="#+id/recyclerView_competitive_goods"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:textColor="#color/blacker"
android:textStyle="bold"
android:textSize="18sp"
android:text="Канцелярские товары"
android:layout_marginTop="20dp"
android:layout_marginLeft="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp" />
<android.support.v7.widget.RecyclerView
android:visibility="gone"
android:fillViewport="true"
android:layout_marginBottom="15dp"
android:layout_marginTop="15dp"
android:id="#+id/recyclerView_stationery_goods"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
and :
RecyclerView recycleView1 = (RecyclerView) findViewById(R.id.recyclerView_popular_goods);
RecyclerView recycleView2 = (RecyclerView) findViewById(R.id.recyclerView_competitive_goods);
RecyclerView recycleView3 = (RecyclerView) findViewById(R.id.recyclerView_stationery_goods);
recycleView1.setNestedScrollingEnabled(false);
recycleView2.setNestedScrollingEnabled(false);
recycleView3.setNestedScrollingEnabled(false);
If your devices API is below v 21, use the following:
ViewCompat.setNestedScrollingEnabled(recycleView1, false);
ViewCompat.setNestedScrollingEnabled(recycleView2, false);
ViewCompat.setNestedScrollingEnabled(recycleView3, false);
Your design needs to change. As soon as you're adding RecyclerView with nested scrolling disabled, you should keep in mind that view will have its items created all at once.
It basically means no typical RecyclerView optimization magic when only needed amount of items is created and rendered, then reused, based on screen real estate etc.
In your case the situation is even less good as you have 3 such RecyclerViews. The performance of your solution will of course depend on hardware and number of items inside each RecyclerView, but pls do consider using just ONE RecyclerView and no ScrollView at all.
Finally I achieved.I used NestedScrollView as root.And used only 1 recyclerview.And in adapter of this recyclerview I created other recyclerViews.Scrolling smooth Thanks #Venky
I have a full screen grid layout with 8 rows of 7 ImageView.
I need all the rows centered without spaces between them (now I have as result the image I uploaded). The space must be only on the top and on the bottom. I already tried to find a solution on the Internet but I haven't found one.
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<GridLayout
android:id="#+id/gameGrid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:rowCount="8"
android:columnCount="7"
>
<!-- ROW 1-1 -->
<ImageView
android:layout_width="0dp"
android:layout_height="0dp"
android:id="#+id/cellR1-1"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
app:srcCompat="#drawable/dl"
/>
<!-- ROW 1-2 -->
<ImageView
android:layout_width="0dp"
android:layout_height="0dp"
android:id="#+id/cellR1-2"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
app:srcCompat="#drawable/dl"
/>
<!-- ROW ... -->
</GridLayout>
</RelativeLayout>
I tried as solution to set all the ImageViews height same as the width but my code didn't worked:
void setGrid(){
//setting the cell height as the cell widht
int imagWidth = cell[0][0].getLayoutParams().width;
for(int r=0;r<rowCount;r++)
for(int c=0;c<columnCount;c++) {
cell[r][c].getLayoutParams().height = imagWidth;
cell[r][c].requestLayout();
}
}
Image:
It might be helpful to you, use the RecyclerView instead of GridLayout
RecyclerView.LayoutManager recyclerViewLayoutManager = new GridLayoutManager(getApplicationContext(), 7); // 7 means how many column you want you can change according to your requirement.
recyclerView.setLayoutManager(recyclerViewLayoutManager);
Try using RecyclerView with GridLayoutManager like below:
GridLayoutManager layoutManager = new GridLayoutManager(getContext(), 7);
YourAdapter adapter = new YourAdapter();
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
int spacing = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 16,
Objects.requireNonNull(getContext()).getResources().getDisplayMetrics());
recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
#Override
public void getItemOffsets(#NotNull Rect outRect, #NotNull View view, #NotNull RecyclerView parent, #NotNull RecyclerView.State state) {
if (parent.getChildAdapterPosition(view) == Objects.requireNonNull(parent.getAdapter()).getItemCount() - 1) {
outRect.bottom = spacing;
}
if (parent.getChildAdapterPosition(view) == 0) {
outRect.top = spacing;
}
}
});
Other solution is:
Instead of adding padding to both the top and bottom items, You can just add the padding to the top and bottom of your RecyclerView and set the clipToPadding attribute to false.
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingTop="8dp"
android:paddingBottom="8dp" />
I want to add horizontal recyclerview at the bottom of fragment. I followed the tutorial and successfully implemented horizontal recyclerview and cardview but after trying all possible answers,horizontal scrolling is not working.
Here is my code :-
XML :-
<RelativeLayout tools:context="com.AlfaCab.Menuactivtiy"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:layout_marginTop="56dp"
android:id="#+id/Mainlayout"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#color/BgColor"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentBottom="true"
android:background="#drawable/offer_white_box">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Offers"
android:gravity="center"
android:textSize="18sp"
android:textColor="#color/BlackTextColor"
android:textStyle="bold"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="5dp" />
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView_offer"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layoutManager="android.support.v7.widget.LinearLayoutManager"
android:scrollbars="none"/>
<!-- android:orientation="horizontal"
android:scrollbars="horizontal"
app:layoutManager="android.support.v7.widget.LinearLayoutManager"
/> -->
</LinearLayout>
</RelativeLayout>
Here is my offer_cardview.xml / custom layout for recyclerview :-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="wrap_content"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp">
<!-- <!–Offer Start–>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#drawable/offer_white_box">
-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!--Offer Coupon-->
<LinearLayout
android:layout_width="140dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#drawable/round_box"
>
<LinearLayout
android:id="#+id/ll_offer_bg"
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal"
android:background="#drawable/offer_red_box"
android:layout_marginBottom="3dp">
<TextView
android:id="#+id/tv_offer_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Get 30% Cashback"
android:textSize="12sp"
android:textColor="#color/WhiteTextColor"
android:textStyle="bold"
android:layout_gravity="center_vertical"
android:gravity="center"/>
</LinearLayout>
<TextView
android:id="#+id/tv_offer_disc"
android:layout_width="match_parent"
android:layout_height="80dp"
android:text="upto Rs. 300 cashback on Outstation"
android:textSize="10sp"
android:textColor="#color/BlackTextColor"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"/>
</LinearLayout>
<!--Offer Coupon Ends-->
</LinearLayout>
<!--</LinearLayout>-->
<!--Offer Ends-->
</android.support.v7.widget.CardView>
</LinearLayout>
HomeFragment.java :-
//a list to store all the products
List<Offer_Data> offerList;
//the recyclerview
RecyclerView recyclerView;
String appOfferId,appOfferTitle,appOfferDes,status;
In onCreate :-
//getting the recyclerview from xml
recyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerView_offer);
After successfully adding data to offerList :-
//creating recyclerview adapter
Offer_Adapter adapter = new Offer_Adapter(getActivity(), offerList);
//adapter.notifyDataSetChanged();
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();// Notify the adapter
Offer_Adapter.java :- (Recyclervire adapter)
public class Offer_Adapter extends RecyclerView.Adapter<Offer_Adapter.ProductViewHolder> {
int[] myImageList;
//this context we will use to inflate the layout
private Context mCtx;
//we are storing all the products in a list
private List<Offer_Data> offerList;
//getting the context and product list with constructor
public Offer_Adapter(Context mCtx, List<Offer_Data> offerList) {
this.mCtx = mCtx;
this.offerList = offerList;
this.myImageList = new int[]{R.drawable.offer_red_box, R.drawable.offer_megento_box, R.drawable.offer_yellow_box};
}
#Override
public ProductViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//inflating and returning our view holder
LayoutInflater inflater = LayoutInflater.from(mCtx);
View view = inflater.inflate(R.layout.offer_cardview, null);
return new ProductViewHolder(view);
}
#Override
public void onBindViewHolder(ProductViewHolder holder, int position) {
//getting the product of the specified position
Offer_Data product = offerList.get(position);
//binding the data with the viewholder views
holder.tv_offer_title.setText(product.getOffer_title());
holder.tv_offer_disc.setText(product.getOffer_disc());
int random_box = getRandom(myImageList);
holder.ll_offer_bg.setBackgroundResource(random_box);
}
public static int getRandom(int[] array) {
int rnd = new Random().nextInt(array.length);
return array[rnd];
}
#Override
public int getItemCount() {
return offerList.size();
}
class ProductViewHolder extends RecyclerView.ViewHolder {
TextView tv_offer_title, tv_offer_disc;
LinearLayout ll_offer_bg;
public ProductViewHolder(View itemView) {
super(itemView);
tv_offer_title = (TextView) itemView.findViewById(R.id.tv_offer_title);
tv_offer_disc = (TextView) itemView.findViewById(R.id.tv_offer_disc);
ll_offer_bg = (LinearLayout)itemView.findViewById(R.id.ll_offer_bg);
}
}
}
Please, help me to make it scroll horizontally.
LinearLayoutManager linearLayoutManager
= new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(linearLayoutManager);
Found the Problem
Change this line
recyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));
to
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false));
Problem is that , you are passing wrong context to recycler view manager
Should use getActivity() not getContext()
ALso no need to add this line in xml
app:layoutManager="android.support.v7.widget.LinearLayoutManager"
I have a Flow layout that display 4 objects for row, but I need display these 4 objects with a textview below each one of them. But it display 2 images and 2 textview, and junp to another row. I tryed to create a ViewGroup to display them together, but I got the same isue. Maybe if I try to set location of the textView inside of the group It canal work, but I dont know How to do it.
What I got:
What I want:
My XML:
<ScrollView
android:id="#+id/svLikes"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="60dp"
android:layout_marginEnd="14dp"
android:layout_marginStart="14dp"
android:layout_marginTop="48dp"
android:background="#3c4052"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="#+id/btnAdd"
app:layout_constraintVertical_bias="0.0"
tools:layout_constraintBottom_creator="1"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintTop_creator="1">
<com.nex3z.flowlayout.FlowLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="5dp"
android:paddingRight="5dp"
app:childSpacingForLastRow="align"
android:paddingLeft="5dp"
app:rowSpacing="8dp"
android:id="#+id/likesContainer"
>
</com.nex3z.flowlayout.FlowLayout>
</ScrollView>
My code:
ImageView iconLike = new ImageView(Register30.this);
TextView txtLike = new TextView(Register30.this);
iconLike.setImageDrawable(getDrawable(R.drawable.x));
txtLike.setText("Unable");
countLikesAdd++;
removeMessageOrShow();
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(140,130);
lp.setMargins(5,5,5,5);
iconLike.setLayoutParams(lp);
txtLike.setLayoutParams(lp);
txtLike.setTextColor(Color.parseColor("#FFFFFF"));
fieldLike.setText("");
ViewGroup gp = new LinearLayout(Register30.this);
gp.addView(iconLike);
gp.addView(txtLike);
likesContainer.addView(gp);
First Method : Static Way
I think what you need is a drawable image on TextView. The drawable image can be inserted in any position except center. Create a suitable image of required sizes form asset studio and use this line on TextView.
android:drawableTop="#drawable/ic_fb_logo_blue_48dp"
A simple implementation is written in below code.
<android.support.v4.widget.NestedScrollView
android:id="#+id/svLikes"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="60dp"
android:layout_marginEnd="14dp"
android:layout_marginStart="14dp"
android:layout_marginTop="48dp"
android:orientation="vertical">
<com.nex3z.flowlayout.FlowLayout
android:id="#+id/likesContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="5dp"
app:childSpacingForLastRow="align"
app:rowSpacing="8dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="#drawable/ic_fb_logo_blue_48dp"
android:gravity="center"
android:text="#string/facebook"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="#drawable/ic_fb_logo_blue_48dp"
android:gravity="center"
android:text="#string/facebook"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="#drawable/ic_fb_logo_blue_48dp"
android:gravity="center"
android:text="#string/facebook"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="#drawable/ic_fb_logo_blue_48dp"
android:gravity="center"
android:text="#string/facebook"
android:textSize="16sp" />
</com.nex3z.flowlayout.FlowLayout>
</android.support.v4.widget.NestedScrollView>
The output image is below. I have used NestedScrollView Instead of ScrollView.
Second Method : Dynamic
Programmatically a number of views can be generated by using grid layout. My implemenation using recylerview is below
Inside Activity class:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
recyclerOne = (RecyclerView) findViewById(R.id.recyclerView);
initRecyclerView();
//update whenever necessary
adapterOne.updateItems(myList);
}
private void initRecyclerView(){
//Datum is model class
int spanCount = 4; // 4 columns in grid
int spacing = 8;
boolean includeEdge = true;
GridLayoutManager gridLayoutManager = new GridLayoutManager(mContext, spanCount);
recyclerOne.setHasFixedSize(false);
recyclerOne.setLayoutManager(gridLayoutManager);
recyclerOne.addItemDecoration(new GridSpacingItemDecoration(spanCount, spacing, includeEdge));
adapterOne = new RecylcerViewAdapter(new ArrayList<Datum>());
recyclerOne.setAdapter(adapterOne);
}
The GridSpacingItemDecoration class is
public class GridSpacingItemDecoration extends RecyclerView.ItemDecoration {
private int spanCount;
private int spacing;
private boolean includeEdge;
public GridSpacingItemDecoration(int spanCount, int spacing, boolean includeEdge) {
this.spanCount = spanCount;
this.spacing = spacing;
this.includeEdge = includeEdge;
}
#Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
int position = parent.getChildAdapterPosition(view); // item position
int column = position % spanCount; // item column
if (includeEdge) {
outRect.left = spacing - column * spacing / spanCount; // spacing - column * ((1f / spanCount) * spacing)
outRect.right = (column + 1) * spacing / spanCount; // (column + 1) * ((1f / spanCount) * spacing)
if (position < spanCount) { // top edge
outRect.top = spacing;
}
outRect.bottom = spacing; // item bottom
} else {
outRect.left = column * spacing / spanCount; // column * ((1f / spanCount) * spacing)
outRect.right = spacing - (column + 1) * spacing / spanCount; // spacing - (column + 1) * ((1f / spanCount) * spacing)
if (position >= spanCount) {
outRect.top = spacing; // item top
}
}
}
}
Inside recycler view adapter class, during binding view use a single textview with drawable image as in first method and update data. For updating whole list you create following function and call it from activity class:
public void updateItems(List<Datum> data) {
list.clear()
list.addAll(data)
notifyDataSetChanged();
}
Other methods
There is also one other method I know which is done by inflating an item and adding the inflated view on the already created linearlayout(or any other container layouts).
Use whatever methods you like. Enjoy coding!
try this , these is for static data
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="80dp"
android:layout_height="80dp"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView4"
android:layout_width="match_parent"
android:layout_height="60dp"
android:src="#android:drawable/btn_star" />
<TextView
android:id="#+id/textView9"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="TextView" />
</LinearLayout>
<LinearLayout
android:layout_width="80dp"
android:layout_height="80dp"
android:orientation="vertical">
<ImageView
android:id="#+id/img2"
android:layout_width="match_parent"
android:layout_height="60dp"
android:src="#android:drawable/btn_star" />
<TextView
android:id="#+id/tekxjtView9"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="TextView" />
</LinearLayout>
<LinearLayout
android:layout_width="80dp"
android:layout_height="80dp"
android:orientation="vertical">
<ImageView
android:id="#+id/img3"
android:layout_width="match_parent"
android:layout_height="60dp"
android:src="#android:drawable/btn_star" />
<TextView
android:id="#+id/txt3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="TextView" />
</LinearLayout>
</LinearLayout>
for dynamic data use
gridlayout and use the above code as Adapter layout
This is silly problem but I could not find what I am doing wrong.
I am using FirebaseRecyclerAdapter to fill recyclerView but there is unexpected space is coming with items.
Fragment class:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_tags, container, false);
mProgressBar = (ProgressBar)view.findViewById(R.id.progressBar);
mTagsRecyclerView = (RecyclerView) view.findViewById(R.id.tagsRecyclerView);
mLinearLayoutManager = new LinearLayoutManager(getContext());
mFirebaseDatabaseReference = FirebaseDatabase.getInstance().getReference();
mFirebaseAdapter = new FirebaseRecyclerAdapter<Tags, TagsViewHolder>(
Tags.class,
R.layout.item_message,
TagsViewHolder.class,
mFirebaseDatabaseReference.child("tags")) {
#Override
protected void populateViewHolder(TagsViewHolder viewHolder, Tags tags, int position) {
mProgressBar.setVisibility(ProgressBar.INVISIBLE);
viewHolder.tagTextView.setText(tags.getTagname());
viewHolder.usrTextView.setText(tags.getUserid());
}
};
mFirebaseAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
#Override
public void onItemRangeInserted(int positionStart, int itemCount) {
super.onItemRangeInserted(positionStart, itemCount);
}
});
mTagsRecyclerView.setLayoutManager(mLinearLayoutManager);
mTagsRecyclerView.setAdapter(mFirebaseAdapter);
mTagsRecyclerView.addOnItemTouchListener(new RecyclerViewItemClickListener(getContext(),
new RecyclerViewItemClickListener.OnItemClickListener() {
#Override public void onItemClick(View v, int position) {
//TODO:
// do something on item click
Log.d("Item clicked at",Integer.toString(position));
}
}));
return view;
}
Fragment layout:
<FrameLayout
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"
tools:context="com.helpplusapp.amit.helpplus.TagsFragment">
<android.support.v7.widget.RecyclerView
android:id="#+id/tagsRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<ProgressBar
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/progressBar"
android:layout_gravity="center"
/>
List item layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#drawable/view_separator"
android:paddingTop="#dimen/margin_default"
android:paddingBottom="#dimen/margin_default"
android:showDividers="end">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/tagTextView"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/usrTextView"
/>
</LinearLayout>
Please suggest.
In listitemlayout xml file make the height of linear layout to wrap content
In list item layout change the linear layout android:height="wrap_content"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:divider="#drawable/view_separator"
android:paddingTop="#dimen/margin_default"
android:paddingBottom="#dimen/margin_default"
android:showDividers="end">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/tagTextView"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/usrTextView"
/>
</LinearLayout>
With the support library 23.2 google introduced a change to the recyclerview:
LayoutManager API: auto-measurement! This allows a RecyclerView to
size itself based on the size of its contents. This means that
previously unavailable scenarios, such as using WRAP_CONTENT for a
dimension of the RecyclerView, are now possible. You’ll find all built
in LayoutManagers now support auto-measurement. Due to this change,
make sure to double check the layout parameters of your item views:
previously ignored layout parameters (such as MATCH_PARENT in the
scroll direction) will now be fully respected.
Source
You need to change the height of the item to wrap_content instead of match_parent.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#drawable/view_separator"
android:paddingTop="#dimen/margin_default"
android:paddingBottom="#dimen/margin_default"
android:showDividers="end">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/tagTextView"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/usrTextView"
/>
</LinearLayout>