I have a TextView for description and another one for current step " app for
recipes "
When i changed orientation of device to landscape and returning to portrait mode again this problem happen ( text in TextView come like another TextView not same although there is single TextView and this overlapping happen ) see attached images .
Normal view before changing orientation
This when i go landscape and returned to portrait again
Xml file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<com.google.android.exoplayer2.ui.SimpleExoPlayerView
android:id="#+id/playerView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
<ImageView
android:id="#+id/thumbImage"
android:layout_gravity="left"
android:paddingRight="15dp"
android:elevation="1dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/description"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:padding="16dp"
android:textSize="17sp"
android:text="intro"
android:freezesText="true"
android:gravity="center_horizontal"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<android.support.design.widget.FloatingActionButton
android:id="#+id/next_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/fab_margin"
android:src="#drawable/ic_next"
android:layout_alignParentRight="true"
app:fabSize="auto" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/back_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/fab_margin"
android:src="#drawable/ic_back"
app:fabSize="auto" />
<TextView
android:id="#+id/currentStep"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="1/10"
android:textSize="20sp"/>
</RelativeLayout>
</LinearLayout>
Java Code :
public class StepDetailsFragment extends android.app.Fragment implements View.OnClickListener {
ArrayList<Step> steps;
StepsAdapter adapter;
#BindView(R.id.description)
TextView description;
#BindView(R.id.currentStep)
TextView current;
#BindView(R.id.next_button)
FloatingActionButton next;
#BindView(R.id.back_button)
FloatingActionButton back;
FragmentOneListener listener;
public int currentIndex;
#BindView(R.id.playerView)
SimpleExoPlayerView playerView;
SimpleExoPlayer player;
private boolean playWhenReady=true;
private long playbackPosition;
private int currentWindow;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.details_fragment, container, false);
ButterKnife.bind(this, root);
Bundle bundle = getArguments();
if (!bundle.containsKey("steps")) {
return root;
}
steps = bundle.getParcelableArrayList("steps");
currentIndex = bundle.getInt("current");
show();
back.setOnClickListener(this);
next.setOnClickListener(this);
return root;
}
#Override
public void onClick(View v) {
int id = v.getId();
if (id == R.id.back_button) {
currentIndex--;
show();
} else if (id == R.id.next_button) {
currentIndex++;
show();
}
}
public void show() {
if (currentIndex <= 0) {
back.setVisibility(GONE);
} else {
back.setVisibility(View.VISIBLE);
}
if (listener != null) {
listener.setCurrent(currentIndex);
}
if (currentIndex >= steps.size() - 1) {
next.setVisibility(GONE);
} else {
next.setVisibility(View.VISIBLE);
}
description.setText(steps.get(currentIndex).getDescription());
current.setText((currentIndex + 1) + "/" + steps.size());
}
Your activity is recreated on rotation.
The fragment manager stores the original fragment in the instance state, but I assume you are adding a new fragment in the onCreate() of the activity.
So you have the restored and the new fragment on top of each other.
Perhaps only add a new fragment if savedInstanceState is null.
Related
My app has a RawMaterialFragment that displays raw materials data in a recyclerView from room database.
I am trying to build a detail activity(MetrialItemView) to show up the details of an individual raw material by clicking or selecting the raw material from the recyclerView.
my problem is how to send the data from the adapter and how to receive the data in the MetrialItemView Activity and display it.
MaterialListAdapter:
public class MaterialListAdapter extends RecyclerView.Adapter<MaterialListAdapter.ViewHolder> {
private final LayoutInflater mInflater;
private FragmentRawMaterials mContext;
private List<RawMaterialsEntity> mMaterial; // Cached copy of Materials
RawMaterialsEntity mCurrent;
public MaterialListAdapter(FragmentRawMaterials context) {
mInflater = LayoutInflater.from(context.getActivity());
mContext = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = mInflater.inflate(R.layout.list_item, parent, false);
return new ViewHolder(itemView);
}
public class ViewHolder extends RecyclerView.ViewHolder {
private final TextView materialName;
private final TextView materialBrand;
private final TextView materialQuantity;
LinearLayout parentLayout;
private ViewHolder(View itemView) {
super(itemView);
materialName = itemView.findViewById(R.id.raw_material_name);
materialBrand = itemView.findViewById(R.id.raw_material_brand);
materialQuantity = itemView.findViewById(R.id.raw_material_quantity);
parentLayout = itemView.findViewById(R.id.parent_layout);
}
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
if (mMaterial != null) {
mCurrent = mMaterial.get(position);
holder.materialName.setText(mCurrent.getRawMaterialName());
holder.materialBrand.setText(mCurrent.getRawMaterialBrand());
holder.materialQuantity.setText(String.valueOf(mCurrent.getRawMaterialQuantity()));
} else {
// Covers the case of data not being ready yet.
holder.materialName.setText("Name NA");
holder.materialBrand.setText("Brand NA");
holder.materialQuantity.setText("Quantity NA");
}
holder.parentLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(mContext.getContext(), MaterialItemView.class);
mContext.startActivity(intent);
}
});
}
public void setMaterial(List<RawMaterialsEntity> materials){
mMaterial = materials;
notifyDataSetChanged();
}
// getItemCount() is called many times, and when it is first called,
// mWords has not been updated (means initially, it's null, and we can't return null).
#Override
public int getItemCount() {
if (mMaterial != null)
return mMaterial.size();
else return 0;
}
}
MaterialItemView:
public class MaterialItemView extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.material_item_view);
}
}
material_list_item:
<?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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Raw Material Name:"
style="#style/OtherTextViews"/>
<TextView
android:id="#+id/material_name_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/OtherTextViewsBody"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Raw Material Brand:"
style="#style/OtherTextViews"/>
<TextView
android:id="#+id/material_brand_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/OtherTextViewsBody"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Unit Weight:"
style="#style/OtherTextViews"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2000"
style="#style/OtherTextViewsBody"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="gm"
style="#style/OtherTextViewsBody"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Unit Cost:"
style="#style/OtherTextViews"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="50"
style="#style/OtherTextViewsBody"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cost per gm/ml:"
style="#style/OtherTextViews"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0.1"
style="#style/OtherTextViewsBody"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Available Quantity:"
style="#style/OtherTextViews"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1000"
style="#style/OtherTextViewsBody"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Total Cost:"
style="#style/OtherTextViews"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="50000"
style="#style/OtherTextViewsBody"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Supplier Name:"
style="#style/OtherTextViews"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pandah"
style="#style/OtherTextViewsBody"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Supplier Email:"
style="#style/OtherTextViews"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pandah#panadh.com"
style="#style/OtherTextViewsBody"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Supplier Phone:"
style="#style/OtherTextViews"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+966555699517"
style="#style/OtherTextViewsBody"/>
</LinearLayout>
</LinearLayout>
FragmentRawMaterials:
public class FragmentRawMaterials extends Fragment{
private RawMaterialViewModel mMaterialViewModel;
private static final int NEW_MATERIAL_ACTIVITY_REQUEST_CODE = 1;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_raw_materials, container, false);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// Setup any handles to view objects here
//FloatingActionButton fab to insert recipes
TextView emptyViewText = view.findViewById(R.id.empty_raw__materials_view);
FloatingActionButton fab = view.findViewById(R.id.fab_raw_materials);
fab.setOnClickListener(view1 -> {
Intent intent = new Intent(getActivity(), RawMaterialsEditor.class);
startActivityForResult(intent, NEW_MATERIAL_ACTIVITY_REQUEST_CODE);
});
RecyclerView recyclerView = view.findViewById(R.id.recyclerview);
//Decoration to add a line divider between recyclerView items
DividerItemDecoration decoration =
new DividerItemDecoration(Objects.requireNonNull(this.getActivity()),
R.drawable.border_line);
recyclerView.addItemDecoration(decoration);
recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
final MaterialListAdapter adapter = new MaterialListAdapter(this);
recyclerView.setAdapter(adapter);
// Check if adapter list is empty, if so empty text view will appear.
adapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
#Override
public void onChanged() {
super.onChanged();
if (adapter.getItemCount() == 0) {
recyclerView.setVisibility(View.GONE);
emptyViewText.setVisibility(View.VISIBLE);
}
else {
recyclerView.setVisibility(View.VISIBLE);
emptyViewText.setVisibility(View.GONE);
}
}
});
mMaterialViewModel = new ViewModelProvider(this).get(RawMaterialViewModel.class);
// Update the cached copy of the words in the adapter.
mMaterialViewModel.getAllMaterials().observe(this, adapter::setMaterial);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == NEW_MATERIAL_ACTIVITY_REQUEST_CODE && resultCode == RESULT_OK) {
RawMaterialsEntity material = new RawMaterialsEntity(data
.getStringExtra(RawMaterialsEditor.EXTRA_REPLY_NAME),
data.getStringExtra(RawMaterialsEditor.EXTRA_REPLY_BRAND),
Float.valueOf(data.getStringExtra(RawMaterialsEditor.EXTRA_REPLY_WEIGHT)),
Float.valueOf(data.getStringExtra(RawMaterialsEditor.EXTRA_REPLY_COST)),
Integer.valueOf(data.getStringExtra(RawMaterialsEditor.EXTRA_REPLY_QUANTITY)),
data.getStringExtra(RawMaterialsEditor.EXTRA_REPLY_S_NAME),
data.getStringExtra(RawMaterialsEditor.EXTRA_REPLY_S_EMAIL),
data.getStringExtra(RawMaterialsEditor.EXTRA_REPLY_S_PHONE),
data.getStringExtra(RawMaterialsEditor.EXTRA_REPLY_UOM));
mMaterialViewModel.insertMaterial(material);
mMaterialViewModel.costPerGm();
mMaterialViewModel.totalCost();
} else {
Toast.makeText(
Objects.requireNonNull(getActivity()).getApplicationContext(),
R.string.editor_insert_rm_failed,
Toast.LENGTH_LONG).show();
}
}
}
each row of dataModel() must have ID and then use putExtra() whan clicked It happens
holder.parentLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(mContext.getContext(), MaterialItemView.class);
intent.putExtra("ID",mCurrent.getID());
mContext.startActivity(intent);
}
});
and use getIntent() in detailActivity
int id =getIntent().getIntExtra("ID",-1);
and then get a row data in detail activity from database(viewModel)by ID and parse it
here is my problem while make a new recyclerview of reviews it shows as below not take all width
My activity
public class ReviewsActivity extends AppCompatActivity {
private ArrayList<Review> reviews;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reviews);
reviews = new Gson().fromJson(getIntent().getStringExtra("reviews"), new
TypeToken<List<Review>>() {
}.getType());
setUpReviewsList();
}
void setUpReviewsList() {
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rvReviews);
ReviewsRvAdapter mAdapter = new ReviewsRvAdapter(this, reviews, true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(mAdapter);
}
}
My Adapter
public class ReviewsRvAdapter extends RecyclerView.Adapter<ReviewsRvHolder>
{
private ArrayList<Review> reviews;
private Context context;
ReviewsRvHolder holder;
private DatabaseReference mDatabase;
boolean isActivity = false;
// Provide a suitable constructor (depends on the kind of dataset)
public ReviewsRvAdapter(Context context, ArrayList<Review> reviews) {
this.reviews = reviews;
this.context = context;
mDatabase = FirebaseDatabase.getInstance().getReference();
}
public ReviewsRvAdapter(Context context, ArrayList<Review> reviews, boolean isActivity) {
this.reviews = reviews;
this.context = context;
mDatabase = FirebaseDatabase.getInstance().getReference();
this.isActivity = isActivity;
}
/**
* Create new views (invoked by the layout manager)
*
* #param parent
* #param viewType
* #return
*/
#Override
public ReviewsRvHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
View layoutView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item_user_review, null);
// View layoutView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.layout_review_summary, null);
return new ReviewsRvHolder(layoutView);
}
/**
* Replace the contents of a view (invoked by the layout manager)
*
* #param holder
* #param position
*/
#Override
public void onBindViewHolder(ReviewsRvHolder holder, int position) {
final int tempPos = position;
this.holder = holder;
Review review = reviews.get(position);
try {
holder.txtReviewDate.setReferenceTime(Long.parseLong(review.getDate()));
} catch (Exception e) {
e.printStackTrace();
}
if (review.getUser() != null) {
holder.txtUserReviewNum.setText(String.format("%s%s",
String.valueOf(review.getUser().getNumOfReviews()),
context.getString(R.string.txt_reviews)));
if (review.getUser().getUserName() != null)
holder.txtReviewerName.setText(review.getUser().getUserName());
}
holder.txtLikesNum.setText(String.valueOf(review.getLikesNum()));
holder.rbReviewerRate.setRating((float) review.getRating());
holder.exTxtUserReview.setText(review.getReview());
holder.imgBtnMenu.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//ReviewOtherPopUp(tempPos);
}
});
}
// Return the size of your dataset (invoked by the layout manager)
#Override
public int getItemCount() {
if (isActivity) {
return reviews.size();
}
//return the smallest number between 3 and list size
return (3 > reviews.size() ? reviews.size() : 3);
}
}
here is activity_reviews.xml
<?xml version="1.0" encoding="utf-8"?>
<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="com.better.sftani.ReviewsActivity">
<android.support.v7.widget.RecyclerView
android:id="#+id/rvReviews"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
and the list_item_user_review.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:elevation="2dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="4dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp">
<ImageView
android:id="#+id/imgProfile"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center_vertical"
android:background="#color/black"
android:padding="8dp"/>
<LinearLayout
android:id="#+id/reviewer_linearlayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/imgProfile"
android:gravity="center_vertical"
android:orientation="vertical"
android:paddingLeft="16dp">
<TextView
android:id="#+id/txtReviewerName"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="Mohamed Abd El-Sattar"
android:textColor="#color/black"/>
<TextView
android:id="#+id/txtUserReviewNum"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="#string/txt_reviews"/>
</LinearLayout>
<ImageButton
android:id="#+id/imgBtnMenu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="#id/reviewer_linearlayout"
android:layout_toRightOf="#id/reviewer_linearlayout"
android:background="#null"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:src="#drawable/ic_dots"/>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="55dp"
>
<RatingBar
android:id="#+id/rbReviewerRate"
style="#style/Base.Widget.AppCompat.RatingBar.Small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:isIndicator="true"
android:stepSize="1"/>
<com.github.curioustechizen.ago.RelativeTimeTextView
android:id="#+id/txtReviewDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:paddingBottom="4dp"
android:paddingTop="4dp"
android:text="-"
app:relative_time_prefix="Completed "/>
</LinearLayout>
<com.better.sftani.Helpers.ExpandableTextView
android:id="#+id/exTxtUserReview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="16dp"
android:paddingRight="16dp"
/>
<LinearLayout
android:layout_width="121dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="48dp">
<ImageButton
android:id="#+id/imgBtnLikeReview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?selectableItemBackground"
android:gravity="center_vertical"
android:src="#drawable/ic_like"
/>
<TextView
android:id="#+id/txtLikesNum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="4dp"
android:text="0"/>
</LinearLayout>
</LinearLayout>
i tried a lot to make all of the layouts match parent and it still as it appears above and i search about the problem i saw this
it gives it specific width
In your ReviewsRvAdapter class, inside the onCreateViewHolder() method, you have this line of code:
View layoutView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item_user_review, null);
Right now, you're passing null as the parent when inflating the view. This makes it impossible for the created view to "match parent" for the width.
Change that to this instead:
View layoutView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item_user_review, parent, false);
Now you will have a non-null parent when you inflate the view, and so the width will be set correctly.
I am working with recyclerview with viewpager in fragment.
I want to make navigation on bottom of screen.
enter image description here
and when I click each button, each fragment show in the frameLayout.
the problem is happen when I enter into the artist fragment second time.(at first time it show up find as I intended.)
as you can see in this image enter image description here
there is noting shows up in the fragment. and swiping the screen not working properly. It should moving stick to the left or right but it stop at the point where I stop moving my finger.
here is my source.
activity_main_board.xml
<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">
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/navigation"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true">
</FrameLayout>
<LinearLayout
android:id="#+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="7dp"
android:layout_weight="1"
android:orientation="horizontal">
<ImageView
android:id="#+id/imageView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scaleType="fitXY"
app:srcCompat="#drawable/image_background7" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="53dp"
android:layout_weight="1"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/my_board_button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView10"
android:layout_width="30dp"
android:layout_height="30dp"
app:srcCompat="#drawable/ic_action_my" />
<TextView
android:id="#+id/textView34"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MY"
android:textSize="10sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/artwork_board_button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView13"
android:layout_width="30dp"
android:layout_height="30dp"
app:srcCompat="#drawable/ic_action_artwork" />
<TextView
android:id="#+id/textView35"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Artwork"
android:textSize="10sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/artist_board_button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView14"
android:layout_width="30dp"
android:layout_height="30dp"
app:srcCompat="#drawable/ic_action_artist" />
<TextView
android:id="#+id/textView36"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Artist"
android:textSize="10sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/alarm_board_button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView20"
android:layout_width="30dp"
android:layout_height="30dp"
app:srcCompat="#drawable/ic_action_alert" />
<TextView
android:id="#+id/textView40"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Alarm"
android:textSize="10sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/menu_board_button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView19"
android:layout_width="30dp"
android:layout_height="30dp"
app:srcCompat="#drawable/ic_action_menu" />
<TextView
android:id="#+id/textView38"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="전체"
android:textSize="10sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
MainBoradActivity.java
public class MainBoardActivity extends AppCompatActivity implements View.OnClickListener{
private static LinearLayout my_board_button;
private static LinearLayout artwork_board_button;
private static LinearLayout artist_board_button;
private static LinearLayout menu_board_button;
private static LinearLayout alarm_board_button;
private static FrameLayout container;
private static MyBoardFragment myBoardFragment;
private static ArtworkBoardFragment artworkBoardFragment;
private static ArtistBoardFragment artistBoardFragment;
private static AlarmBoardFragment alarmBoardFragment;
private static MenuBoardFragment menuBoardFragment;
private static FragmentManager fragmentManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_board);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
container = (FrameLayout) findViewById(R.id.container);
my_board_button = (LinearLayout) findViewById(R.id.my_board_button);
artwork_board_button = (LinearLayout) findViewById(R.id.artwork_board_button);
artist_board_button = (LinearLayout) findViewById(R.id.artist_board_button);
menu_board_button = (LinearLayout) findViewById(R.id.menu_board_button);
alarm_board_button = (LinearLayout) findViewById(R.id.alarm_board_button);
my_board_button.setOnClickListener(this);
artwork_board_button.setOnClickListener(this);
artist_board_button.setOnClickListener(this);
menu_board_button.setOnClickListener(this);
alarm_board_button.setOnClickListener(this);
myBoardFragment = new MyBoardFragment();
artworkBoardFragment = new ArtworkBoardFragment();
artistBoardFragment = new ArtistBoardFragment();
alarmBoardFragment = new AlarmBoardFragment();
menuBoardFragment = new MenuBoardFragment();
fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().add(R.id.container, myBoardFragment).commitAllowingStateLoss();
}
#Override
public void onClick(View view) {
switch (view.getId()){
case R.id.my_board_button:
FragmentTransaction myTransaction = fragmentManager.beginTransaction();
myTransaction.replace(R.id.container, myBoardFragment);
myTransaction.addToBackStack("0");
myTransaction.commitAllowingStateLoss();
break;
case R.id.artwork_board_button:
FragmentTransaction artworkTransaction = fragmentManager.beginTransaction();
artworkTransaction.replace(R.id.container, artworkBoardFragment);
artworkTransaction.addToBackStack("1");
artworkTransaction.commitAllowingStateLoss();
break;
case R.id.artist_board_button:
FragmentTransaction artistTransaction = fragmentManager.beginTransaction();
artistTransaction.replace(R.id.container, artistBoardFragment);
artistTransaction.addToBackStack("2");
artistTransaction.commitAllowingStateLoss();
break;
case R.id.alarm_board_button:
FragmentTransaction alarmTransaction = fragmentManager.beginTransaction();
alarmTransaction.replace(R.id.container, alarmBoardFragment);
alarmTransaction.addToBackStack("3");
alarmTransaction.commitAllowingStateLoss();
break;
case R.id.menu_board_button:
FragmentTransaction menuTransaction = fragmentManager.beginTransaction();
menuTransaction.replace(R.id.container, menuBoardFragment);
menuTransaction.addToBackStack("4");
menuTransaction.commitAllowingStateLoss();
break;
}
}
}
ArtistBoardFragment.java
public class ArtistBoardFragment extends Fragment {
private static TabLayout artist_tabs;
private static ViewPager artist_container;
private static FragmentPagerAdapter pageAdapter;
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_artist_board, container, false);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
artist_tabs = (TabLayout) view.findViewById(R.id.artist_tabs);
artist_container = (ViewPager) view.findViewById(R.id.artist_container);
pageAdapter = new FragmentPagerAdapter(getActivity().getSupportFragmentManager()) {
ArtistNewFragment artistNewFragment = new ArtistNewFragment();
ArtistCategoryFragment artistCategoryFragment = new ArtistCategoryFragment();
private final String[] menuFragmentNames = new String[]{
"new",
"category"
};
#Override
public Fragment getItem(int position) {
switch (position){
case 0:
Bundle recentBundle = new Bundle();
recentBundle.putInt("page", position);
artistNewFragment.setArguments(recentBundle);
return artistNewFragment;
case 1:
Bundle bestBundle = new Bundle();
bestBundle.putInt("page", position);
artistCategoryFragment.setArguments(bestBundle);
return artistCategoryFragment;
default:
return null;
}
}
#Override
public int getCount() {
return menuFragmentNames.length;
}
#Override
public CharSequence getPageTitle(int position) {
return menuFragmentNames[position];
}
};
artist_container.setAdapter(pageAdapter);
artist_container.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
artist_tabs.setupWithViewPager(artist_container);
}
#Override
public void onResume() {
super.onResume();
}
}
ArtistNewFragment.java
public class ArtistNewFragment extends Fragment {
private static RecyclerView new_content_list;
int pastVisibleItems, visibleItemCount, totalItemCount;
private static TimelineAdapter timelineAdapter;
private static RequestManager requestManager;
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_artist_new_content, container, false);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
new_content_list = (RecyclerView) view.findViewById(R.id.new_content_list);
requestManager = Glide.with(getActivity());
final LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL,false);
new_content_list.setLayoutManager(layoutManager);
new_content_list.setHasFixedSize(true);
new_content_list.setNestedScrollingEnabled(false);
//final NotiAdapter notiAdapter = new NotiAdapter(getApplicationContext(), FunctionBase.createFilter, false, lastCheckTime);
timelineAdapter = new TimelineAdapter(getActivity(), requestManager);
timelineAdapter.setObjectsPerPage(3);
new_content_list.setAdapter(timelineAdapter);
new_content_list.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
Log.d("dx", String.valueOf(dx));
Log.d("dy", String.valueOf(dy));
if(dy > 0) {
visibleItemCount = layoutManager.getChildCount();
totalItemCount = layoutManager.getItemCount();
pastVisibleItems = layoutManager.findFirstVisibleItemPosition();
if ( (visibleItemCount + pastVisibleItems) >= totalItemCount) {
timelineAdapter.loadNextPage();
}
}
}
});
}
#Override
public void onResume() {
super.onResume();
timelineAdapter.loadObjects(0);
}
}
It's hard to tell exactly due to the navigational structure of your app, but think the solution to your problem is to use the childFragmentManager inside of the ArtistBoardFragment. i.e, instead of
pageAdapter = new FragmentPagerAdapter(getActivity().getSupportFragmentManager()) ...
Use
pageAdapter = new FragmentPagerAdapter(getChildFragmentManager()) ...
Before you ask, yes I know there are many questions that are very similar to this and I have tried most of them to no avail. My problem is that a CardView is not displaying within a RecyclerView. The items whithin it are displaying but not the card itself.
Without further or do, here's my code:
Adapter:
Integer count = 0;
Boolean isStart = true;
String datag = "";
String typeg = "";
Integer LastItemType=0; //0=None 1=Text 2=Image
ViewHolder a;
#Override
public EntryAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
a= createholder(parent, viewType);
return(a);
}
public ViewHolder createholder(ViewGroup parent, int viewtype) {
if (typeg.equals("image")) {
View root = LayoutInflater.from(parent.getContext())
.inflate(R.layout.listitems, parent, false);
CardView card = (CardView) root.findViewById(R.id.card_view);
ImageView image = (ImageView) root.findViewById(R.id.Img);
((ViewGroup)image.getParent()).removeView(root.findViewById(R.id.card_view));
if (LastItemType == 2) {
((ViewGroup)image.getParent()).removeView(root.findViewById(R.id.Img));
}
if (LastItemType == 1) {
((ViewGroup)image.getParent()).removeView(root.findViewById(R.id.Txt));
}
ViewHolder vh = new ViewHolder(image, card);
LastItemType = 2;
return vh;
} else {
if (typeg.equals("text")) {
View root = LayoutInflater.from(parent.getContext())
.inflate(R.layout.listitems, parent, false);
CardView card = (CardView) root.findViewById(R.id.card_view);
TextView image = (TextView) root.findViewById(R.id.Txt);
((ViewGroup)image.getParent()).removeView(root.findViewById(R.id.card_view));
if (LastItemType == 1) {
((ViewGroup)image.getParent()).removeView(root.findViewById(R.id.Txt));
}
if (LastItemType == 2) {
((ViewGroup)image.getParent()).removeView(root.findViewById(R.id.Img));
}
ViewHolder vh = new ViewHolder(image, card);
LastItemType = 1;
return vh;
}
return null; //TODO: REMOVE!
}
}
#Override
public void onBindViewHolder(EntryAdapter.ViewHolder holder, int position) {
// Deal with data
}
public class ViewHolder extends RecyclerView.ViewHolder {
public ImageView imgg;
public TextView txtg;
public CardView cardg;
public ViewHolder(ImageView image, CardView card) {
super(image);
imgg = image;
cardg = card;
}
public ViewHolder(TextView text, CardView card) {
super(text);
txtg = text;
cardg = card;
}
}
#Override
public int getItemCount() {
return count;
}
public void refresh(String data, String type, ViewGroup parent) {
isStart = false;
datag = data;
typeg = type;
count++;
createholder(parent, -100);
}
listitems.xml:
<?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.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view"
android:layout_gravity="center"
android:layout_width="200dp"
android:layout_height="200dp"
card_view:cardCornerRadius="4dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
>
<ImageView
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:layout_width="wrap_content"
android:layout_margin="10dp"
android:id="#+id/Img"
android:scaleType="center"
android:maxHeight="100dp"
android:maxWidth="150dp"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="0.25"
android:id="#+id/Txt"
android:layout_gravity="center_horizontal|center_vertical"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
Thanks in advance!
Check out my code, i use it for each of my app after modifying it slightly. You can also use adapter if you have more than one RecyclerViews by changing it's type field and layout and views accordingly. It contains a CoordinatorLayout that has CollapsingLayout with ImageView in it. It contains many layouts for Material Design, i hope it helps.
Activity contains RecyclerView and CardView, i removed things like database, Floating action buttons that you may not need and may make it more difficult to understand. If you need the whole class contact me.
public class MeasureListActivity extends AppCompatActivity
implements MeasureListAdapter.OnRecyclerViewMeasureClickListener {
// Views
private RecyclerView mRecyclerView;
private MeasureListAdapter mAdapter;
private Toolbar toolbar;
// List that keeps values displayed on the screen
private List<Measure> listMeasure;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_prev_measures);
setViews();
}
private void setViews() {
/*
* Set toolbar and arrow icon to return back
*/
toolbar = (Toolbar) findViewById(R.id.toolbarPrevMeasure);
setSupportActionBar(toolbar);
// Enable home button for API < 14
getSupportActionBar().setHomeButtonEnabled(true);
// Enable home button for API >= 14
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
/*
* RecylerView to display items as a list
*/
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerViewPrevMeasure);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mAdapter = new MeasureListAdapter(this, listMeasure, 0);
// Attach an instance of OnRecyclerViewMeasureClickListener that
// implements itemClicked()
mAdapter.setClickListener(this);
mRecyclerView.setAdapter(mAdapter);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void itemMeasureClicked(View view, int position) {
}
}
public class MeasureListAdapter extends RecyclerView.Adapter<MeasureListAdapter.MyViewHolder> {
private LayoutInflater inflater;
private List<Measure> data = Collections.emptyList();
// This is for delegating event from adapter's onClick() method to
// NavigationDrawerFragment
private OnRecyclerViewMeasureClickListener recyclerClickListener;
private DecimalFormat decimalFormat;
private int type = 0;
private Context mContext;
public MeasureListAdapter(Context context, List<Measure> data, int type) {
mContext = context;
inflater = LayoutInflater.from(context);
this.data = data;
decimalFormat = new DecimalFormat("###.#");
this.type = type;
}
#Override
public int getItemCount() {
return data.size();
}
#Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
Measure measure = data.get(position);
String title = measure.getTitle();
String note = measure.getNote();
String date = measure.getFormattedDate();
double angle = measure.getAnglePhoto();
// Compass
double azimuth = measure.getAngleAzimuth();
double pitch = measure.getAnglePitch();
double roll = measure.getAngleRoll();
String bearing = measure.getBearing();
holder.tvTitle.setText(title);
holder.tvNote.setText(note);
holder.tvAngle.setText(
mContext.getString(R.string.angle) + ": " + decimalFormat.format(angle) + ConstantsApp.DEGREE_ICON);
// Compass
holder.tvAzimuth.setText(
mContext.getString(R.string.azimuth) + ": " + decimalFormat.format(azimuth) + ConstantsApp.DEGREE_ICON);
holder.tvPitch.setText(
mContext.getString(R.string.pitch) + ": " + decimalFormat.format(pitch) + ConstantsApp.DEGREE_ICON);
holder.tvRoll.setText(
mContext.getString(R.string.roll) + ": " + decimalFormat.format(roll) + ConstantsApp.DEGREE_ICON);
holder.tvBearing.setText(mContext.getString(R.string.bearing) + " " + bearing);
holder.tvDate.setText("Date" + ": " + measure.getFormattedDate());
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int arg1) {
View view = null;
view = inflater.inflate(R.layout.custom_row_angle_photo, parent, false);
MyViewHolder viewHolder = new MyViewHolder(view);
return viewHolder;
}
/**
* get an instance of OnRecyclerViewClickListener interface
*
* #param OnRecyclerViewMeasureClickListener
* callback that is used by adapter to invoke the method of the
* class implements the OnRecyclerViewClickListener interface
*/
public void setClickListener(OnRecyclerViewMeasureClickListener recyclerClickListener) {
this.recyclerClickListener = recyclerClickListener;
}
public void delete(int position) {
data.remove(position);
notifyItemRemoved(position);
}
class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
// Views
private TextView tvTitle, tvNote, tvAngle, tvAzimuth, tvPitch, tvRoll, tvBearing, tvDate;
public MyViewHolder(View itemView) {
super(itemView);
tvTitle = (TextView) itemView.findViewById(R.id.tvDisplayTitle);
tvAngle = (TextView) itemView.findViewById(R.id.tvDisplayAngle);
// Compass
tvAzimuth = (TextView) itemView.findViewById(R.id.tvDisplayAzimuth);
tvPitch = (TextView) itemView.findViewById(R.id.tvDisplayPitch);
tvRoll = (TextView) itemView.findViewById(R.id.tvDisplayRoll);
tvBearing = (TextView) itemView.findViewById(R.id.tvDisplayBearing);
tvNote = (TextView) itemView.findViewById(R.id.tvDisplayNote);
tvDate = (TextView) itemView.findViewById(R.id.tvDisplayDate);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (recyclerClickListener != null) {
recyclerClickListener.itemMeasureClicked(v, getLayoutPosition());
}
}
}
/**
* RecyclerViewClickListener interface helps user to set a clickListener to
* the RecyclerView. By setting this listener, any item of Recycler View can
* respond to any interaction.
*
* #author Fatih
*
*/
public interface OnRecyclerViewMeasureClickListener {
/**
* This is a callback method that be overriden by the class that
* implements this interface
*/
public void itemMeasureClicked(View view, int position);
}
}
Measure class only contains setters and getters for int and String values so i don't put it.
Layout for Activity
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/layoutMainMeasure"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:background="#android:color/background_light"
android:paddingBottom="50dp" >
<android.support.design.widget.AppBarLayout
android:id="#+id/appbarPrevMeasure"
android:layout_width="match_parent"
android:layout_height="200dp"
android:fitsSystemWindows="true"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar" >
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsingToolbarPrevMeasure"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|enterAlwaysCollapsed"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp" >
<ImageView
android:id="#+id/backgroundPrevMeasure"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_collapseMode="parallax"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
android:src="#drawable/bg_material" />
<android.support.v7.widget.Toolbar
android:id="#+id/toolbarPrevMeasure"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerViewPrevMeasure"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:background="#eeeeee"
android:paddingTop="12dp" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fabLog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="80dp"
app:layout_anchor="#id/appbarPrevMeasure"
app:layout_anchorGravity="bottom|right|end"
android:src="#drawable/ic_save_white_36dp"
android:tint="#android:color/white"
app:backgroundTint="#FFA500" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fabClearDB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/activity_horizontal_margin"
app:layout_anchor="#id/appbarPrevMeasure"
app:layout_anchorGravity="bottom|right|end"
android:src="#drawable/ic_delete_white_36dp"
android:tint="#android:color/white"
app:backgroundTint="#D463C3" />
</android.support.design.widget.CoordinatorLayout>
Layout for adapter rows with CardView
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:cardview="http://schemas.android.com/apk/res-auto"
android:id="#+id/cardRecord"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="#eeeeee"
cardview:cardCornerRadius="5dp"
cardview:cardElevation="5dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="8dp" >
<TextView
android:id="#+id/tvDisplayTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:textColor="#FF0000" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/tvDisplayAngle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:text="#string/angle_"
android:textColor="#525657"
android:textSize="18sp" />
<TextView
android:id="#+id/tvDisplayAzimuth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:text="#string/angle_"
android:textColor="#525657"
android:textSize="14sp" />
<TextView
android:id="#+id/tvDisplayBearing"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:text="#string/angle_"
android:textColor="#525657"
android:textSize="14sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/tvDisplayPitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="12dp"
android:text="#string/angle_"
android:textColor="#525657"
android:textSize="14sp" />
<TextView
android:id="#+id/tvDisplayRoll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="12dp"
android:text="#string/angle_"
android:textColor="#525657"
android:textSize="14sp" />
</LinearLayout>
<TextView
android:id="#+id/tvDisplayDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:text="#string/date_"
android:textColor="#9EA9AD"
android:textSize="14sp" />
<TextView
android:id="#+id/tvDisplayNote"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:text=""
android:textColor="#828A8C"
android:textSize="14sp" />
</LinearLayout>
How it looks
Does your RecyclerView show ImageView or TextView only?
If yes, because your ViewHolder Constructor is wrong. You have to call super(itemView) in your ViewHolder's Constructor, itemView is the view which is displayed as a row in RecyclerView. To fix your issue, I think you should change your code as below:
public ViewHolder createholder(ViewGroup parent, int viewtype) {
if (typeg.equals("image") || typeg.equals("text")) {
View root = LayoutInflater.from(parent.getContext())
.inflate(R.layout.listitems, parent, false);
return new ViewHolder(root, typeg.equals("text"));
}
//FIXME: As my expericence you should NOT return null for ViewHolder. You have to sure the typeg is one of "image" or "text". I think you should change typeg to Boolean variable to not return null ViewHolder.
return null;
}
public class ViewHolder extends RecyclerView.ViewHolder {
public ImageView imgg;
public TextView txtg;
public CardView cardg;
public ViewHolder(View itemView, boolean isTypeText) {
super(itemView);
imgg = (ImageView) itemView.findViewById(R.id.Img);
cardg = (CardView) itemView.findViewById(R.id.card_view);
txtg = (TextView) itemView.findViewById(R.id.Txt);
imgg.setVisibility(isTypeText ? View.GONE : View.VISIBLE);
txtg.setVisibility(isTypeText ? View.VISIBLE : View.GONE);
}
}
Here is the proper Documentation Read and And Follow it.
You must be Doing Something Wrong in Gradle.!
so, the issue was Your XML was not showing CardView in it here we go.!
Add following Dependencies.!
compile 'com.android.support:design:25.3.1'
compile 'com.android.support:support-v4:25.3.1'
compile 'com.android.support:cardview-v7:25.3.1'
compile 'com.android.support:recyclerview-v7:25.3.1'
compile 'com.android.support:appcompat-v7:25.3.1'
testCompile 'junit:junit:4.12'
Here is Your XML is Working Perfectly Fine.!
<?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.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view"
android:layout_gravity="center"
android:layout_width="200dp"
android:layout_height="200dp"
card_view:cardCornerRadius="4dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
>
<ImageView
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:layout_width="wrap_content"
android:layout_margin="10dp"
android:id="#+id/Img"
android:scaleType="center"
android:maxHeight="100dp"
android:maxWidth="150dp"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="0.25"
android:id="#+id/Txt"
android:layout_gravity="center_horizontal|center_vertical"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
Now Clean and Rebuild Your Project.!
ok try this its Working Fine.now.!
Is it because you are removing card_view like so?
((ViewGroup)image.getParent()).removeView(root.findViewById(R.id.card_view));
In my app, I have requirement of Multi Payne layout. In this layout, the first fragment is a ListView which shows the list of items. On click of the list item, a detail view will open up on the right hand side of the list item. But, in my case, when I run my app on tablet, the detail view appears along with ListView by default. While, I want that it should appear on click of the list item.
Below is my code:
Activity Class:
public class OrderActivity extends FragmentActivity implements
OnOrderSelectedListener {
private static final String TAG = "OrderActivity";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate called");
setContentView(R.layout.order_details);
if (findViewById(R.id.fragment_container) != null) {
if (savedInstanceState != null) {
return;
}
OrderListFragment orderListFragment = new OrderListFragment();
orderListFragment.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, orderListFragment).commit();
}
}
#Override
public void onOrderSelected(int position) {
Log.d(TAG, "onOrderSelected called");
OrderDetailFragment detailsFrag = (OrderDetailFragment) getSupportFragmentManager()
.findFragmentById(R.id.order_detail_fragment);
if (detailsFrag != null) {
if (!detailsFrag.isVisible()) {
detailsFrag.setUserVisibleHint(true);
detailsFrag.updateOrderView(position);
}
} else {
OrderDetailFragment newFragment = new OrderDetailFragment();
Bundle args = new Bundle();
args.putInt(OrderDetailFragment.ARG_POSITION, position);
newFragment.setArguments(args);;
FragmentTransaction transaction = getSupportFragmentManager()
.beginTransaction();
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
transaction.commit();
}
}
}
LIst Fragment
public class OrderListFragment extends ListFragment {
private static final String TAG = "OrderListFragment";
OnOrderSelectedListener mOnOrderSelectedListener;
public interface OnOrderSelectedListener {
public void onOrderSelected(int position);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate called");
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d(TAG, "onCreateView called");
return inflater.inflate(R.layout.order_list, null);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.d(TAG, "onActivityCreated called");
ArrayList<Data> mDataList = new ArrayList<Data>();
Data mData1 = new Data("1", "11001", "08/07/2013", "GAUGE_RUN",
"Dispatched", "Terminal:", "Rail Terminal:", "New York",
"Washington DC");
Data mData2 = new Data("1", "11002", "08/07/2013", "GAUGE_RUN",
"Dispatched", "Terminal:", "Rail Terminal:", "New York",
"Washington DC");
mDataList.add(mData1);
mDataList.add(mData2);
setListAdapter(new OrderAdapter(getActivity(),
R.layout.order_list_item, mDataList));
}
#Override
public void onStart() {
super.onStart();
Log.d(TAG, "onStart called");
if (getFragmentManager().findFragmentById(R.id.order_detail_fragment) != null) {
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
}
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
Log.d(TAG, "onAttach called");
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception.
try {
mOnOrderSelectedListener = (OnOrderSelectedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnHeadlineSelectedListener");
}
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
Log.d(TAG, "onListItemClicked");
mOnOrderSelectedListener.onOrderSelected(position);
getListView().setItemChecked(position, true);
}
}
Detail Fragment
public class OrderDetailFragment extends Fragment {
public final static String ARG_POSITION = "position";
int mCurrentPosition = -1;
private static final String TAG = "OrderDetailFragment";
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d(TAG, "onCreateView called");
if (savedInstanceState != null) {
mCurrentPosition = savedInstanceState.getInt(ARG_POSITION);
}
return inflater.inflate(R.layout.acceptance_details, container, false);
}
#Override
public void onStart() {
Log.d(TAG, "onStart called");
super.onStart();
Bundle args = getArguments();
if (args != null) {
updateOrderView(args.getInt(ARG_POSITION));
} else if (mCurrentPosition != -1) {
updateOrderView(mCurrentPosition);
}
}
public void updateOrderView(int position) {
Log.d(TAG, "updateOrderView called");
/*
* TextView article = (TextView)
* getActivity().findViewById(R.id.article);
* article.setText(Ipsum.Articles[position]); btnNext = (Button)
* getActivity().findViewById(R.id.btnNext);
* btnNext.setOnClickListener(this); mCurrentPosition = position;
*/
}
#Override
public void onSaveInstanceState(Bundle outState) {
Log.d(TAG, "onSaveInstanceState called");
super.onSaveInstanceState(outState);
outState.putInt(ARG_POSITION, mCurrentPosition);
}
}
Activity 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="match_parent"
android:background="#android:color/white"
android:orientation="horizontal" >
<com.dzo.dispatchcrude.driverapp.ui.HeaderBar
android:id="#+id/headerBar"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_alignParentTop="true" >
</com.dzo.dispatchcrude.driverapp.ui.HeaderBar>
<LinearLayout
android:id="#+id/linOrderView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/headerBar"
android:baselineAligned="false"
android:orientation="horizontal" >
<fragment
android:id="#+id/order_list_fragment"
android:name="com.dzo.dispatchcrude.driverapp.ui.OrderListFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<fragment
android:id="#+id/order_detail_fragment"
android:name="com.dzo.dispatchcrude.driverapp.ui.OrderDetailFragment"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="30dp"
android:layout_weight="2" />
</LinearLayout>
</RelativeLayout>
List Fragment 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"
android:orientation="vertical" >
<ListView
android:id="#id/android:list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:dividerHeight="1dp" />
</LinearLayout>
Order Detail Fragment
<?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"
android:layout_margin="20dp"
android:background="#drawable/corner_shape"
android:orientation="vertical" >
<TextView
android:id="#+id/txtHeaderType"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_marginBottom="-5dp"
android:background="#drawable/upper_corner"
android:gravity="center"
android:text="#string/acceptance_details"
android:textColor="#android:color/white"
android:textSize="30sp"
android:textStyle="bold" />
<TextView
android:id="#+id/txtHeaderSource"
android:layout_width="match_parent"
android:layout_height="70dp"
android:background="#drawable/rectangle"
android:gravity="center"
android:textSize="25sp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:gravity="center_vertical"
android:orientation="horizontal" >
<TextView
android:id="#+id/txtTruck"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical"
android:paddingLeft="15dp"
android:text="#string/truck"
android:textColor="#color/text_color"
android:textSize="20sp"
android:typeface="sans" />
<Spinner
android:id="#+id/truckSpinner"
style="#android:style/Widget.Spinner"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_margin="10dp"
android:layout_weight="3"
android:background="#drawable/bg_edit_text"
android:gravity="center"
android:spinnerMode="dropdown" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/divider_color" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:gravity="center_vertical"
android:orientation="horizontal" >
<TextView
android:id="#+id/txtTrailor"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical"
android:paddingLeft="15dp"
android:text="#string/trailor"
android:textColor="#color/text_color"
android:textSize="20sp" />
<Spinner
android:id="#+id/trailorSpinner"
style="#android:style/Widget.Spinner"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_margin="10dp"
android:layout_weight="3"
android:background="#drawable/bg_edit_text"
android:gravity="center"
android:spinnerMode="dropdown" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/divider_color" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:gravity="center_vertical"
android:orientation="horizontal" >
<TextView
android:id="#+id/txtTrailor2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical"
android:paddingLeft="15dp"
android:text="#string/trailor2"
android:textColor="#color/text_color"
android:textSize="20sp" />
<Spinner
android:id="#+id/trailor2Spinner"
style="#android:style/Widget.Spinner"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_margin="10dp"
android:layout_weight="3"
android:background="#drawable/bg_edit_text"
android:gravity="center"
android:spinnerMode="dropdown" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/divider_color" />
<Button
android:id="#+id/btnAccept"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_marginBottom="25dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="25dp"
android:background="#drawable/input_button"
android:gravity="center"
android:text="#string/accept"
android:textColor="#android:color/white"
android:textSize="30sp"
android:textStyle="bold" />
</LinearLayout>
I did gone through Fragments Basic Demo given on the Android developer's site, but couldn't figure out my mistake.
try this
listview.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
listview.setItemChecked(0,false);