Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 1st, 2012  |  syntax: None  |  size: 5.43 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Why is ListView not scrolling consistently?
  2. <ListView
  3.      android:id="@+id/list_view"
  4.      android:layout_height="match_parent"
  5.      android:layout_width="match_parent"
  6.      android:scrollingCache="true">
  7.  
  8. </ListView>
  9.        
  10. adptr = new ArrayAdapter<String>(iF, R.layout.list_item, showing) {
  11.  
  12.         @Override
  13.         public View getView(int position, View convertView, ViewGroup grp) {
  14.  
  15.             LinearLayout lin = new LinearLayout(this.getContext());
  16.             lin.setOrientation(LinearLayout.HORIZONTAL);
  17.  
  18.             // Icon
  19.             ImageView v = new ImageView(this.getContext());
  20.             // v.setBackgroundDrawable(iF.getResources().getDrawable(R.drawable.cube_icon));
  21.  
  22.             // Text
  23.             TextView txt = new TextView(this.getContext());
  24.             txt.setTextSize(Float.valueOf(prefs.getString("pref_txtSize", "12")));
  25.             txt.setPadding(10, 10, 10, 10);
  26.             txt.setText(this.getItem(position));
  27.             txt.setTextColor(getLineColor(position));
  28.  
  29.             // Shortcut
  30.             LinearLayout shortLin = new LinearLayout(this.getContext());
  31.             shortLin.setGravity(Gravity.RIGHT);
  32.  
  33.             LayoutParams par = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
  34.             shortLin.setLayoutParams(par);
  35.  
  36.             TextView s = new TextView(this.getContext());
  37.             s.setTextSize(Float.valueOf(prefs.getString("pref_txtSize", "12")));
  38.             s.setWidth(iF.getResources().getDimensionPixelSize(R.dimen.shortcutWidth));
  39.             s.setPadding(10, 10, 10, 10);
  40.  
  41.             s.setText(getShortcut(position));
  42.  
  43.             shortLin.addView(s);
  44.  
  45.             // Return
  46.             txt.invalidate();
  47.             v.invalidate();
  48.             s.invalidate();
  49.  
  50.             lin.addView(v);
  51.             lin.addView(txt);
  52.             lin.addView(shortLin);
  53.  
  54.             return lin;
  55.         }
  56.     };
  57.        
  58. public View getView(int position, View convertView, ViewGroup parent) {
  59.  
  60.     View view = convertView;
  61.     ViewHolder holder;
  62.  
  63.     if (view == null) {
  64.  
  65.         LayoutInflater inflater = (LayoutInflater) context
  66.                 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  67.         view = inflater
  68.                 .inflate(R.layout.list_view_home_item, parent, false);
  69.  
  70.         holder = new ViewHolder();
  71.         holder.title = (TextView) view
  72.                 .findViewById(R.id.textView);
  73.  
  74.         holder.title.setText("blah");
  75.         view.setTag(holder);            
  76.     } else {        
  77.         holder = (ViewHolder) view.getTag();
  78.     }
  79.  
  80.     return view;
  81.        
  82. static class ViewHolder {
  83.     TextView title;
  84. }
  85.        
  86. @Override
  87. public View getView(int position, View convertView, ViewGroup grp) {
  88.     if(convertView == null) {
  89.         LinearLayout lin = new LinearLayout(this.getContext());
  90.         lin.setOrientation(LinearLayout.HORIZONTAL);
  91.  
  92.         // Icon
  93.         ImageView v = new ImageView(this.getContext());
  94.         // v.setBackgroundDrawable(iF.getResources().getDrawable(R.drawable.cube_icon));
  95.  
  96.         // Text
  97.         TextView txt = new TextView(this.getContext());
  98.         txt.setId(1);
  99.         txt.setTextSize(Float.valueOf(prefs.getString("pref_txtSize", "12")));
  100.         txt.setPadding(10, 10, 10, 10);
  101.         txt.setTextColor(getLineColor(position));
  102.  
  103.         // Shortcut
  104.         LinearLayout shortLin = new LinearLayout(this.getContext());
  105.         shortLin.setGravity(Gravity.RIGHT);
  106.  
  107.         LayoutParams par = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
  108.         shortLin.setLayoutParams(par);
  109.  
  110.         TextView s = new TextView(this.getContext());
  111.         s.setId(2);
  112.         s.setTextSize(Float.valueOf(prefs.getString("pref_txtSize", "12")));
  113.         s.setWidth(iF.getResources().getDimensionPixelSize(R.dimen.shortcutWidth));
  114.         s.setPadding(10, 10, 10, 10);
  115.  
  116.         shortLin.addView(s);
  117.         lin.addView(v);
  118.         lin.addView(txt);
  119.         lin.addView(shortLin);
  120.     }
  121.  
  122.     TextView txt = (TextView)convertView.findViewById(1);
  123.     txt.setText(this.getItem(position));
  124.     TextView txt = (TextView)convertView.findViewById(2);
  125.     txt.setText(getShortcut(position));
  126.  
  127.  
  128.     return lin;
  129. }
  130.        
  131. @Override
  132.         public View getView(int position, View convertView, ViewGroup parent) {
  133.  
  134.             View view = convertView;
  135.             ViewHolder holder;
  136.  
  137.             if (view == null) {
  138.  
  139.                 LayoutInflater inflater = (LayoutInflater) iF.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  140.                 view = inflater.inflate(R.layout.list_item, parent, false);
  141.  
  142.                 holder = new ViewHolder();
  143.                 holder.title = (TextView) view.findViewById(R.id.item_text);
  144.                 holder.shortcut = (TextView) view.findViewById(R.id.item_short);
  145.  
  146.                 holder.title.setId(1);
  147.                 holder.shortcut.setId(2);
  148.  
  149.                 holder.title.setText(getItem((position)));
  150.                 holder.shortcut.setText(getShortcut(position));
  151.  
  152.                 holder.title.setTextSize(Float.valueOf(prefs.getString("pref_txtSize", "12")));
  153.                 holder.shortcut.setTextSize(Float.valueOf(prefs.getString("prefs_txtSize", "12")));
  154.  
  155.                 view.setTag(holder);
  156.             } else {
  157.                 holder = (ViewHolder) view.getTag();
  158.  
  159.                 TextView title = (TextView) convertView.findViewById(1);
  160.                 title.setText(getItem(position));
  161.  
  162.                 TextView shortcut = (TextView) convertView.findViewById(2);
  163.                 shortcut.setText(getShortcut(position));
  164.             }
  165.  
  166.             return view;
  167.         }