Android - List not binding to RecyclerView MvvmCross - android

I am fairly new to android and I am trying to bind a List I have in my ViewModel to a RecyclerView in my UI. I am using Xamarin and MvvmCross. I have the RecyclerView setup like this:
<android.support.design.widget.NavigationView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/navigation_view"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_gravity="start"
android:fitsSystemWindows="true"
android:theme="#style/Theme.AppCompat.Light">
<MvvmCross.Droid.Support.V7.RecyclerView.MvxRecyclerView
android:id="#+id/navigation_recycler_view"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:clickable="true"
app:MvxItemTemplate="#layout/menu_item"
app:MvxBind="ItemsSource MyItems"/>
</LinearLayout>
Here's my menu item:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="48dp" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="13sp"
app:MvxBind="Text Title" />
</LinearLayout>
and lastly, my VM List is something like this:
MyItems = new List<NavItem>
{
new NavItem { Title = "Option1" }
new NavItem { Title = "Option2" }
};
EDIT:
Here where I set the properties:
List<NavItem> _myItems;
public List<NavItem> MyItems
{
get => _myItems;
set => SetProperty(ref _myItems, value);
}
and MyItem:
public string Title { get; set; }
I'm pretty sure it's setup properly; however, for some reason, it won't populate my recycler view. Did I miss something? any help would be very much appreciated. Thanks!

Figured it out! I had xmlns:app="https: instead of xmlns:app="http:

Related

How to show Empty Message when RecyclerView is empty?

I am loading some contents from Firebase and also using FirebaseRecyclerAdapter .
This is the Function which shows data from Firebase successfully :
public void showNotifications() {
databaseReference = FirebaseDatabase.getInstance().getReference("Notifications");
databaseReference.keepSynced(true);
options = new FirebaseRecyclerOptions.Builder<NotificationModel>()
.setQuery(FirebaseDatabase.getInstance().getReference("Notifications"), NotificationModel.class)
.build();
notifRecyclerView.setLayoutManager(new LinearLayoutManager(this));
notificationAdaper = new NotificationAdaper(options, NotificationActivity.this);
notifRecyclerView.setAdapter(notificationAdaper);
checkEmpty(); //will show empty image when recyclerView is empty
}
This is my checkEmpty() method :
private void checkEmpty() {
if (notificationAdaper.getItemCount() == 0) {
Log.e("Item count", String.valueOf(notificationAdaper.getItemCount()));
no_notif.setVisibility(View.VISIBLE);
} else {
no_notif.setVisibility(View.GONE);
}
}
Here is the UI code :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
tools:context="com.devapps.NotificationActivity">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
app:theme="#style/ToolbarTheme">
<TextView
android:id="#+id/toolbarTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
android:text="Notifications"
android:textColor="#android:color/white"
android:textSize="18sp"
android:textStyle="bold"
android:visibility="visible" />
</androidx.appcompat.widget.Toolbar>
<ImageView
android:id="#+id/no_notif"
android:layout_width="260dp"
android:layout_height="260dp"
android:visibility="gone"
android:layout_centerInParent="true"
android:scaleType="centerCrop"
android:src="#drawable/nonotification" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/notifRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/toolbar" />
</RelativeLayout>
The Empty Image Placeholder is always visible even when there is an Item in Firebase Database. How to fix it.
Everytime i am getting E/Item count: 0 even when there is an Item in Firebase Database.
May be you are just altering the visibility of empty view(which you are using to notify user) and not hiding the other views. Like you have recycler view so you need to hide that and view empty message otherwise the message view may be behind the recycler view and hence it is not visible.
Solution
Hide other views on that particular screen
private void checkEmpty() {
if (notificationAdaper.getItemCount() == 0) {
Log.e("Item count", String.valueOf(notificationAdaper.getItemCount()));
no_notif.setVisibility(View.VISIBLE);
notifRecyclerView.setVisibility(View.GONE);
} else {
no_notif.setVisibility(View.GONE);
notifRecyclerView.setVisibility(View.VISIBLE);
}
}
NOTE: notifRecyclerView is the id of recycler view...... I have used it for reference..... if you have declared recycler view with another name in the file , then use that name in lieu of notifRecyclerView
You can use this method to get Recycler View Count
recyclerView.getAdapter().getItemCount();

Get a field from Fragment on Adapter and get List from Adapter on Fragment

I'm making a list of checkbox with itens on my adapter and my fragment has one checkBox on the top of the list with "Select All" and on the bottom of the list a textView "x Itens Selected" plus a button.
My problem 1 is how to get this "x": the count of how many checkboxes are checked, because they are on Adapter and the TextView is on Fragment, I can't set it on Adapter.
To get this, I'm using a setOnCheckedChangeListener from the list of checkboxes.
I've tried to get Fragment from Adapter with a lot of ways but without success. Sending on constructor, getting from viewHolder, getting from getSupportFragmentManager(), getFragments(), getFragmentById, getFragmentByTag.
I've tried FragmentObserver also, but it didnt work.
Problem 2: So I've tried to make a list on Adapter with all the checked Itens, but I need to send it to Fragment to manage it. Also, I can't get it from Adapter on Fragment (on onCreateView).
Can someone help please?
My Adapter.java onBindViewHolder:
#Override
public void onBindViewHolder(final PedidoItemRefugadoViewHolder holder, final int position) {
//pedidoItem is each object from the list
final PedidoItem pedidoItem = itensRefugados.get(position);
//checkCadaItemRefugado is each checkbox from Adapter
holder.checkCadaItemRefugado.setText(pedidoItem.getItemPedidoObservacao(context));
holder.checkCadaItemRefugado.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int count = 0;
int size = itensRefugados.size();
for (int i=0; i<size; i++){
if (holder.checkCadaItemRefugado.isSelected()) {
count++;
//itensRefugadosChecked is the new list with all the checked items
itensRefugadosChecked.add(pedidoItem);
}
}
}
});
}
My XML Adapter:
<?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"
xmlns:card_view="http://schemas.android.com/tools"
android:id="#+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="6dp"
app:cardElevation="3dp"
app:cardPreventCornerOverlap="false"
app:cardUseCompatPadding="false"
card_view:cardCornerRadius="2dp"
card_view:cardElevation="6dp"
android:foreground="?attr/selectableItemBackground">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/frame_layout_notificacao"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp">
<CheckBox
android:id="#+id/check_cada_item_refugado"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Item: Pedido: \nMotivo:"/>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
My XML Fragment:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context="br.com.zapgrafica.appzap.fragments.ItensRefugadosFragment">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="32dp"
android:orientation="horizontal"
android:layout_marginTop="8dp"
android:layout_marginStart="7dp">
<CheckBox
android:id="#+id/check_itens_refugados"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Selecionar todos"/>
</RelativeLayout>
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/itens_refugados_layout_swipe_to_refresh"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="42dp"
android:layout_marginBottom="66dp">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#android:color/transparent"
android:clipToPadding="false"
android:divider="#null"
android:dividerHeight="0dp"
android:listSelector="#android:color/transparent"
android:scrollbarStyle="outsideOverlay"
android:scrollbars="vertical" />
</android.support.v4.widget.SwipeRefreshLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/txt_itens_refugados_total"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:layout_above="#+id/btn_itens_refugados_total"
android:text=""/>
<Button
android:id="#+id/btn_itens_refugados_total"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="#string/itens_refugados_confirmar"
android:theme="#style/ThemeButtonAjusteOnlineRefugo"/>
</RelativeLayout>
</FrameLayout>
You can do it using interface.
Create your interface class, initialize it in adapter class and implement it in your fragment. So when you will need to send any value to fragment, use method which you have created in interface to send data to fragment. You will get checked checkbox values in fragment. Refer this link. Hope this will help you.

How to Display New Item on MvxListView on Button Click in Android Xamarin Visual Studio

I want to display a new line of TextView everytime I click a button. I am using MvvmCross and I know that I should use MvxListView which is assigned to a layout template, but I do not know how to display it on screen on button click.
Can someone give me a simple code example of doing this from ViewModel and very short explanation about which line of code that initiate the display of new TextView?
UPDATE
I provide my code below:
ViewModel.cs
public class FirstViewModel
: MvxViewModel
{
private ObservableCollection<Unit> unitCodes;
public ObservableCollection<Unit> UnitCodes
{
get { return unitCodes; }
set { unitCodes = value; RaisePropertyChanged(() => UnitCodes); }
}
public IMvxCommand ButtonCommand { get; private set; }
public FirstViewModel()
{
unitCodes = new ObservableCollection<Unit>();
ButtonCommand = new MvxCommand(() =>
{
UnitCodes = UnitCodes ?? new ObservableCollection<Unit>();
UnitCodes.Add(new Unit("123", "Test Name"));
});
}
public class Unit : MvxViewModel
{
private string unitCode;
public string UnitCode
{
get { return unitCode; }
set { unitCode = value; RaisePropertyChanged(() => UnitCode); }
}
private string unitName;
public string UnitName
{
get { return unitName; }
set { unitName = value; RaisePropertyChanged(() => UnitName); }
}
public Unit() { }
public Unit(string unitCode, string unitName)
{
UnitCode = unitCode;
UnitName = unitName;
}
}
}
View.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="40dp"
local:MvxBind="Click ButtonCommand"
android:text="Click To Add" />
<Mvx.MvxListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
local:MvxItemTemplate="#layout/unitcodeitemlayout"
local:MvxBind="ItemSource UnitCodes" />
</LinearLayout>
unitcodeitemlayout.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
local:MvxBind="Text UnitName" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="30dp"
local:MvxBind="Text UnitCode" />
</LinearLayout>
My code does not have any compilation or run-time error, however there is nothing happening on button click. What I want is to add "123" and "Test Name" to the display every time I click the button.
Your code is right. However, you misspelled the ItemsSource under MvxListView axml.
It should be:
<Mvx.MvxListView
...
local:MvxBind = "ItemsSource UnitCodes"/>
In order to set the ListView, take a look at this MvvmCross tutorial
You could end up with something like this:
ViewModel:
public class MyListViewModel : MvxViewModel
{
private ObservableCollection<TextViewCellViewModel> _listItems;
public virtual ObservableCollection<TextViewCellViewModel> ListItems {
get {
return _listItems;
}
set {
_listItems = value;
RaisePropertyChanged(() => ListItems);
}
}
private IMvxCommand _addTextViewCommand;
public IMvxCommand AddTextViewCommand {
get {
_addTextViewCommand = _addTextViewCommand ?? new MvxCommand(AddTextView);
return _addTextViewCommand;
}
}
private void AddTextView()
{
ListItems = ListItems ?? new ObservableCollection<TextViewCellViewModel>();
ListItems.Add(new TextViewCellViewModel());
}
public class TextViewCellViewModel : MvxViewModel
{
private string _text;
public string Text { get { return _text; } set { _text = value; RaisePropertyChanged(() => Text); } }
public TextViewCellViewModel() { }
}
}
Android ListView Layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="fill_vertical"
android:paddingRight="20dp"
android:paddingLeft="20dp">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/frameLayout"
android:paddingBottom="50dp">
<MvxListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="vertical"
android:id="#+id/list_view_tripplanner_result"
android:divider="#android:color/transparent"
android:paddingTop="10dp"
android:clipToPadding="false"
android:dividerHeight="7dp"
android:descendantFocusability="afterDescendants"
local:MvxItemTemplate="#layout/cell_textview"
local:MvxBind="ItemsSource ListItems;" />
</FrameLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:paddingTop="5dp"
android:paddingBottom="10dp">
<Button
android:layout_width="match_parent"
android:layout_height="40dp"
local:MvxBind="Click AddTextViewCommand"
android:text="Add TextView"
android:textColor="#color/primary_white"
android:background="#android:color/darker_gray" />
</LinearLayout>
</RelativeLayout>
Which should look like this: (notice button on the bottom of the Listview)
Where #layout/cell_textview would look something like this:
layout for the ItemTemplate for the ListView would be:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:textSize="20dp"
local:MvxBind="Text Text"/>
</LinearLayout>

SwipeRefreshLayout Issues When Using emptyView and ListView

I'm using a SwipeRefreshLayout wrapped around a ListView. When the ListView is empty, I've set a TextView to appear. This 'works' fine, but when the ListView is empty and the empty view is showing, the SwipeRefreshLayout doesn't display right. If you drag down, no circle comes down but when you release your finger the circle flashes on the screen and then disappears. If you just touch the screen anywhere, in fact, the SwipeRefreshLayout will appear in the same fashion. The following is how I've defined it in my XML:
<FrameLayout
android:id="#+id/empty_view_requests"
android:layout_width="match_parent"
android:layout_centerInParent="true"
android:layout_below="#+id/appbar"
android:layout_height="match_parent">
<android.support.v4.widget.SwipeRefreshLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/appbar"
android:id="#+id/buddy_requests_swipe_layout">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/buddy_requests_listview"
android:layout_gravity="center_horizontal|top"
/>
</android.support.v4.widget.SwipeRefreshLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:layout_gravity="center_horizontal|center_vertical"
android:id="#+id/norequests_textview"
android:text="No buddy requests."/>
</FrameLayout>
EDIT: Activity Code
swipeRefreshLayout = (SwipeRefreshLayout)findViewById(R.id.buddy_requests_swipe_layout);
swipeRefreshLayout.setColorSchemeResources(R.color.lb_green, R.color.lb_orange, R.color.lb_purple);
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
VolleyBuddies.makeGetBuddyRequestsAPICall(BuddyRequestsActivity.this, BuddyRequestsActivity.this);
}
});
FrameLayout noRequestsLayout = (FrameLayout)findViewById(R.id.empty_view_requests);
ListView requestsListView = (ListView)findViewById(R.id.buddy_requests_listview);
requestListAdapter = new BuddyRequestsActivityListAdapter(this, this);
requestsListView.setAdapter(requestListAdapter);
requestsListView.setEmptyView(noRequestsLayout);
How can I fix this?
I am pasting working code of mine.Even I have faced the issues. hope this will works for you.
Initially set the emptyview visibility View.GONE.and then on the API Call response callBacks, if there are no items/statuscode..etc ,setvisibility View.VISIBLE and set it as emptyviewfor listview.
findViewById(R.id.lvemptyview).setVisibility(View.GONE);
mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
if (Utils.isInternetConnected(getApplicationContext())) {
CallAPICall(lat, lng);
mSwipeRefreshLayout.setRefreshing(false);
} }});
on volley response callback
mSwipeRefreshLayout.setRefreshing(false);
if (statuscode== 200) {
try {
String content = new String(arg2, "UTF-8");
MemberResp memberResp;
Gson gson = new Gson();
Type type = new TypeToken<MemberResp>() {
}.getType();
memberResp = gson.fromJson(content, type);
if (memberResp.Status == 1) {
if (memberResp.bar_lists.size() > 0) {
findViewById(R.id.lvemptyview).setVisibility(View.GONE);
setadapter();
} else {
Adapter.ClearAll();
findViewById(R.id.lvemptyview).setVisibility(View.VISIBLE);
lvList.setEmptyView(findViewById(R.id.lvemptyview));
}
Try placing your TextView inside a ScrollView. I found that the SwipeRefreshLayout requires a scrollable view of some sort
Your code would be
<FrameLayout
android:id="#+id/empty_view_requests"
android:layout_width="match_parent"
android:layout_centerInParent="true"
android:layout_below="#+id/appbar"
android:layout_height="match_parent">
<android.support.v4.widget.SwipeRefreshLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/appbar"
android:id="#+id/buddy_requests_swipe_layout">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/buddy_requests_listview"
android:layout_gravity="center_horizontal|top"
/>
</android.support.v4.widget.SwipeRefreshLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:layout_gravity="center_horizontal|center_vertical"
android:id="#+id/norequests_textview"
android:text="No buddy requests."/>
</ScrollView>
</FrameLayout>
Should work right away
You have set your parent framelayout as an empty view of the child(listview). Try removing that line.
Empty layout is text view. Please refer following code. and set adapter after SetEmptyListView.
TextView noRequestsLayout = (TextView)findViewById(R.id.norequests_textview);
ListView requestsListView = (ListView)findViewById(R.id.buddy_requests_listview);
requestListAdapter = new BuddyRequestsActivityListAdapter(this, this);
requestsListView.setEmptyView(noRequestsLayout);
requestsListView.setAdapter(requestListAdapter);

How to map a list of list to RecyclerView

I am trying to map a List<List<Object>> to a RecyclerView in Android, but the result is totally messed up.
For example, I have a list of list like this (just for explaining, not my real case):
List<List<String>> list = {{a, b, c}, {d, e}, {f, g, h, i}};
the Recyclerview should display like (first row contains three subrows, second has two and the last on has four):
|----a-----|
|----b-----|
|----c-----|
|=======|
|----d-----|
|----e-----|
|=======|
|----f-----|
|----g-----|
|----h-----|
|----i-----|
but the result is not exactly like the above order. some element is duplicated and some disappears. for example:
|----d-----|
|----e-----|
|----c-----|
|=======|
|----d-----|
|----e-----|
|=======|
|----f-----|
|----a-----|
Here is a piece of my code:
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
List<Event> list = eventList.get(position);
if (holder.rows < list.size()) {
holder.rowViewGroup.removeAllViews();
holder.rows = 0;
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
TextView startView = new TextView(context);
startView.setLayoutParams(layoutParams);
Event headEvent = list.get(0);
Calendar startCal = headEvent.getStartCal();
startView.setText(String.format("%tD", startCal));
holder.rowViewGroup.addView(startView);
for (final Event event : list) {
View element = LayoutInflater.from(context).inflate(R.layout.element, null);
//start time view
TextView startTimeView = (TextView)element.findViewById(R.id.eventStartTimeInElement);
Calendar startTimeCal = event.getStartCal();
startTimeView.setText(String.format("%tl:%tM %tp", startTimeCal, startTimeCal, startTimeCal));
//end date time view
TextView endView = (TextView)element.findViewById(R.id.eventEndTimeInElement);
Calendar endCal = event.getEndCal();
endView.setText(String.format("%tD %tl:%tM %tp", endCal, endCal, endCal, endCal));
//title view
TextView title = (TextView)element.findViewById(R.id.eventTitleInElement);
title.setText(event.getTitle());
//member name
Drawable divider = context.getResources().getDrawable(R.drawable.divider);
ImageView dividerView = new ImageView(context);
dividerView.setImageDrawable(divider);
holder.rowViewGroup.addView(dividerView);
holder.rowViewGroup.addView(element);
holder.rows++;
}
}
}
Here is my row.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/recycleViewRow">
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="0dp"
card_view:cardElevation="2sp"
card_view:cardUseCompatPadding="true"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/rowView"
android:paddingLeft="20dp"
android:paddingRight="20dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/eventStartTimeInRow"
/>
</LinearLayout>
</android.support.v7.widget.CardView>
and element.xml(which is contained by row.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="wrap_content">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="#+id/eventStartTimeInElement"
android:layout_weight="2" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="#+id/eventEndTimeInElement"
android:gravity="end"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="#+id/eventTitleInElement"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right">
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
card_view:cardCornerRadius="2dp"
card_view:cardElevation="2sp"
card_view:cardUseCompatPadding="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="#+id/memberNameInElement"
android:gravity="end"
/>
</android.support.v7.widget.CardView>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
And I know this is not a really good idea to implement this, so could anybody please tell me a better way to implement this? Thanks...
I think your best bet is to flatten your list of lists into one list.
This is just for purposes of the ListView. Your adapter will act as though the lists have all been strung into one long list.
Start by creating a single-list index for all the items of your nested lists.
For example:
List<List<String>> listOfLists = ... // your original data
List<String> listForView = new ArrayList<String>();
for (List<String> innerList : listOfLists) {
for (String str : innerList) {
listForView.add(str);
}
listForView.add("---"); // e.g. marker value for divider
}
Now in your adapter's getItem() need only return listForView.get(position).
Anytime your nested list structure changes, you redo this flattening operation and call notifyDataSetChanged().
Things get a little trickier if — given the position — you have to figure out which list an item is in, but you could always index the inner lists in a similar fashion.

Categories

Resources