ListFragment适配器的通用视图持有者类型

时间:2014-12-18 08:20:56

标签: java android generics android-fragments android-adapter

我希望能够拥有适用于任何产品的通用适配器 ListFragment,以便我可以发送任何类型的viewholder,它只会加载列表。这就是我到目前为止所遇到的一些错误。我原本想要一个适用于朋友列表的适配器,但现在我希望能够将适配器重用于未来的项目。我来自c ++背景,所以这里的模板有点不同。 如果你们可以告诉我在实际的ListFragment中做什么,那将是我的一年

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import java.util.ArrayList;


public class FriendsAdapter<T> extends ArrayAdapter<T>
{
private ArrayList<T> objects;
private final LayoutInflater inflater;
private int resources;
private T viewHolder;
private int currPosition;

public FriendsAdapter(Context context, ArrayList<T> objects,
                      int resources, AdapterCommand<T> listener, T viewholder)
{
    super(context, R.layout.friends_fragment);
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount()
{
    return objects.size();
}

@Override
public T getItem(int position)
{
    return objects.get(position);
}

@Override
public long getItemId(int position)
{
    return 0;
}
public int getPosition()
{
   return this.currPosition;
}

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
    View rowView = convertView;
    T viewHolder;
    currPosition = position;
    if (rowView == null)
    {
          // alternate layouts
        if (position % 2 == 0)
        {
            rowView = inflater.inflate(R.layout.friends_fragment_even, null);
        }
        else
        {
            rowView = inflater.inflate(R.layout.friends_fragment_odd, null);
        }

        rowView = inflater.inflate(resources, parent, false);
        viewHolder = new T();
        rowView.setTag(viewHolder);
    }
    else
    {
        viewHolder = (T) rowView.getTag();
    }

    T object = getItem(position);
    return rowView;
}

/*
public void NewFriend(String name,String status,Drawable img) // for adding a single friend to the list 
{
    Friend newFriend = new Friend(name, status, img);
    if (rowView == null)
    {


    }
}
*/
public void DeletedFriend(String name,String hash) // delete a single friend from the list
{
  // loop through , find name , remove the item from the list

}

}

1 个答案:

答案 0 :(得分:1)

正如here所解释的,您可以使用GenericViewHolder(在我的应用程序中用于RecyclerAdapter而不是ListView的适配器)

这是我的实施

public static class GenericViewHolder extends RecyclerView.ViewHolder {

    private SparseArray<View> holder = new SparseArray<View>();

    public GenericViewHolder(View v) {
        super(v);
        deepScan((ViewGroup)v);
    }

    private void deepScan(ViewGroup v) {
        if (v.getChildCount() == 0) return;
        for(int i=0; i< v.getChildCount(); ++i) {
            View nextChild = v.getChildAt(i);
            put(nextChild);
            if (nextChild instanceof ViewGroup) deepScan((ViewGroup)nextChild);
        }
    }

    public <T extends View> T get(Integer id) {
        return (T) holder.get(id);
    }

    public void put(View view) { put(view, view.getId()); }

    public void put(View view, Integer id) {
        if (id == -1) return;  // Mean that hasn't id
        holder.put(id, view);
    }

    public void putViewFromParent(View parent, Integer id) {
        View view = parent.findViewById(id);
        put(view, id);
    }

    public void clear() {
        holder.clear();
    }

    public TextView getAsTextView(int id) { return (TextView) holder.get(id); }
    public ImageView getAsImageView(int id) { return (ImageView) holder.get(id); }
    public EditText getAsEditText(int id) { return (EditText) holder.get(id); }
    public Button getAsButton(int id) { return (Button) holder.get(id); }
    public Spinner getAsSpinner(int id) { return (Spinner) holder.get(id); }
    public ListView getAsListView(int id) { return (ListView) holder.get(id); }

    // ... add more getter or use the generic form
    //    Type x = holder.<Type>get(id);
}