如何使用Android在View方法上返回多个View对象

时间:2014-04-24 16:13:42

标签: java android design-patterns

我有这个适配器,它将自定义列表视图显示到我的布局:

    public class AttendedBaseAdapter extends ArrayAdapter<DashboardListing> {

    private List<DashboardListing> items;
    private Activity activity;
    private static View v, vDashboard;

    public AttendedBaseAdapter(Activity a, int textViewResourceId, List<DashboardListing> items){
        super(a, textViewResourceId, items);
        activity = a;
        this.items = items;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        v = convertView;
        vDashboard = convertView;

        final DashboardListing attended = items.get(position);


        LayoutInflater vi = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.row_attended, parent, false);

        LayoutInflater viDashboard = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        vDashboard = viDashboard.inflate(R.layout.fragment_dashboard, parent, false);

        SmartImageView imgVenue = (SmartImageView) v.findViewById(R.id.event_poster_url);
        imgVenue.setImageUrl(attended.getPoster());


        TextView txtEventName = (TextView) v.findViewById(R.id.event_name);
        TextView txtVenueName = (TextView) v.findViewById(R.id.event_venue_name);

        txtEventName.setText(attended.getName());
        txtVenueName.setText(attended.getVenue());

        TextView txtAttendedCount = (TextView) vDashboard.findViewById(R.id.txt_attended_count);

        txtAttendedCount.setText("100");


        return v;
    }

}

在getView(...)方法的这一部分:

@Override
 public View getView(int position, View convertView, ViewGroup parent) {
        v = convertView;
        vDashboard = convertView;

        final DashboardListing attended = items.get(position);


        LayoutInflater vi = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.row_attended, parent, false);

        LayoutInflater viDashboard = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        vDashboard = viDashboard.inflate(R.layout.fragment_dashboard, parent, false);

        SmartImageView imgVenue = (SmartImageView) v.findViewById(R.id.event_poster_url);
        imgVenue.setImageUrl(attended.getPoster());


        TextView txtEventName = (TextView) v.findViewById(R.id.event_name);
        TextView txtVenueName = (TextView) v.findViewById(R.id.event_venue_name);

        txtEventName.setText(attended.getName());
        txtVenueName.setText(attended.getVenue());

        TextView txtAttendedCount = (TextView) vDashboard.findViewById(R.id.txt_attended_count);

        txtAttendedCount.setText("100");


        return v;
    }

如何返回多个(不仅仅是视图 v ,但我还想返回视图 vDashboard ?因为我有2个布局View v指的是 R.layout.row_attended 和View vDashboard引用 R.layout.fragment_dashboard

1 个答案:

答案 0 :(得分:0)

  

如何返回多个(不仅仅是View v,但我还想返回View vDashboard?

您需要将它们包装在某种容器中(例如,LinearLayout),其中包含如何在该容器中调整和定位它们的规则,然后从getView()返回容器。

更简单的方法是直接或使用<include>元素组合布局文件,这样您只需要为一个布局充气。

换句话说,getView()会返回一个 View,它将成为ListView行(或GridView单元格或其他内容)给定的positionView可以是ViewGroup填充的任何内容,只要它符合您的UI要求。