5

Tıklamadaki satır öğelerinin renklerini değiştirmeyi kolaylaştırmak için çok basit bir imleç özel imleç bağdaştırıcısı oluşturmak istiyorum. Aşağıdaki koduÖzel basit imleç bağdaştırıcısı oluşturma

private static int save = -1; 

public void onListItemClick(ListView parent, View v, int position, long id) { 

    parent.getChildAt(position).setBackgroundColor(Color.BLUE); 

    if (save != -1 && save != position){ 
     parent.getChildAt(save).setBackgroundColor(Color.BLACK); 
    } 

    save = position;     

} 

kullanarak bu akıştan kodu var https://stackoverflow.com/a/7649880/498449

ben basit imleç adaptörü kullanılır ve onClick kodunu yerleştirdiğiniz ama ListFragment varsayılan liste görünümleri yeniden kullanır çünkü olurdu

, siz kaydırdıkça, birden çok görünüm vurgulanır. IRC'de konuşurken, özel bir imleç bağdaştırıcısı oluşturduğum önerildi. Bununla birlikte, en iyi uygulamayı nasıl yapacağımı ve yukarıdaki kod snippet'inin nereye sığacağını bulamıyorum. Yardımı büyük ölçüde takdir edebiliriz.

public class AreaCursorAdapter extends CursorAdapter { 
    private Context context; 


    public AreaCursorAdapter(Context context, Cursor c) { 
     super(context, c); 
     // TODO Auto-generated constructor stub 
    } 

    @Override 
public void bindView(View view, Context context, Cursor cursor) { 
    TextView list_item = (TextView)view.findViewById(android.R.id.text1); 
    list_item.setText(cursor.getString(cursor.getColumnIndex(INDICATOR_NAME))); 

} 

@Override 
public View newView(Context context, Cursor cursor, ViewGroup parent) { 
    LayoutInflater inflater = LayoutInflater.from(context); 
    View v = inflater.inflate(android.R.layout.simple_list_item_1, parent, false); 
    bindView(v, context, cursor); 
    return v; 
} 

} 

İmleç bağdaştırıcısını çevrimiçi bulduğum bir kodla güncelleştirdim. Ancak iki problemim var. 1. Bir imleç yükleyici kullanıyorum, bu yüzden kurucuya geçmek için bir "imleç" nesnesine sahip değilim. 2. Eclipse'den, kurucunun kullanımdan kaldırıldığına dair bir uyarı alıyorum.

+0

NewView size, BindView içinde size seçim int göre içerik ve arka plan ayarlamak, LayoutInflater.inflate ile görünümü oluşturmak seçimi int olarak değiştirin ve bağdaştırıcıya içeriğinde bir değişiklik olduğunu söyleyin. – zapl

+0

@zapl Kod snippet'i gösterebilir misiniz? Ayrıca, bunu bir imleç yükleyici kullanarak yapıyorum, bu yüzden kurucuya geçmek için bir "imleç" nesnesi yok. –

+0

Şu anda bir snippet'im yok :(google muhtemelen.Yükleyicilerde 'onLoadFinished' içinde adapter.swapCursor()' işlevi var – zapl

cevap

18

O yol hakkında yapmak mümkün olmalıdır: onclick sen jsut içinde

class YourListFragment extends ListFragmentOrSomethingElse { 
    private AreaCursorAdapter mAdapter; 

    @Override  
    public void onCreate() { 
     mAdapter = new AreaCursorAdapter(this, null); 
     setListAdapter(mAdapter); 
    } 

    @Override 
    public void onListItemClick(ListView parent, View v, int position, long id) { 
     mAdapter.setSelectedPosition(position); 
    } 

    @Override 
    public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) { 
     mAdapter.swapCursor(cursor); 
     // should reset that here maybe 
     mAdapter.setSelectedPosition(-1); 
    } 
} 

public class AreaCursorAdapter extends CursorAdapter { 
    private Context context; 
    private int mSelectedPosition; 
    LayoutInflater mInflater; 

    public AreaCursorAdapter(Context context, Cursor c) { 
     // that constructor should be used with loaders. 
     super(context, c, 0); 
     mInflater = LayoutInflater.from(context); 
    } 

    public void setSelectedPosition(int position) { 
     mSelectedPosition = position; 
     // something has changed. 
     notifyDataSetChanged(); 
    } 

    @Override 
    public void bindView(View view, Context context, Cursor cursor) { 
     TextView list_item = (TextView)view.findViewById(android.R.id.text1); 
     list_item.setText(cursor.getString(cursor.getColumnIndex(INDICATOR_NAME))); 
     int position = cursor.getPosition(); // that should be the same position 
     if (mSelectedPosition == position) { 
      view.setBackgroundColor(Color.RED); 
     } else { 
      view.setBackgroundColor(Color.WHITE); 
     } 
    } 

    @Override 
    public View newView(Context context, Cursor cursor, ViewGroup parent) { 
     View v = mInflater.inflate(android.R.layout.simple_list_item_1, parent, false); 
     // edit: no need to call bindView here. That's done automatically 
     return v; 
    } 

} 
+0

Sen bir hayat kurtarıcısın! Yukarıdaki kodla çalışmasını sağlayın –

+0

Sağlayıcıyı listelemede nerede bulabilirim? – nia

+0

@nia Bu, bir "CursorLoader" kullanıyordu. Sorgu 'onCreateLoader' içinde değil (değil) Yukarıdaki kodda gösterilmiştir) http://helpmeco.de/2012/3/using-an-android-cursor-loader-with-a-content-provider örneğin – zapl