How to add layout.xml to another layout.xml programmatically - java

I have Fragment that contain Linear layout with orientation "horizontal" (ItemViewContainer).
I'm created another layout that container imageView and textView (ItemView).
I want to add the ItemView to the ItemViewContainer programmatically a few times.
How can i do that instead of adding the ItemView with from ItemViewContainer.
ItemView
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/LLTopServiceRootView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:id="#+id/CVServiceIMGHolder"
android:layout_width="50dp"
android:layout_height="50dp"
android:translationZ="5dp"
card_view:cardBackgroundColor="#color/fragment_background_color"
card_view:cardCornerRadius="100dp"
card_view:elevation="5dp">
<ImageView
android:id="#+id/IVServiceIMG"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:tint="#color/text_color"
card_view:srcCompat="#drawable/lights_icon" />
</androidx.cardview.widget.CardView>
<TextView
android:id="#+id/TVServiceName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/text_color"
android:layout_gravity="center_horizontal"
android:text="Service\nName"
android:textAlignment="center" />
</LinearLayout>
ItemViewContainer
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="15dp"
android:orientation="vertical"
android:translationZ="5dp"
app:cardBackgroundColor="#color/fragment_background_color"
app:cardCornerRadius="5dp"
card_view:elevation="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
android:id="#+id/LLTopServiceContainer"
</LinearLayout>
</androidx.cardview.widget.CardView>
Fragment that contain layout with the ItemViewContainer:
public class HomeFragment extends Fragment {
private View view;
private TextView mTVPageTitle, mTVHomeNavigationTXT;
private LottieAnimationView mLAVHome;
private ButtonsManager bm;
private LinearLayout mLLTopServiceContainer;
private final String TAG = "HomeLifeCycle";
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_home, container, false);
bm = new ButtonsManager(getContext());
initView(view);
Log.d(TAG, "onCreateView: ");
return view;
}
private void initView(View view) {
if (getActivity() != null) {
mLAVHome = getActivity().findViewById(R.id.LAVHome);
mTVPageTitle = getActivity().findViewById(R.id.TVPageTitle);
mTVHomeNavigationTXT = getActivity().findViewById(R.id.TVHome);
mLLTopServiceContainer = view.findViewById(R.id.LLTopServiceContainer);
addSelectedIconStyle();
}
}
}
I want to add the itemView 4th times

You could do it dynamically like this way : Simple Android RecyclerView example
Or adding manually:
LayoutInflater inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout linearLayout= (LinearLayout) findViewById(R.id.LLTopServiceContainer);
View view = (View) inflater.inflate(R.layout.item:view_name_file, linearLayout, true);

Related

How to display ListItem in Fragment

I want to create Fragment with ListView, so, I have a ListView with listitem in my Fragment activity, but my listitem is not displayed when i open my fragment, i need help with it, how display items in fragment? When i make this ListView in Activity, all is working well, how fix this? My code here:
FollowFragment.java
public class FollowFragment extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
getActivity().setTitle("Подписки");
return inflater.inflate(R.layout.fragment_follow,container,false);
}
}
follow_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
android:padding="10dp">
<ListView
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:listitem="#layout/item_follow"/>
</FrameLayout>
item_follow.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView 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"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/favourite_match_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="8dp">
<androidx.appcompat.widget.AppCompatTextView
android:id="#+id/team_1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:fontFamily="#font/roboto_medium"
android:text="FC Chelsea"
android:textColor="#color/black"
tools:ignore="MissingConstraints" />
<androidx.appcompat.widget.AppCompatTextView
android:id="#+id/team_2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:fontFamily="#font/roboto_medium"
android:text="Manchester City"
app:layout_constraintTop_toBottomOf="#+id/team_1"
android:textColor="#color/black"
tools:ignore="MissingConstraints" />
<androidx.appcompat.widget.AppCompatTextView
android:id="#+id/match_date"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:fontFamily="#font/roboto"
android:text="25 сентября 2021 года"
android:textColor="#color/colorGrey"
android:textSize="12sp"
app:layout_constraintTop_toBottomOf="#id/team_2"
tools:ignore="MissingConstraints" />
<ImageButton
android:layout_width="48dp"
android:layout_height="50dp"
android:layout_marginEnd="24dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:src="#drawable/ic_love"
android:background="#00000000"
app:layout_constraintLeft_toRightOf="#id/team_2"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
well that's because you didn't assign an adapter with the correct data to it yet.
your adapter should extend ArrayAdapter and override getView in which you should set your view with the correct content based on your data.
what an adapter might look like :
public class MyAdapter extends ArrayAdapter<YourDataCustomClassHere> {
ArrayList<YourDataClassHere> data;
Context context ;
public MyAdapter(#NonNull Context context, int resource, #NonNull ArrayList<YourDataCustomClassHere> objects) {
super(context, resource, objects);
this.data = objects;
this.context = context;
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
if (convertView == null) {
convertView = ((LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.item_follow, parent, false);
}
//example on setting one of your fields
((AppCompatTextView) convertView.findViewById(android.R.id.team_1))
.setText(data.get(position).getText());
//set Other Fields here
return convertView;
}
}
where your data class should be a custom class that holds the correct information for each item in the list and getText in data.get(position).getText() is an arbitary method I assumed that will be in your custom class to get some sort of text but you're free to replace it with what you want.
then in your onCreateView you should set the adapter to your ListView like that :
public class FollowFragment extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
getActivity().setTitle("Подписки");
View rootView= inflater.inflate(R.layout.fragment_follow,container,false);
ListView list = rootView.findViewById(R.id.listview);
MyAdapter myadapter = new MyAdapter(getContext(),R.layout.item_follow,YourDataArrayListHere);
list.setAdapter(myadapter);
return rootView;
}
}
I trust that you'll find your way and replace YourDataArrayListHere with the correct ArrayList of your data custom class.
anyway I hope you reconsider your choice of ListView as it consume much Ram (Memory) during runtime and use a RecyclerView instead

Fragment viewpager No adapter attached; skipping layout

I tried a lot of solutions but no solution has worked for me.
I am getting data from the server and showing it inside a recycleview. The data shows fine but shows this error in the logcat
Access denied finding property "persist.vendor.log.tel_dbg"
E/RecyclerView: No adapter attached; skipping layout
E/RecyclerView: No adapter attached; skipping layout
sometimes when I refresh again and again the recyclerview becomes empty.
I am using viewpager consisting of 2 fragments inside a parent fragment.
this recycleview is using in a nested scrollview
some time data not showing
Code snippet:
homebuyer_adapter_recycler=new homebuyer_adapter_recycle(getActivity(), items);
LinearLayoutManager home = new LinearLayoutManager(getActivity());
home.setOrientation(LinearLayoutManager.VERTICAL);
allitemsgrid.setLayoutManager(home);
allitemsgrid.setAdapter(r);
Here is more about
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent,
final int viewType) {
View v = LayoutInflater.from(context).inflate(R.layout.items_view,parent,
false);
RecyclerView.LayoutParams lp = new
RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
v.setLayoutParams(lp);
ViewHolder viewHolder = new ViewHolder(v);
HERE IS MY item view
<?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="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
app:cardCornerRadius="6dp"
android:layout_marginBottom="3dp"
app:cardBackgroundColor="#color/white">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="150dp">
<com.makeramen.roundedimageview.RoundedImageView
android:layout_width="match_parent"
android:layout_height="150dp"
app:riv_corner_radius="6dp"
android:id="#+id/image_items"
android:scaleType="fitXY"
/>
<com.makeramen.roundedimageview.RoundedImageView
android:layout_width="match_parent"
android:layout_height="150dp"
app:riv_corner_radius="6dp"
android:background="#drawable/blackshade"
android:scaleType="fitXY"/>
<TextView
android:id="#+id/items_all"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="#dimen/_5sdp"
android:textColor="#color/white"
android:fontFamily="sans-serif-smallcaps"
android:paddingLeft="20dp"
android:text="Message" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:text="Timing"
android:padding="#dimen/_5sdp"
android:textColor="#color/white"
android:layout_alignParentBottom="true"
android:fontFamily="#font/mylight"
android:layout_alignParentRight="true"
android:textSize="10dp"
android:id="#+id/shoptimming"/>
</RelativeLayout>
<TextView
android:id="#+id/name_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:alpha="0.6"
android:fontFamily="#font/arimo_bold"
android:paddingLeft="20dp"
android:text="Name"
android:textColor="#color/black"
android:textSize="17dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:orientation="horizontal">
<TextView
android:id="#+id/minumum_order"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_weight="1"
android:drawablePadding="10dp"
android:fontFamily="sans-serif"
android:paddingLeft="20dp"
android:text="Minimum " />
<TextView
android:id="#+id/price_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:fontFamily="sans-serif"
android:gravity="left"
android:paddingLeft="20dp"
android:text="charges"
/>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
Here is my adapter class
public class homebuyer_adapter_recycle extends RecyclerView.Adapter<homebuyer_adapter_recycle.ViewHolder> {
private ArrayList<seller_information> listData;
private LayoutInflater layoutInflater;
int lastpostition=-1;
Context context;
public homebuyer_adapter_recycle(Context aContext, ArrayList<seller_information> listData) {
this.listData = listData;
layoutInflater = LayoutInflater.from(aContext);
context=aContext;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder
(#NonNull ViewGroup parent, final
int viewType) {
View v = LayoutInflater.from(parent.getContext()).
inflate(R.layout.items_view,parent, false);
ViewHolder viewHolder = new ViewHolder(v);
Log.i("inadapter","calling time");
v.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
phone_number_shop = listData.get(viewType).getPhonenumber();
homebuyer.Delivery_charges_shop =
listData.get(viewType).getDiliveryfee();
homebuyer.Shop_name = listData.get(viewType).getShopname();
homebuyer.minimum_order = listData.get(viewType).getMinorder();
//profession=items.get(i).getName();
// Toast.makeText(getActivity(),phn,Toast.LENGTH_SHORT).show();
all_and_cetegory_items items = new all_and_cetegory_items();
Bundle b = new Bundle();
b.putString("phone",homebuyer.phone_number_shop);
items.setArguments(b);
FragmentTransaction fragmentTransaction =
((AppCompatActivity)context).getSupportFragmentManager()
.beginTransaction();
fragmentTransaction.replace(R.id.nav_host_fragment, items);
fragmentTransaction.addToBackStack(null).commit();
}
});
return viewHolder;
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.name.setText(listData.get(position).getShopname());
//
Picasso.with(context).load(listData.get(position)
.getShopimage()).into(holder.shopimage);
Glide.with(context).load(listData.get(position).getShopimage())
.into(holder.shopimage);
holder.dilivery.setText("Rs "+listData.get(position).getDiliveryfee()+"
Delivery fee");
holder.minorder.setText("Rs "+listData.get(position).getMinorder()+" minimum");
holder.items.setText(listData.get(position).getShopmessage());
holder.time.setText("Service Available
"+listData.get(position).getStartingtime()+" to
"+listData.get(position).getEndingtime());
Log.i("inadapter","calling time"+listData.get(position).getShopname());
}
#Override
public int getItemCount() {
return listData.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView name;
TextView dilivery;
TextView items;
TextView minorder;
TextView time;
ImageView shopimage;
public ViewHolder(#NonNull View v) {
super(v);
name = (TextView) v.findViewById(R.id.name_item);
dilivery = (TextView) v.findViewById(R.id.price_item);
minorder = (TextView) v.findViewById(R.id.minumum_order);
items=(TextView)v.findViewById(R.id.items_all);
time=(TextView)v.findViewById(R.id.shoptimming);
shopimage=(ImageView) v.findViewById(R.id.image_items);
}
}
}
In error it is saying that the Adapter for RecylerView is not attached. So, try to add adapter to the layout using:
recyclerView.setAdapter(categoryAdapter);
I am assuming homebuyer_adapter_recycler is your adapter.
According to that, you haven't really set your adapter as the logcat is specifying.
Add the below code instead of allitemsgrid.setAdapter(r);
allitemsgrid.setAdapter(homebuyer_adapter_recycler);
If my assumption or suggestion is wrong, it is okay. It is probably because your question is not very clear and does not provide the necessary details. Please provide more details such as the adapter class code and what is allitemsgrid, homebuyer_adapter_recycler and r.

Native Ad Admob to Custom List Adapter in Android

I'm struggling to find a way to add a native ad within my app on Android.
MainActivity Class:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tab1, container, false);
final String[] numbers = {"one","two", "", "three", "four", "five","six","seven" "eight","nine", "ten" "eleven","twelve", "thirteen"};
Integer[] images = {0,1,2,3,4,5,6,7,8,9,10,11,12,13
};
CustomListAdapter adapter=new CustomListAdapter(this.getActivity(), mobileArray, images);
NativeExpressAdView adView = (NativeExpressAdView) rootView.findViewById(R.id.adView);
AdRequest request = new AdRequest.Builder().build();
adView.loadAd(request);
list=(ListView) rootView.findViewById(R.id.list_view);
list.setAdapter(adapter);
CustomListAdapter Class:
public class CustomListAdapter extends ArrayAdapter<String> {
private final Activity context;
private final String[] numbers;
private final Integer[] imgid;
public CustomListAdapter(Activity context, String[] numbers, Integer[] imgid) {
super(context, R.layout.listv, itemname);
// TODO Auto-generated constructor stub
this.context=context;
this.numbers=numbers;
this.imgid=imgid;
}
public View getView(int position,View view,ViewGroup parent) {
LayoutInflater inflater=context.getLayoutInflater();
View rowView=inflater.inflate(R.layout.listv, null,true);
NativeExpressAdView adView = (NativeExpressAdView) rowView.findViewById(R.id.adView);
TextView txtTitle = (TextView) rowView.findViewById(R.id.number);
ImageView imageView = (ImageView) rowView.findViewById(R.id.imgid);
TextView extratxt = (TextView) rowView.findViewById(R.id.textView1);
txtTitle.setText(numbers[position]);
imageView.setImageResource(imgid[position]);
extratxt.setText("Number: "numbers[position]);
return rowView;
};
}
I want to add the native add within a particular index in array, ideally where it shows "". I'm trying to add the adView into the adapter but unsure how to do this.
Edit:
listv layout:
<?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"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal">
<ImageView
android:id="#+id/imgid"
android:layout_width="60dp"
android:layout_height="60dp"
android:padding="5dp" />
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:padding="2dp"
android:textColor="#4CBE99" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"/>
</LinearLayout>
</LinearLayout>
<com.google.android.gms.ads.NativeExpressAdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="visible"
android:layout_gravity="center"
ads:adSize="345x80"
ads:adUnitId="ca-app-pub-3940256099942544/2793859312"></com.google.android.gms.ads.NativeExpressAdView>
You can try like this,
public class CustomListAdapter extends ArrayAdapter<String> {
private NativeExpressAdView adView;
private NativeExpressAdView getAddView() {
if (adView != null) {
return adView;
}
adView = new NativeExpressAdView(context);
adView.setAdUnitId("[your unit id]");
adView.setAdSize(new AdSize(AdSize.FULL_WIDTH, 80));
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
adView.setLayoutParams(layoutParams);
AdRequest request = new AdRequest.Builder().build();
adView.loadAd(request);
return adView;
}
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.layout_add, null, true);
// NativeExpressAdView adView = (NativeExpressAdView) rowView.findViewById(R.id.adView);
LinearLayout linearLayout = (LinearLayout) rowView.findViewById(R.id.addView);
linearLayout.removeAllViews();
TextView txtTitle = (TextView) rowView.findViewById(R.id.number);
ImageView imageView = (ImageView) rowView.findViewById(R.id.imgid);
TextView extratxt = (TextView) rowView.findViewById(R.id.textView1);
txtTitle.setText(numbers[position]);
imageView.setImageResource(imgid[position]);
extratxt.setText("Number: " + numbers[position]);
if (position == 2) {
linearLayout.addView(getAddView());
linearLayout.setVisibility(View.VISIBLE);
} else {
linearLayout.removeAllViews();
linearLayout.setVisibility(View.GONE);
}
return rowView;
}
;
}
Layour xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
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">
<ImageView
android:id="#+id/imgid"
android:layout_width="60dp"
android:layout_height="60dp"
android:padding="5dp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:padding="2dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#4CBE99"/>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/addView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"></LinearLayout>
</LinearLayout>

Recycler adapter doesn't call onCreateViewHolder

My Fragment
public class PostFragment extends BaseFragment implements PostView {
#Inject
PostPresenter presenter;
private RecyclerView recyclerView;
PostAdapter adapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_post, container, false);
recyclerView = (RecyclerView) v.findViewById(R.id.postFragmentRecycler);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
String s = "word";
ArrayList<String> array = new ArrayList<>();
for(int i = 0; i != 20; i++) {
array.add(s);
}
adapter = new PostAdapter(array);
recyclerView.setAdapter(adapter);
return v;
}
Adapter
public class PostAdapter extends RecyclerView.Adapter {
private ArrayList<String> arrayList;
public PostAdapter(ArrayList<String> arrayList) {
Log.d("ADAPTER", "constructor");
this.arrayList = arrayList;
}
class MyViewHolder extends RecyclerView.ViewHolder {
public View view;
MyViewHolder(View itemView) {
super(itemView);
this.view = itemView;
}
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Log.d("ADAPTER", "CREATE");
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.post_item, parent, false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
Log.d("ADAPTER", "bind");
}
#Override
public int getItemCount() {
Log.d("ADAPTER", "size = " + String.valueOf(arrayList.size()));
return arrayList.size();
}
}
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"
tools:context="exp.glorio.view.fragments.PostFragment">
<android.support.v7.widget.RecyclerView
android:id="#+id/postFragmentRecycler"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
post_item
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_marginTop="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="visible">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_weight="1"
android:orientation="horizontal">
<ImageView
android:id="#+id/postGroupLogo"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginLeft="20dp"
android:layout_gravity="center"/>
<TextView
android:id="#+id/postGroupName"
android:layout_width="180dp"
android:layout_height="30dp"
android:textColor="#color/colorText"
android:textSize="12sp"
android:layout_marginLeft="30dp"
android:layout_gravity="center"
/>
</LinearLayout>
<TextView
android:id="#+id/postText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<LinearLayout
android:id="#+id/postAttachmentsContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
and log:
03-17 17:11:23.753 30538-30538/exp.glorio D/ADAPTER: constructor
03-17 17:11:23.903 30538-30538/exp.glorio D/ADAPTER: size = 20
03-17 17:11:23.903 30538-30538/exp.glorio D/ADAPTER: size = 20
03-17 17:11:23.953 30538-30538/exp.glorio D/ADAPTER: size = 20
03-17 17:11:23.953 30538-30538/exp.glorio D/ADAPTER: size = 20
I have many other adapters in this app and they work. In this case as u can see I don't forget to add LayoutManager and Recycler view not under ScrollView in xml. Constructor and getItemCount works (why 4 times ?) but onBindViewHolder and onCreateViewHolder no.
The problem was in root layout of activity. It was android.support.constraint.ConstraintLayout. In combination with parameter layout_height"match_parent" of container of fragment it's give that problem. If change ConstraintLayout to any other - it will work properly.

ViewPager and Fragments: Why do I always see the same thing?

I have created a ViewPager with three "pages". The code is this
MainActivity.java
public class MainActivity extends FragmentActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
PagerTabStrip pagerTabStrip = (PagerTabStrip) findViewById(R.id.pager_tab_strip);
FragmentPagerAdapter fragmentPagerAdapter = new MyFragmentPagerAdapter(
getSupportFragmentManager());
viewPager.setAdapter(fragmentPagerAdapter);
pagerTabStrip.setDrawFullUnderline(true);
pagerTabStrip.setTabIndicatorColor(Color.DKGRAY);
}
}
MyFragmentPageAdapter.java
public class MyFragmentPagerAdapter extends FragmentPagerAdapter {
private String[] pageTitle = {
"Page1", "Page2", "Page3"
};
public MyFragmentPagerAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
#Override
public Fragment getItem(int position) {
Fragment fragment = new PageFragment();
Bundle arguments = new Bundle();
arguments.putString("pageIndex", Integer.toString(position + 1));
fragment.setArguments(arguments);
return fragment;
}
#Override
public int getCount() {
return pageTitle.length;
}
#Override
public CharSequence getPageTitle(int position) {
return pageTitle[position];
}
}
PageFragment.java
public class PageFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = getActivity().getLayoutInflater().inflate(R.layout.fragment_page, null);
return view;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<android.support.v4.view.PagerTabStrip
android:id="#+id/pager_tab_strip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#33B5E5"
android:textColor="#FFFFFF"
android:paddingTop="10dp"
android:paddingBottom="10dp" />
</android.support.v4.view.ViewPager>
</RelativeLayout>
fragment_page.xml
<?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" >
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:textSize="16sp"
android:textStyle="italic"
android:gravity="center_horizontal"
android:textColor="#color/red"
android:text="#string/inf" />
<TextView
android:id="#+id/textView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="60dp"
android:textSize="28sp"
android:gravity="center_horizontal"
android:textStyle="bold"
android:text="#string/ben" />
<TextView
android:id="#+id/textView3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="130dp"
android:gravity="center_horizontal"
android:textSize="18sp"
android:text="Prova"
/>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_centerInParent="true"
android:text="#string/verifica" />
</RelativeLayout>
But now i visualize in all three pages the same thing. If I for example on page 2 I want a TextView with the text "This is the 2 page" and on the third page a TextView with the text "This is the page 3" and in the first page two TextView with the button ... how can I? I'm going crazy, please let pass me the code to do this thing. Please.
Once you inflate PageFragment's layout you need to get a reference of the TextView so you can display the position on it via the Bundle you are passing using setArguments(). Use your view variable inside onCreateView() to get a reference of the TextView. (i.e. view.findViewById()). Then use getArguments() in your PageFragment to retrieve the Bundle with that has position, and set the TextView to that value.
this is a good example for what you want.
Just create a function in your page fragment class to configure elements and modify onCreateView to attach childs.
public class PageFragment extends Fragment {
TextView tv1 ;
// ...
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = getActivity().getLayoutInflater().inflate(R.layout.fragment_page, null);
/// Attach your childs
tv1 = view.findViewById(R.id.textView1);
return view;
}
public void configure(int position) {
tv1 .setText("This is "+ position);
}
}
Then just call the function configure in getItem function
#Override
public Fragment getItem(int position) {
PageFragment fr = new PageFragment();
fr.configure(position + 1);
return fr;
}

Categories

Resources