android


listview refresh in android databinding


I am working with one of application using android MVVM architecture with databinding concepts.
The problem is when i am updating the textview of the xml the entire view gets updated.so listview gets refreshed and scroll to top position.i want to restrict the listview when onItem click event occurs.
Following xml contains listview.
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:bind="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="kenoservicesVM"
type="com.naushad.kenocustomer.landing.KenoServicesVM" />
<variable
name="kenoservice"
type="com.naushad.kenocustomer.landing.KenoService" />
</data>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/nliveo_transparent"
android:paddingEnd="20sp"
android:paddingStart="20sp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:text="Total"
android:textColor="#color/white" />
<ScrollView
android:id="#+id/SCROLLER_ID"
android:layout_width="fill_parent"
android:layout_height="160sp"
android:layout_margin="0sp"
android:background="#color/nliveo_transparent"
android:fillViewport="true"
android:scrollbars="none">
<ListView
android:id="#+id/lstServices"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onItemClick="#{kenoservicesVM::onItemClick}"
bind:items="#{kenoservicesVM.mList}">
</ListView>
<!--android:onItemClick="#{kenoservicesVM::onItemClick}"-->
</ScrollView>
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:text="Total"
android:textColor="#color/white" />
<TextView
android:id="#+id/totalCharges"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:paddingRight="10dp"
android:paddingTop="10dp"
android:textColor="#color/white" />
<!--android:text="#{kenoservicesVM.totalPrice ?? #string/_0_aed}"-->
</LinearLayout>
<RelativeLayout
android:id="#+id/your_btn_id"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/bgNext"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginBottom="5dip"
android:layout_marginTop="15dip"
android:alpha="0.5"
android:src="#drawable/blue_round_light_bg" />
<Button
android:id="#+id/btn_add_card"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_marginBottom="5dip"
android:layout_marginTop="15dip"
android:background="#drawable/blue_next_bg" />
</RelativeLayout>
</LinearLayout>
</FrameLayout>
</layout>
Following viewmodel class indicates the viewmodel which i have used in xml.
public class KenoServicesVM extends BaseObservable {
public ObservableArrayList<KenoService> mList = new ObservableArrayList<>();
private Context mContext;
#NonNull
private String mTotalPrice;
int selectedServiceCharges=0;
private int bar;
public ObservableField<String> price;
Handler handler;
public KenoServicesVM(List<KenoService> list, Context mContext) {
mList.addAll(list);
this.mContext = mContext;
}
#NonNull
public String getTotalPrice() {
return mTotalPrice;
}
public void setTotalPrice(#NonNull String totalPrice) {
this.mTotalPrice = totalPrice;
// notifyPropertyChanged(BR.kenoservicesVM);
// notifyPropertyChanged(BR.totalPrice);
notifyChange();
// notifyPropertyChanged(BR.kenoservicesVM);
}
#BindingAdapter("bind:items")
public static void bindList(ListView view, ObservableArrayList<KenoService> list) {
CustomListAdapter adapter = new CustomListAdapter(list);
view.setAdapter(adapter);
}
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
if(position == 0) return;
if(mList.get(position).getSelected())
mList.get(position).setSelected(false);
else
mList.get(position).setSelected(true);
for (int i=0 ;i<mList.size();i++){
KenoService keno = mList.get(i);
if(keno.getSelected())
selectedServiceCharges +=Integer.parseInt(keno.getKenoServiceCharge());
}
/*handler = new Handler();
Runnable runnable = new Runnable() {
#Override
public void run() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
handler.post(new Runnable() {
#Override
public void run() {
price = new ObservableField<>(selectedServiceCharges +" AED");
}
});
}
};
new Thread(runnable).start();*/
setTotalPrice(selectedServiceCharges +" AED");
// dialogueServicesBinding.totalCharges.setText(selectedServiceCharges +" AED");
// mTotalPrice = selectedServiceCharges +" AED";
// ModuleManager.startActivity(ModuleManager.DISPATCH_MODULE, DispatchModule.STATE_FORGOTPASSWORD, (Activity) mContext);
}
}
I am new to android databinding concepts.Please help me to resolve this issue when i click on listview item and refreshing the view.Thanks in advance.
You need to use Bindable for your properties.
#NonNull
#Bindable
public String getTotalPrice() {
return mTotalPrice;
}
This will create the BR.totalPrice field and you'll be able to call
notifyPropertyChanged(BR.totalPrice);
and the text should update with it
android:text="#{kenoservicesVM.totalPrice ?? #string/_0_aed}"
Additionally #NonNull is a promise you might not be able to hold. Consider adding a default value.
Sorry I can't leave a comment due to reputation but I have bumped into one of the similar problem before, you can pass mList and position in your setTotalPrice methods and then called notifyItemChanged instead of notifyChange, this way it will update only the item at the specified position in your list
public void setTotalPrice(#NonNull String totalPrice, int position, ObservableArrayList<KenoService> mList, TextView totalCharges) {
this.mTotalPrice = totalPrice;
mList.notifyItemChanged(position);
totalCharges.setText(//whatever u need here);
totalCharges.invalidate();
}

Related Links

Restrict android application to specific region
Get actionbar previous title
Restrict upload from Android Photo's instead of gallery
Loading my transparent activity hides the keyboard
How could I use Context Wrapper in order to save SharedPreferences in an external sd card folder in Android?
Animated Childrens Book App (iOS & Android)
Android application support multiple screen sizes
Playing video from http server in android
I have taken image from gallery to image view in same intent.And on image view click I want to send that image another Activity through intent.
Galaxy S4 save byte[] image contain gray line
Releasing Camera takes a long time after locked screen
How to move up my controls when they would be hidden by the soft keyboard?
encrypting my password using MD5
How to implement 4 buttons in Android App, Eclipse
youtube video 400 network when try to play video
Markers Disappear after re-running the application

Categories

HOME
xml
linkedin
flex
components
interpolation
iis-6
google-cloud-datalab
vert.x
minecraft-forge
fhir
java.util.logging
plink
android-intent
cumulocity
gruntfile
glyphicons
feathersjs
fileserver
red5
identity-management
squashfs
imagemap
php-mysqlidb
event-store
aggregation
short-url
mount
iis-express
sqlpackage
nsurl
sammy.js
cd
connection-pool
sim-card
news
skylink
apriori
dcmtk
congestion-control
android-checkbox
automapper-5
truezip
flac
django-1.10
structuremap4
juice-ui
docpad
meteor-packages
grails-plugin
mapxtreme
xcode-server
rspec2
mapinfo
tooleap
eigenvector
pre-build-event
qiime
angular-ui-typeahead
data-protection
grass
asyncdisplaykit
wiredep
toolkit
visual-sourcesafe-2005
batik
amiga
jsvc
teamcity-9.1
bcache
entry-point
imagefilter
wcm
tweenlite
xml-namespaces
parallel-testing
functional-java
renderer
cfcache
koala
android-audiorecord
openargs
high-resolution
gwt-openlayers
eclipse-kepler
script#
scringo
joomla-template
jquery-blockui
rmiregistry
cuda-gdb
lambdaj
floating-point-exceptions
convention
jqote
http-daemon
appledoc
uideviceorientation
dot-emacs
skype4java
fail-fast-fail-early

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App