1

我正在使用 Mono For Android (C#),我试图让 ListView 连续显示 2 行。有一些教程,但其中一些在 MFA 中不起作用,而那些起作用的教程非常复杂。在 ListView 中显示 2 行的最简单方法是什么?

4

3 回答 3

2

我假设如果您希望在列表项中超过 1 行,您肯定必须使用 customAdapter 类,因此我将在自定义适配器类下方发布,该类将在一行的两行显示相同的字符串,您基于它进行修改按要求!

public class CustomAdapter extends ArrayAdapter<String>{
private Activity activity;
public CustomAdapter(Context context, int textViewResourceId, List<String> objects) {
    super(context, textViewResourceId, objects);
    this.activity = (Activity)context;
}

public View getView(int position, View convertView, ViewGroup parent) {
    //View customView = convertView;
    String stringValue = getItem(position);
    // you can use this method as well instead of what i have used in this example, inflate your customView referring to a preformed xml file. extract the fields required from this view and set their values 
    //if (customView == null) {
    //  final LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    //  customView = vi.inflate(R.layout.your_custom_list_row, null);
    //}
    //extract fields from your_custom_list_row and set their values


LinearLayout customView = new LinearLayout(activity);

TextView t1 = new TextView(activity);
t1.setText(stringValue);
TextView t2= new TextView(activity);
t2.setText(stringValue);

customView.addView(t1);
customView.addView(t2);
return customView;
}


}

这将进入您的主要活动

List<String> stringsList = new ArrayList<String>();
//populate arraylist
CustomAdapter custAdpt = new CustomAdapter(this, R.layout.your_custom_list_row, stringsList);
listView.setAdapter(custAdpt);
于 2012-12-27T10:48:25.680 回答
1

在你的 getview() 方法中尝试这个:

ViewHolder holder;
         if (convertView   == null) 
            {
                //this should only ever run if you do not get a view back            
             LayoutInflater  inflater = (LayoutInflater) contxt
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
             convertView  = inflater.inflate(R.layout.homelistrow, null); 

             holder = new ViewHolder();

             holder.text = (Textview) convertView
                        .findViewById(R.id.icon);


             holder.text1 = (TextView) convertView
                        .findViewById(R.id.name_label);
             convertView.setTag(holder);


            }
         else {

                         holder = (ViewHolder) convertView.getTag();

                     }

         holder.text.setText(Your 1st row value);
         holder.text2.setText(Your 2nd row value);
于 2012-12-27T11:50:52.537 回答
1

也许您想要的是多个项目类型,可以在单个列表视图中显示不同的项目。如果是这样,您可以使用这样的自定义适配器:

package com.example.myapplication.customarray;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.myapplication.R;
import java.util.List;

/**
 * Created by SONY on 14/10/2016.
 */
public class CustomAdapter extends ArrayAdapter<BaseItem> {

    private List<BaseItem> data;

    public CustomAdapter(Context context, List<BaseItem> data) {
        super(context, 0, data);
        this.data = data;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        BaseItem baseItem = data.get(position);
        if(convertView == null) {
            if(baseItem.type == 0) {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_student, parent, false);
                convertView.setTag(ViewHolder.createHolder(convertView));
            }
            else {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_header, parent, false);
                convertView.setTag(ViewHolderHeader.createViewHolder(convertView));
            }
        }
        if(baseItem.type == 0) {
            ViewHolder holder = (ViewHolder) convertView.getTag();
            StudentItem student = (StudentItem) data.get(position);
            holder.textName.setText(student.name);
            holder.textID.setText(student.id);
            holder.imgStudent.setImageResource(student.picture);
        }
        else {
            ViewHolderHeader holderHeader = (ViewHolderHeader) convertView.getTag();
            HeaderItem headerItem = (HeaderItem) data.get(position);
            holderHeader.textLabel.setText(headerItem.label);
        }
       return convertView;
    }

    @Override
    public int getCount( ) {
        return data.size();
    }
    @Override
    public int getViewTypeCount() {
        return 2;
    }
    @Override
    public int getItemViewType(int position) {
        BaseItem item = data.get(position);
        return item.type;
    }



    public static class ViewHolder  {
        public TextView textName;
        public TextView textID;
        public ImageView imgStudent;

        public static final ViewHolder createHolder(View view) {
            ViewHolder holder = new ViewHolder();
            holder.textName = (TextView) view.findViewById(R.id.textName);
            holder.textID = (TextView) view.findViewById(R.id.textID);
            holder.imgStudent = (ImageView) view.findViewById(R.id.imgStudent);
            return holder;
        }
    }
    public static class ViewHolderHeader {
        public TextView textLabel;

        public static final ViewHolderHeader createViewHolder(View view) {
            ViewHolderHeader holderHeader = new ViewHolderHeader();
            holderHeader.textLabel = (TextView)view.findViewById(R.id.textHeader);
            return holderHeader;
        }
    }
}

你可以在这里阅读

于 2016-10-16T15:49:01.410 回答