I have stuck with this issue more then 3 days. I have used one RecyclerView and on recyclerview's each item have on AutoCompleteTextView. When try to search on autocompletetextview drop down not display. Please do some help that how or where I need to update my code to use atutocomplettextview in every recyclerview items.
I have tried below are the cases:
To show popup manually autoCompleteTextView.showDropDown()
notify adapter when user is typing in autocompletetextview
Every time reset the new adapter once use type on autocompletetextview
I have tried all the option's of android:windowSoftInputMode="" in AndroidMenifest
Checked more then 20+ questions on StackOver
I have tried to set ArrayAdapter from onBindViewHolder() method also
Below are the my code:
This is activity xml file
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="viewModel"
type="com..job..i.viewModel.job.JobViewModel" />
<import type="android.view.View" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/screenBG"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rvSupervisor"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:itemCount="1"
tools:listitem="#layout/job_filter_adapter" />
</androidx.appcompat.widget.LinearLayoutCompat>
</androidx.core.widget.NestedScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
This is adapter class
public class JobFilterAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
public final String tag = JobFilterAdapter.class.getSimpleName();
private final Activity _activity;
private final LayoutInflater inflater;
private final List<StaffInfo> _staffList;
private final List<String> jobList;
public JobFilterAdapter(Activity activity, List<StaffInfo> staffList,
List<String> list) {
jobStaffAdapter = this;
_activity = activity;
inflater = _activity.getLayoutInflater();
_staffList = staffList;
jobList = list;
}
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
return new AdapterHolder(DataBindingUtil.inflate(inflater, R.layout.row_job, parent, false),
new JObWatcher());
}
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder holder, #SuppressLint("RecyclerView") int position) {
AdapterHolder adapterHolder = (AdapterHolder) holder;
StaffInfo model = _staffList.get(holder.getAdapterPosition());
adapterHolder.binding.autoCompleteTxtJob.setText(model.getStaffNo(), false);
adapterHolder.binding.txtStaffName.setText(model.getStaffName());
adapterHolder.jobWatcher.updateState(position, adapterHolder);
}
#Override
public int getItemCount() {
return _staffList.size();
}
class AdapterHolder extends RecyclerView.ViewHolder {
private final RowJobBinding binding;
JObWatcher jobWatcher;
ArrayAdapter jobArrayAdapter;
public AdapterHolder(#NonNull RowJobBinding _binding, JObWatcher _jobWatcher) {
super(_binding.getRoot());
binding = _binding;
jobWatcher = _jobWatcher;
jobArrayAdapter = new ArrayAdapter(_activity, R.layout.row_job_item,jobList);
binding.autoCompleteTxtJob.setThreshold(1);
}
}
public class JObWatcher {
int position;
AdapterHolder holder;
private void updateState(int pos, AdapterHolder viewHolder) {
position = pos;
holder = viewHolder;
manageAutoText();
}
private void manageAutoText() {
holder.binding.autoCompleteTxtJob.setOnFocusChangeListener((view, b) -> {
if (b) {
holder.binding.autoCompleteTxtJob.setAdapter(holder.jobArrayAdapter);
} else {
holder.binding.autoCompleteTxtJob.setAdapter(null);
}
});
}
}
}
This is adapter's item xml file
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/row"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/_5sdp"
android:padding="#dimen/_5sdp">
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="0dp">
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="#dimen/_25sdp"
android:orientation="horizontal">
<androidx.appcompat.widget.AppCompatTextView
style="#style/txtJobDetailRowLeft"
android:text="#string/lbl_job" />
<androidx.appcompat.widget.AppCompatAutoCompleteTextView
android:id="#+id/autoCompleteTxtJob"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="6"
android:background="#drawable/bg_et_first"
android:gravity="center_vertical"
android:inputType="text"
android:paddingLeft="#dimen/_5sdp"
android:singleLine="true"
android:textColor="#color/black"
android:textSize="#dimen/_9sdp" />
</androidx.appcompat.widget.LinearLayoutCompat>
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="#dimen/_25sdp"
android:layout_marginTop="#dimen/_5sdp"
android:orientation="horizontal">
<androidx.appcompat.widget.AppCompatTextView
style="#style/txtJobDetailRowLeft"
android:text="#string/lbl_staff_name" />
<androidx.appcompat.widget.AppCompatTextView
android:id="#+id/txtStaffName"
style="#style/txtJobDetailRowRight" />
</androidx.appcompat.widget.LinearLayoutCompat>
</androidx.appcompat.widget.LinearLayoutCompat>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
Related
Six hours and I cannot figure out what is going on, I have a simplest Adapter and an Activity
Activity
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv_stories_comments1"
android:background="#color/_light_green"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="0dp"
/>
</LinearLayout>
In OnCreate
var RV = FindViewById<AndroidX.RecyclerView.Widget.RecyclerView>(Resource.Id.rv_stories_comments1);
var ad2 = new Stories_Comments_Adapter(comments);
RV.SetAdapter(ad2);
Stories_Comments_Adapter is also as simple as it gets
internal class Comment_ViewHolder : RecyclerView.ViewHolder
{
public TextView username { get; set; }
public Comment_ViewHolder(View itemView) : base(itemView)
{
username = itemView.FindViewById<TextView>(Resource.Id.story_comment_username);
}
}
public class Stories_Comments_Adapter : RecyclerView.Adapter
{
private List<CommentItem> comments;
public Stories_Comments_Adapter(List<CommentItem> comments)
{
this.comments = comments;
}
public override int ItemCount => comments.Count;
public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
Android.Util.Log.Debug("CommentsDebug", "OnBind");
var Current_Item = comments[position];
Comment_ViewHolder viewHolder = holder as Comment_ViewHolder;
viewHolder.username.Text = Current_Item.name;
}
public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
{
Android.Util.Log.Debug("CommentsDebug", "OnCreate");
LayoutInflater inflater = LayoutInflater.From(parent.Context);
View itemView = inflater.Inflate(Resource.Layout.item_story_comment, parent, false);
return new ChatViewHolder(itemView);
}
}
Where 'item_story_comment.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="wrap_content"
android:background="?attr/colorBackground"
android:orientation="vertical">
<TextView
android:id="#+id/story_comment_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/demo_about"
android:textColor="#color/red"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:gravity="center_horizontal"
android:layout_gravity="center_horizontal"
/>
</RelativeLayout>
When I RV.SetAdapter(ad2);, it never binds, I never see any data or see the debug messages
I have so many RecyclerViews in this app and all working fine, I feel like I am making some simple or dumb mistake but cannot figure it out what.
Any idea?
A RecyclerView needs both Adapter and LayoutManager to show up its items.
I am using AleSpero's library to create expandable cardviews for my layout. Works as expected, but now I want to add multiple cardviews in the same fragment layout, dynamically binding to some async list data that loads. How would that be possible?
Followed the demo on the library. Here is how I am adding the cards in the layout:
<LinearLayout
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/watchlist_holder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="20dp"
android:orientation="vertical"
android:clipChildren="false"
android:background="#FAFAFA">
<com.alespero.expandablecardview.ExpandableCardView
android:id="#+id/main_profile_card"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:title="My Watchlist"
app:inner_view="#layout/watchlist_layout"
app:expandOnClick="true" />
</LinearLayout>
#layout/watchlist_layout is a layout I want to repeat, it contain some textView and a recyclerView to show list data. Any help or guidance would be great.
You will need to create different Adapter and Inner Row XML Layout files for each RecyclerView.
For Ex: You inflate a layout in a RecyclerView inside the MainActivity. You are using an Adapter class which is inflating the rows based on your List. Inside onBindViewHolder, you should get the object of the inner RecyclerView which is present in the row layout of the parent view. Once you have the object, create another list and initialize another adapter for the inner recyclerview. Use the new adapter to populate data inside it (similar to the first recyclerview).
Keep in mind, the process remains same, with each recyclerview
Steps:
Create recylcerview inside the layout which is going to display the list
Create a separate row_layout to be inflated in each row based on the number of list data
Crete an Adapter class, which receives data from the parent class and inflates the layout (row_ayout) in the recyclerview
Repeat these steps for N number of Nested RecyclerViews
For the demo, I am attaching sample codes with this answer to help you understand my concept.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recylcerViewParent"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
item_layout_row.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<com.alespero.expandablecardview.ExpandableCardView
android:id="#+id/main_profile_card"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
app:expandOnClick="true"
app:inner_view="#layout/watchlist_inner"
app:title="My Watchlist" />
</RelativeLayout>
item_recycler_view_favorite.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_with_favorites"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/favorites_count"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/margin_small"
android:layout_marginLeft="#dimen/margin_small"
android:textSize="12sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/margin_small"
android:layout_marginEnd="#dimen/margin_small"
android:orientation="horizontal"
android:weightSum="2">
<Button
android:id="#+id/btn_view_details"
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_weight="1"
android:background="#android:color/transparent"
android:gravity="start|center_vertical"
android:padding="5dp"
android:text="Atish"
android:textColor="#color/colorPrimaryDark" />
<Button
android:id="#+id/btn_add_symbols"
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_weight="1"
android:background="#android:color/transparent"
android:gravity="end|center_vertical"
android:padding="5dp"
android:text="Agrawal"
android:textColor="#color/colorPrimaryDark" />
</LinearLayout>
</LinearLayout>
watchlist_inner.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_with_favorites"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/favorites_count"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/margin_small"
android:layout_marginLeft="#dimen/margin_small"
android:textSize="12sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/margin_small"
android:layout_marginEnd="#dimen/margin_small"
android:orientation="horizontal"
android:weightSum="2">
<Button
android:id="#+id/btn_view_details"
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_weight="1"
android:background="#android:color/transparent"
android:gravity="start|center_vertical"
android:padding="5dp"
android:text="VIEW DETAILS"
android:textColor="#color/colorPrimaryDark" />
<Button
android:id="#+id/btn_add_symbols"
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_weight="1"
android:background="#android:color/transparent"
android:gravity="end|center_vertical"
android:padding="5dp"
android:text="ADD SYMBOLS"
android:textColor="#color/colorPrimaryDark" />
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler_view_favorite"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="#dimen/margin_small"
android:layout_marginEnd="#dimen/margin_small"
android:layout_marginBottom="#dimen/margin_small" />
</LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
List<String> stringsList = new ArrayList<>();
RecyclerView recyclerViewLayout;
InnerAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerViewLayout = findViewById(R.id.recylcerViewParent);
//Dummy add 10 objects in the list
for (int i = 0; i < 10; i++) {
stringsList.add(String.valueOf(i));
}
populateRecyclerView();
}
/**
* Create N items in the recycler view
*/
private void populateRecyclerView() {
//Initialize Adapter
recyclerViewLayout.setLayoutManager(new LinearLayoutManager(this));
recyclerViewLayout.setHasFixedSize(false);
adapter = new InnerAdapter(recyclerViewLayout, this);
adapter.setData(this.stringsList);
recyclerViewLayout.setAdapter(adapter);
}
}
InnerAdapter.java
public class InnerAdapter extends RecyclerView.Adapter<InnerAdapter.ViewHolder> {
private List<String> data;
private RecyclerView recyclerView;
private int i = 0;
private Context mContext;
public InnerAdapter(RecyclerView recyclerView, Context context) {
this.recyclerView = recyclerView;
this.mContext = context;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
//Inflater creates rows from a given layout file
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View v = inflater.inflate(R.layout.item_layout_row, parent, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
//Method to perform actions on individual row based on the position. We will get back to this later
//Change the title of the CardView
holder.main_profile_card.setTitle(String.valueOf(position));
//Creating a dummy adapter again to populate the inner recyclerview
List<String> innerData = new ArrayList<>();
for (int i = 0; i < 10; i++) {
innerData.add(String.valueOf(i));
}
//Initialize Inner Adapter
holder.recycler_view_favorite.setLayoutManager(new LinearLayoutManager(mContext));
holder.recycler_view_favorite.setHasFixedSize(false);
InnerFavAdapter adapter = new InnerFavAdapter(holder.recycler_view_favorite, mContext);
adapter.setData(innerData);
holder.recycler_view_favorite.setAdapter(adapter);
}
#Override
public int getItemCount() {
return data.size();
}
public void setData(List<String> data) {
this.data = data;
}
class ViewHolder extends RecyclerView.ViewHolder {
RecyclerView recycler_view_favorite;
ExpandableCardView main_profile_card;
ViewHolder(View itemView) {
super(itemView);
//Get the object of the views from the row layout
main_profile_card = itemView.findViewById(R.id.main_profile_card);
recycler_view_favorite = itemView.findViewById(R.id.recycler_view_favorite);
}
}
}
InnerFavAdapter.java
public class InnerFavAdapter extends RecyclerView.Adapter<InnerFavAdapter.ViewHolder> {
private List<String> data;
private RecyclerView recyclerView;
private int i = 0;
private Context mContext;
public InnerFavAdapter(RecyclerView recyclerView, Context context) {
this.recyclerView = recyclerView;
this.mContext = context;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
//Inflater creates rows from a given layout file
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View v = inflater.inflate(R.layout.inner_recycler_view_favorite, parent, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
//Method to perform actions on individual row based on the position. We will get back to this later
}
#Override
public int getItemCount() {
return data.size();
}
public void setData(List<String> data) {
this.data = data;
}
class ViewHolder extends RecyclerView.ViewHolder {
ViewHolder(View itemView) {
super(itemView);
}
}
}
I will fill view by adapter and when iam fill data to RV then can not scroll.
This is my RC:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/trip_ticket_refresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/trip_ticket_recycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</android.support.v4.widget.SwipeRefreshLayout>
RC is fill by this view:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="#style/TripMyTicketScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:orientation="vertical">
<Button
android:id="#+id/my_ticket_port_and_directions"
android:layout_width="match_parent"
android:layout_height="#dimen/grid_14"
android:layout_marginEnd="#dimen/grid_2"
android:layout_marginLeft="#dimen/grid_2"
android:layout_marginRight="#dimen/grid_2"
android:layout_marginStart="#dimen/grid_2"
android:layout_marginTop="#dimen/grid_1"
android:height="#dimen/grid_5"
android:background="#color/bg_on_board_button"
android:drawableEnd="#drawable/ic_keyboard_arrow_right"
android:drawableLeft="#drawable/ic_rudder_black_32dp"
android:drawablePadding="6dp"
android:drawableRight="#drawable/ic_keyboard_arrow_right"
android:drawableStart="#drawable/ic_rudder_black_32dp"
android:gravity="center|start"
android:padding="#dimen/grid_2"
android:text="#string/my_ticket_port_and_direction"
android:textColor="#color/text_color_my_trip_button"
android:textStyle="bold" />
<include layout="#layout/view_my_trip_time_schedule" />
<include layout="#layout/view_my_trip_passengers_and_cabins" />
<include layout="#layout/view_my_trip_booking_number" />
</LinearLayout>
There are some linear layouts with buttons and textview in <include layout=...
And when view across the screen size then normally start scroll but in this case not.
Adapter is empty for now because i have to solve this problem a them i will continue.
Can you help me?
Thank you.
Update
This is RC init in Fragment
private void initRecyclerView(){
mAdapter = new MyTicketAdapter(getContext());
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
mRecyclerRv.setLayoutManager(mLayoutManager);
mRecyclerRv.setAdapter(mAdapter);
}
Adapter does not fill any data for now. Here is adapter:
public class MyTicketAdapter extends RecyclerView.Adapter<MyTicketAdapter.MyViewHolder> {
private static final int BARCODE_IMAGE_WIDTH = 150;
private static final int BARCODE_IMAGE_HEIGHT = 40;
private Departure mDeparture;
private final Context mContext;
public class MyViewHolder extends RecyclerView.ViewHolder {
//
// #BindView(R.id.my_ticket_booking_number_barcode_ticket)
// ImageView mBarcodeImageView;
//
// #BindView(R.id.my_ticket_booking_number_barcode_booking_number)
// TextView mBookingNumberBelowBarcode;
//
public MyViewHolder(View view) {
super(view);
ButterKnife.bind(this, view);
}
}
/**
* Creates a new instance of this class.
*
* #param context need values from resource.
*/
public MyTicketAdapter(Context context) {
mContext = context;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item_trip_ticket, parent, false);
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(MyTicketAdapter.MyViewHolder holder, int position) {
if (mDeparture == null) {
return;
}
TravelMateLogs.MY_TICKET_ADAPTER.i("--------Departure: " + mDeparture.toString());
// generateBarcode(holder);
}
#Override
public int getItemCount() {
// There is only one item.
if(mDeparture == null){
return 0;
}else {
return 1;
}
}
/**
* Sets the data to be displayed in the list.
*
* #param departure The departure to be displayed in the view. Null will clear the list.
*/
void setData(#Nullable Departure departure) {
mDeparture = departure;
notifyDataSetChanged();
}
}
<android.support.v7.widget.RecyclerView
android:id="#+id/trip_ticket_recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
If is wrap, recyclerview is not visible. Also please post recyclerview initialisation.
Solution:
I had to wrap all part to linearlayout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="#style/TripMyTicketScrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/bg_trip_tab_view"
android:orientation="vertical"
android:paddingBottom="#dimen/grid_14">
<include layout="#layout/view_my_trip_time_schedule" />
<include layout="#layout/view_my_trip_passengers_and_cabins" />
<include layout="#layout/view_my_trip_booking_number" />
</LinearLayout>
</LinearLayout>
Now it is work ...
I'm using 4 cardviews in XML, and they are only loading a picture and a textview each. Not using a RecyclerView or anything dynamic.
My Navigation drawer opens with so much lag and the entire app lags after adding the cardviews. Is there a better way to implement these cardviews?
These are simply nested within a Linearlayout.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/app_bar_main"
android:orientation="vertical"
tools:context="com.example.jesse.apbiologystudyappv2.MainActivity">
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view_1_natural_selection"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
card_view:cardCornerRadius="8dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="96dp"
android:scaleType="centerCrop"
android:src="#drawable/apple_tree" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Natural Selection and Evolution"
android:typeface="normal"
android:layout_centerInParent="true"
android:textAlignment="center"
android:textColor="#color/colorTextIcons"
android:textSize="24dp"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view_2_descent_and_ancestry"
android:layout_gravity="center"
android:layout_marginBottom="16dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="8dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="96dp"
android:scaleType="centerCrop"
android:src="#drawable/gravestones" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Descent and Ancestry"
android:typeface="normal"
android:layout_centerInParent="true"
android:textAlignment="center"
android:textColor="#color/colorTextIcons"
android:textSize="24dp"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view_3_speciation"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_marginBottom="16dp"
android:layout_height="wrap_content"
card_view:cardCornerRadius="8dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="96dp"
android:scaleType="centerCrop"
android:src="#drawable/dinosaur" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Speciation and Extinction"
android:typeface="normal"
android:layout_centerInParent="true"
android:textAlignment="center"
android:textColor="#color/colorTextIcons"
android:textSize="24dp"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view_4_origin_of_life"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
card_view:cardCornerRadius="8dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="96dp"
android:scaleType="centerCrop"
android:src="#drawable/bacteria" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Origin of Life"
android:typeface="normal"
android:layout_centerInParent="true"
android:textAlignment="center"
android:textColor="#color/colorTextIcons"
android:textSize="24dp"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
You should use RecyclerView to manage memory as well. To do this you need only one CardView.
activity_main.xml :
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/app_bar_main"
android:orientation="vertical"
tools:context="com.example.jesse.apbiologystudyappv2.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="#+id/lstActions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="1dp"
android:padding="0dp"/>
</LinearLayout>
cardview_item.xml:
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view_1_natural_selection"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
card_view:cardCornerRadius="8dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="96dp"
android:scaleType="centerCrop"
android:id="#+id/imgActoun" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/txtActoun"
android:typeface="normal"
android:layout_centerInParent="true"
android:textAlignment="center"
android:textColor="#color/colorTextIcons"
android:textSize="24dp"/>
</RelativeLayout>
Create a class ActionItem.java like this:
public class ActionItem
{
private final int mTitleResID;
private final int mIconResID;
public ActionItem(int titleResID, int iconResID)
{
this.mTitleResID = titleResID;
this.mIconResID = iconResID;
}
public int getTitleResID()
{
return mTitleResID;
}
public int getIconResID()
{
return mIconResID;
}
}
onCreateView in MainActivity must be like this :
public View onCreateView(String name, Context context, AttributeSet attrs)
{
List<ActionItem> actionItems = new ArrayList<>();
actionItems.add(new ActionItem(R.string.NaturalSelectionAndEvolution, R.drawable.apple_tree));
actionItems.add(new ActionItem(R.string.DescentAndAncestry, R.drawable.gravestones));
RecyclerView lstActions = (RecyclerView) findViewById(R.id.lstActions);
ActionAdapter actionAdapter = new ActionAdapter(actionItems);
lstActions.setAdapter(actionAdapter);
return super.onCreateView(name, context, attrs);
}
Add ActionAdapter Like this:
public class ActionAdapter extends RecyclerView.Adapter<ActionAdapter.ActionHolder>
{
private final List<ActionItem> mActionItems;
public ActionAdapter(List<ActionItem> actionItems)
{
this.mActionItems = actionItems;
}
#Override
public ActionHolder onCreateViewHolder(ViewGroup viewGroup, int i)
{
LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
View itemView = inflater.inflate(R.layout.cardview_item, viewGroup, false);
return new ActionHolder(itemView);
}
#Override
public void onBindViewHolder(ActionHolder actionHolder, int position)
{
ActionItem actionItem = mActionItems.get(position);
actionHolder.mTxtTitle.setText(actionItem.getTitleResID());
actionHolder.mImgIcon.setImageResource(actionItem.getIconResID());
}
#Override
public int getItemCount()
{
return this.mActionItems.size();
}
public class ActionHolder extends RecyclerView.ViewHolder implements View.OnClickListener
{
protected final TextView mTxtTitle;
protected final ImageView mImgIcon;
public ActionHolder(View view)
{
super(view);
mImgIcon = (ImageView) view.findViewById(R.id.imgAction);
mTxtTitle = (TextView) view.findViewById(R.id.txtAction);
}
}
}
I implemented Fred's answer with a few changes. Instead of putting this
public View onCreateView(String name, Context context, AttributeSet attrs){
List<ActionItem> actionItems = new ArrayList<>();
actionItems.add(new ActionItem(R.string.NaturalSelectionAndEvolution, R.drawable.apple_tree));
actionItems.add(new ActionItem(R.string.DescentAndAncestry, R.drawable.gravestones));
RecyclerView lstActions = (RecyclerView) findViewById(R.id.lstActions);
ActionAdapter actionAdapter = new ActionAdapter(actionItems);
lstActions.setAdapter(actionAdapter);
return super.onCreateView(name, context, attrs);
}
in onCreateView, I implemented it all in onCreate of MainActivity and put some declerations at the top of the class:
private RecyclerView.LayoutManager mLayoutManager;
private List<ActionItem> actionItems = new ArrayList<>();
private RecyclerView listOfCards;
private ActionAdapter actionAdapter;
I was still getting a lot of lag, however, and it was due to XML loading the images. So, I used the Glide dependency available on Github instead. In ActionAdapter, I added a context requirement and then changed onBindViewHolder. The context requirement is there to satisfy Glide's method which requires a context.
public class ActionAdapter extends RecyclerView.Adapter<ActionAdapter.ActionHolder> {
private final List<ActionItem> mActionItems;
private Context mContext;
public ActionAdapter(List<ActionItem> actionItems, Context context) {
this.mActionItems = actionItems;
this.mContext = context;
}
#Override
public ActionHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
View itemView = inflater.inflate(R.layout.cardview_item, viewGroup, false);
return new ActionHolder(itemView);
}
#Override
public void onBindViewHolder(ActionHolder actionHolder, int position) {
ActionItem actionItem = mActionItems.get(position);
actionHolder.mTxtTitle.setText(actionItem.getTitleResID());
Glide.with(mContext).load(actionItem.getIconResID()).into(actionHolder.mImgIcon);
}
#Override
public int getItemCount() {
return this.mActionItems.size();
}
public class ActionHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
protected final TextView mTxtTitle;
protected final ImageView mImgIcon;
public ActionHolder(View view) {
super(view);
mImgIcon = (ImageView) view.findViewById(R.id.imgAction);
mTxtTitle = (TextView) view.findViewById(R.id.txtAction);
}
#Override
public void onClick(View v) {
//do something
}
}
}
As you can see, i removed actionHolder.mImgIcon .setText.... from onBindViewHolder and replaced it with the Glide command. This makes the application skip no frames when launching and produce no more occurences of lag when opening the navigation drawer. Turns out the lag was produced by a combination of redundant RelativeLayouts, XML insertion of the picture (rather than the more efficient glide) and not using a RecyclerView.
I am using android.support.v7.widget.CardView to show ImageView with two TextView's in RecyclerView but card view not showing the content(showing only blank white cards) when the screen orientation is vertical, on the other hand it is showing content when orientation is landscape.
When I run the same project from AndroidStudio there is no problem everything works fine.
I don't understand what is problem out there, is there problem with eclipse?
Please see the code below,
PostListFragment
public class PostListFragment extends Fragment implements AppConfig {
private ArrayList<Post> mPostArrayList;
private PostRecyclerAdapter mPostRecyclerAdapter;
private RecyclerView mRecyclerView;
//other declarations
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//removed
}
private void init() {
// Set up RecyclerView
mRecyclerView = (RecyclerView) mRootView
.findViewById(R.id.mPostListRecyclerView);
// Setup layout manager for mPostArrayList and column count
final LinearLayoutManager mLayoutManager = new LinearLayoutManager(
getActivity());
// Control orientation of the mPostArrayList
mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mLayoutManager.scrollToPosition(0);
// Attach layout manager
mRecyclerView.setLayoutManager(mLayoutManager);
// Listen to the item touching
mRecyclerView.addOnItemTouchListener(new RecyclerItemClickListener(
getActivity(),
new RecyclerItemClickListener.OnItemClickListener() {
#Override
public void onItemClick(View itemView, int position) {
//some action
}
}));
mPostArrayList = new ArrayList<>();
// Bind adapter to recycler
mPostRecyclerAdapter = new PostRecyclerAdapter(
getActivity(), mPostArrayList);
mRecyclerView.setAdapter(mPostRecyclerAdapter);
}
private void getPosts() {
// Execute async task
new AsyncPosts().execute(mPostURL);
}
public class AsyncPosts extends AsyncTask<Object, String, JSONObject> {
//no problem with this
}
}
Layout used for PostListFragment
<?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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/mParentLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="#+id/mPostListContainer"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/mPostListSwipeRefreshLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<android.support.v7.widget.RecyclerView
android:id="#+id/mPostListRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.v4.widget.SwipeRefreshLayout>
</FrameLayout>
<!-- removed -->
</LinearLayout>
Here is adapter to set the values,
PostRecyclerAdapter
public class PostRecyclerAdapter extends
RecyclerView.Adapter<PostRecyclerAdapter.SimpleItemViewHolder> {
private Context mContext;
private List<Post> post;
// Provide a suitable constructor (depends on the kind of data store)
public PostRecyclerAdapter(Context context, List<Post> items) {
this.mContext = context;
this.post = items;
}
// Return the size of your data set (invoked by the layout manager)
#Override
public int getItemCount() {
return this.post.size();
}
// Create new items (invoked by the layout manager)
// Usually involves inflating a layout from XML and returning the holder
#Override
public SimpleItemViewHolder onCreateViewHolder(ViewGroup viewGroup,
int viewType) {
View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(
R.layout.post_list_item, viewGroup, false);
return new SimpleItemViewHolder(itemView);
}
// Replace the contents of a view (invoked by the layout manager)
// Involves populating data into the item through holder
#Override
public void onBindViewHolder(SimpleItemViewHolder viewHolder, int position) {
Glide.with(mContext).load(post.get(position).IMG_URL)
.placeholder(R.drawable.ic_placeholder).crossFade(1000)
.centerCrop().into(viewHolder.mPostListSmallThumbnail);
viewHolder.mPostListSmallTitle.setText(post.get(position).POST_TITLE);
viewHolder.mPostListSmallContent
.setText(post.get(position).POST_CONTENT);
}
// Provide a reference to the views for each data item
// Provide access to all the views for a data item in a view holder
public final static class SimpleItemViewHolder extends
RecyclerView.ViewHolder {
ImageView mPostListSmallThumbnail;
TextView mPostListSmallTitle, mPostListSmallContent;
public SimpleItemViewHolder(View itemView) {
super(itemView);
mPostListSmallThumbnail = (ImageView) itemView
.findViewById(R.id.mPostListSmallThumbnail);
mPostListSmallTitle = (TextView) itemView
.findViewById(R.id.mPostListSmallTitle);
mPostListSmallContent = (TextView) itemView
.findViewById(R.id.mPostListSmallContent);
}
}
}
post_list_item
<?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:id="#+id/mPostListSmallCard"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:foreground="#drawable/card_background"
android:clickable="true"
app:cardCornerRadius="#dimen/blog_card_radius"
app:cardUseCompatPadding="true"
app:contentPadding="#dimen/blog_card_radius">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/mPostListSmallThumbnail"
android:layout_width="#dimen/blog_image_thumb_dim"
android:layout_height="#dimen/blog_image_thumb_dim"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="#drawable/ic_placeholder"
android:adjustViewBounds="true"
android:contentDescription="#string/image_thumbnail_placeholder" />
<TextView
android:id="#+id/mPostListSmallTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="#dimen/post_list_margin_left"
android:layout_marginStart="#dimen/post_list_margin_right"
android:layout_toEndOf="#+id/mPostListSmallThumbnail"
android:layout_toRightOf="#+id/mPostListSmallThumbnail"
android:ellipsize="end"
android:lines="2"
android:singleLine="false"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/mPostListSmallContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/mPostListSmallThumbnail"
android:layout_alignEnd="#+id/mPostListSmallTitle"
android:layout_alignLeft="#+id/mPostListSmallTitle"
android:layout_alignRight="#+id/mPostListSmallTitle"
android:layout_alignStart="#+id/mPostListSmallTitle"
android:layout_below="#+id/mPostListSmallTitle"
android:ellipsize="end"
android:lines="3"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
</android.support.v7.widget.CardView>
Here is post_list_item for landscape layout
land/post_list_item
<?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:id="#+id/mPostListSmallCard"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="#dimen/blog_card_margin_landscape"
android:layout_marginLeft="#dimen/blog_card_margin_landscape"
app:cardCornerRadius="#dimen/blog_card_radius"
app:cardUseCompatPadding="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/mPostListSmallThumbnail"
android:layout_width="#dimen/blog_image_thumb_dim"
android:layout_height="#dimen/blog_image_thumb_dim"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:adjustViewBounds="true"
android:contentDescription="#string/image_thumbnail_placeholder" />
<TextView
android:id="#+id/mPostListSmallTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="#dimen/post_list_margin_left"
android:layout_marginStart="#dimen/post_list_margin_right"
android:layout_toEndOf="#+id/mPostListSmallThumbnail"
android:layout_toRightOf="#+id/mPostListSmallThumbnail"
android:ellipsize="end"
android:lines="2"
android:singleLine="false"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/mPostListSmallContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/mPostListSmallThumbnail"
android:layout_alignEnd="#+id/mPostListSmallTitle"
android:layout_alignLeft="#+id/mPostListSmallTitle"
android:layout_alignRight="#+id/mPostListSmallTitle"
android:layout_alignStart="#+id/mPostListSmallTitle"
android:layout_below="#+id/mPostListSmallTitle"
android:ellipsize="end"
android:lines="3"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
</android.support.v7.widget.CardView>