Reputation: 21
I used View-Holder class for displaying custom list view.In some devices like Galaxy S3,S4 the scrolling in list view will be very slow.In some devices it's very fast.I don't know why it's not scrolling fast in S3 and S4.Please can any one help me.
Here My code is:
public class doctortAdapter extends BaseAdapter {
@Override
public int getCount() {
// TODO Auto-generated method stub
return doctorData_list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(final int position, View convertView,
ViewGroup parent) {
// TODO Auto-generated method stub
View view = convertView;
final RecordHolder holder;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.custom_list, null);
holder = new RecordHolder();
holder.txtName = (TextView) view
.findViewById(R.id.textView_name);
holder.txtAddress1 = (TextView) view
.findViewById(R.id.textView_address);
holder.txtAddress2 = (TextView) view
.findViewById(R.id.textView_address1);
holder.btn_book = (Button) view.findViewById(R.id.button_book);
holder.btn_advice = (Button) view
.findViewById(R.id.button_advice);
holder.btn_favourite = (Button) view
.findViewById(R.id.button_faviourate);
holder.txtReview = (TextView) view.findViewById(R.id.textView2);
holder.img_personPic = (ImageView) view
.findViewById(R.id.imageView1);
holder.rt = (RatingBar) view.findViewById(R.id.ratingBar1);
holder.btn_book.setFocusable(false);
holder.btn_advice.setFocusable(false);
holder.btn_favourite.setFocusable(false);
holder.rt.setFocusable(false);
holder.rt.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if (event.getAction() == MotionEvent.ACTION_UP) {
DoctorData doc_data = doctorData_list.get(position);
selected_doctorID = doc_data.getDocID();
if (checkSession() == true) {
addReview();
} else {
ratingBarDialog();
}
}
return true;
}
});
holder.btn_favourite.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
DoctorData doc_data = doctorData_list.get(position);
doctorid = doc_data.getDocID();
if (checkSession() == true) {
favouriteDialog();
} else {
// ratingBarDialog();
signInDialog();
}
}
});
holder.btn_book.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
DoctorData doc_data = doctorData_list.get(position);
startActivity(new Intent(MainActivity.this,
DoctorDetailsActivity.class).putExtra(
"doctorData", doc_data).putExtra(
"TabSelection", "schedule"));
Editor prefsEditor = pref.edit();
Gson gson = new Gson();
String json = gson.toJson(doc_data);
prefsEditor.putString("doctorData", json);
prefsEditor.commit();
finish();
}
});
holder.btn_advice.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this,
FreeAdviceActivity.class));
finish();
}
});
holder.txtReview.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
DoctorData doc_data = doctorData_list.get(position);
startActivity(new Intent(MainActivity.this,
DoctorDetailsActivity.class).putExtra(
"doctorData", doc_data).putExtra(
"TabSelection", "review"));
Editor prefsEditor = pref.edit();
Gson gson = new Gson();
String json = gson.toJson(doc_data);
prefsEditor.putString("doctorData", json);
prefsEditor.commit();
finish();
}
});
view.setTag(holder);
} else {
holder = (RecordHolder) view.getTag();
}
DoctorData doc_data = doctorData_list.get(position);
holder.txtName.setText(doc_data.getFirstName() + " "
+ doc_data.getLastName());
holder.txtAddress1.setText(doc_data.getAddress1());
holder.txtAddress2.setText(doc_data.getAddress2());
return view;
}
}
public class RecordHolder {
TextView txtName, txtAddress1, txtAddress2, txtReview;
ImageView img_personPic;
Button btn_book, btn_favourite, btn_advice;
RatingBar rt;
}
custom listview code:
<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=".MainActivity" >
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="80dp"
android:layout_height="70dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:src="@drawable/defaultprofilepic" />
<TextView
android:id="@+id/textView_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/imageView1"
android:layout_marginLeft="16dp"
android:layout_toRightOf="@+id/imageView1"
android:text="No Name"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/imageView1"
android:layout_alignParentRight="true"
android:layout_below="@+id/textView2"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:orientation="horizontal"
android:weightSum="2" >
<Button
android:id="@+id/button_advice"
style="@style/buttons"
android:layout_width="wrap_content"
android:layout_alignLeft="@+id/imageView1"
android:layout_below="@+id/imageView1"
android:layout_weight="1"
android:text="Free Advice" />
<Button
android:id="@+id/button_book"
style="@style/buttons"
android:layout_width="wrap_content"
android:layout_alignTop="@+id/button_faviourite"
android:layout_marginLeft="5dp"
android:layout_toRightOf="@+id/button_faviourite"
android:layout_weight="1"
android:text="$0 Book 0nline" />
</LinearLayout>
<TextView
android:id="@+id/textView_address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView_name"
android:layout_below="@+id/textView_name"
android:layout_marginTop="3dp"
android:maxLines="2"
android:text="No address"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/textView_address1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView_name"
android:layout_below="@+id/textView_address"
android:layout_marginTop="3dp"
android:text="No address"
android:textAppearance="?android:attr/textAppearanceSmall" />
<RatingBar
android:id="@+id/ratingBar1"
style="@style/foodRatingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView_address1"
android:layout_below="@+id/textView_address1"
android:layout_marginTop="5dp" />
<Button
android:id="@+id/button_faviourate"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignRight="@+id/linearLayout1"
android:layout_alignTop="@+id/textView_name"
android:background="@drawable/favoirate_selector" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/ratingBar1"
android:layout_alignRight="@+id/linearLayout1"
android:text="Reviews"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#07a3d3" />
</RelativeLayout>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/relativeLayout1"
android:layout_marginLeft="100dp"
android:text="TextView"
android:visibility="invisible" />
</RelativeLayout>
and mainActicity:
<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=".MainActivity" >
<TableLayout
android:id="@+id/tableLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="5dp">
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_weight="1"
android:src="@drawable/doctor" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_weight="1"
android:src="@drawable/location" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_weight="1"
android:src="@drawable/insurance" />
</TableRow>
</TableLayout>
<TableLayout
android:id="@+id/tableLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/tableLayout" >
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Speciality"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Location"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Small Text"
android:visibility="invisible"
android:textAppearance="?android:attr/textAppearanceSmall" />
</TableRow>
</TableLayout>
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_below="@+id/tableLayout1"
android:layout_marginTop="5dp"
android:background="@drawable/new_header" >
<TextView
android:id="@+id/textView_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
<Button
android:id="@+id/button_privious"
style="?android:attr/buttonStyleSmall"
android:layout_width="30dp"
android:layout_height="40dp"
android:layout_alignBaseline="@+id/textView_date"
android:layout_alignBottom="@+id/textView_date"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp"
android:background="@drawable/leftarrow" />
<Button
android:id="@+id/button_next"
style="?android:attr/buttonStyleSmall"
android:layout_width="30dp"
android:layout_height="40dp"
android:layout_alignBaseline="@+id/textView_date"
android:layout_alignBottom="@+id/textView_date"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:background="@drawable/rightarrow" />
<ImageView
android:id="@+id/imageView_date"
android:layout_width="40dp"
android:layout_height="30dp"
android:layout_centerVertical="true"
android:layout_marginRight="15dp"
android:layout_toLeftOf="@+id/button_next"
android:src="@drawable/calendar" />
</RelativeLayout>
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_above="@+id/button_next_call1"
android:layout_alignParentLeft="true"
android:layout_below="@+id/relativeLayout1" >
</ListView>
<ProgressBar
android:id="@+id/progressBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:indeterminateDrawable="@drawable/my_custom_progresswheel"
android:visibility="invisible"
/>
<Button
android:id="@+id/button_next_call1"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_margin="5dp"
android:text="Next"
android:textColor="#ffffff"
android:background="@drawable/customborder_backbutton"/>
<Button
android:id="@+id/button_previous"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_margin="5dp"
android:text="Previous"
android:textColor="#ffffff"
android:visibility="invisible"
android:background="@drawable/customborder_backbutton" />
</RelativeLayout>
Upvotes: 2
Views: 814
Reputation: 10938
What is doctorData_list? is its get method's time complexity O(1)?
Pass in the parent ViewGroup when inflating (but don't add it to the root):
view = inflater.inflate(R.layout.custom_list, parent, false);
Don't nest RelativeLayouts, they cause exponential layout passes which is really not nice in a ListView The root for your listview item looks like it can be a vertical LinearLayout, and the root for the Activity could be a FrameLayout with a LinearLayout inside it?
Return doctorData_list.get(position)
in getItem
Return position
in getItemId
Save the position into the view's tag outside of the if(view == null)/else
condition. Read that tag in the onClick
, onTouch
, etc methods out of the view that is passed into those methods.
Currently, your buttons will use the position that the view was first created for - if you scroll down and they get recycled, when you click on something in position X, it will update the data for a different position.
Upvotes: 0
Reputation: 3513
I checked your code and I found out:--
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
declare it in as instance variable of adapter class and then use that.
getListView().setFastScrollEnabled(true);
Hope this helps..enjoy..!
I checked your xml and your code, everything is fine i think the problem is your listview row having many view which you are getting findviewbyid() which takes a time so may be you need to optimize that.Remove those View findViewById() which you are not using for display.
Upvotes: 1
Reputation: 17401
set all the onclick out side the if
after this:
} else {
holder = (RecordHolder) view.getTag();
}
//set all the onClick listener
Upvotes: 0